HDU——1498 50 years, 50 colors
阿新 • • 發佈:2017-08-26
ota range tro begin 最大 play ice 第四屆 multi
There will be a n*n matrix board on the ground, and each grid will have a color balloon in it.And the color of the ballon will be in the range of [1, 50].After the referee shouts "go!",you can begin to crash the balloons.Every time you can only choose one kind of balloon to crash, we define that the two balloons with the same color belong to the same kind.What‘s more, each time you can only choose a single row or column of balloon, and crash the balloons that with the color you had chosen. Of course, a lot of students are waiting to play this game, so we just give every student k times to crash the balloons.
Here comes the problem: which kind of balloon is impossible to be all crashed by a student in k times.
給你一個 n*n 的矩陣,每個格子上對應著相應顏色的氣球,每次你可以選擇一行或一列的同種顏色的氣球進行踩破,問你在K次這樣的操作後,哪些顏色的氣球是不可能被踩破完的。
思路:
本題要求的是求出什麽樣的氣球是根本就不可能被踩破的,而不是求的我們經過k次操作後剩下的氣球的情況。
這樣的話我們可以這樣來考慮:現在我們一共有k次操作,也就是說對於每一種顏色的球,我們有k次機會都可以選擇這一種顏色來把它全部清除。這樣的話我們只需要判斷我們在進行k次操作以後,我們當前選的顏色是否可以被完全清除就好了!
怎麽判斷?!我們仍然可以采用二分圖來做,我們對於每一種顏色的球單獨處理,將其橫縱坐標看成兩類,每一次只能選擇一行或者是一列,這就跟上一個題是一樣的了,然後進行二分圖匹配,這樣我們匹配出來的是我們可以用幾次來將這種顏色的求全部消滅。
我們判斷我們構建出來的圖的最大匹配數是否大於k,若不是,那麽說明我們可以將這種球消滅。反之,則不可以。記錄。
代碼:
50 years, 50 colors
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2683 Accepted Submission(s): 1538
There will be a n*n matrix board on the ground, and each grid will have a color balloon in it.And the color of the ballon will be in the range of [1, 50].After the referee shouts "go!",you can begin to crash the balloons.Every time you can only choose one kind of balloon to crash, we define that the two balloons with the same color belong to the same kind.What‘s more, each time you can only choose a single row or column of balloon, and crash the balloons that with the color you had chosen. Of course, a lot of students are waiting to play this game, so we just give every student k times to crash the balloons.
Here comes the problem: which kind of balloon is impossible to be all crashed by a student in k times.
Input There will be multiple input cases.Each test case begins with two integers n, k. n is the number of rows and columns of the balloons (1 <= n <= 100), and k is the times that ginving to each student(0 < k <= n).Follow a matrix A of n*n, where Aij denote the color of the ballon in the i row, j column.Input ends with n = k = 0. Output For each test case, print in ascending order all the colors of which are impossible to be crashed by a student in k times. If there is no choice, print "-1". Sample Input 1 1 1 2 1 1 1 1 2 2 1 1 2 2 2 5 4 1 2 3 4 5 2 3 4 5 1 3 4 5 1 2 4 5 1 2 3 5 1 2 3 4 3 3 50 50 50 50 50 50 50 50 50 0 0 Sample Output -1 1 2 1 2 3 4 5 -1 Author 8600 Source “2006校園文化活動月”之“校慶杯”大學生程序設計競賽暨杭州電子科技大學第四屆大學生程序設計競賽 題目大意:
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define N 110 using namespace std; bool vis[N],vist[N]; int n,k,sum,tot,ans[N],girl[N],a[N][N],map[N][N]; int read() { int x=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1; ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘; ch=getchar();} return x*f; } int find(int x) { for(int i=1;i<=n;i++) { if(!vis[i]&&map[x][i]) { vis[i]=true; if(girl[i]==-1||find(girl[i])) {girl[i]=x; return 1;} } } return 0; } int col() { int s=0; memset(girl,-1,sizeof(girl)); for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); if(find(i)) s++; } return s; } void begin() { sum=0,tot=0; memset(a,0,sizeof(a)); memset(ans,0,sizeof(ans)); memset(vist,0,sizeof(vist)); } int main() { while(1) { n=read(),k=read(); if(n==0&&k==0) break; begin(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) a[i][j]=read(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(!vist[a[i][j]]) { vist[a[i][j]]=true; memset(map,0,sizeof(map)); for(int u=1;u<=n;u++) for(int v=1;v<=n;v++) if(a[u][v]==a[i][j]) map[u][v]=1; if(col()>k) ans[++sum]=a[i][j]; } if(sum==0) {printf("-1\n"); continue;} sort(ans+1,ans+1+sum); for(int i=1;i<sum;i++) printf("%d ",ans[i]); printf("%d\n",ans[sum]); } return 0; }
HDU——1498 50 years, 50 colors