Hdu 1498 二分匹配

50 years, 50 colors

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1484    Accepted Submission(s): 798


Problem Description
On Octorber 21st, HDU 50-year-celebration, 50-color balloons floating around the campus, it's so nice, isn't it? To celebrate this meaningful day, the ACM team of HDU hold some fuuny games. Especially, there will be a game named "crashing color balloons".

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.

Hdu 1498 二分匹配

 

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
Accepted Code:
 1 /*************************************************************************  2  > File Name: 1498.cpp  3  > Author: Stomach_ache  4  > Mail: [email protected]  5  > Created Time: 2014年07月14日 星期一 22时35分33秒  6  > Propose:  7  ************************************************************************/

 8 

 9 #include <cmath>

10 #include <string>

11 #include <cstdio>

12 #include <fstream>

13 #include <cstring>

14 #include <iostream>

15 #include <algorithm>

16 using namespace std; 17 

18 const int MAX_N = 100 + 5; 19 int n, k; 20 int cx[MAX_N], cy[MAX_N], mark[MAX_N]; 21 int G[MAX_N][MAX_N], A[MAX_N][MAX_N], ans[MAX_N]; 22 

23 int

24 path(int u) { 25       for (int v = 1; v <= n; v++) { 26           if (!mark[v] && G[u][v]) { 27               mark[v] = 1; 28             if (cy[v] == -1 || path(cy[v])) { 29                   cx[u] = v; 30                 cy[v] = u; 31                 return 1; 32  } 33  } 34  } 35     return 0; 36 } 37 

38 int

39 MaxMatch() { 40       memset(cx, -1, sizeof(cx)); 41     memset(cy, -1, sizeof(cy)); 42     int res = 0; 43     for (int i = 1; i <= n; i++) { 44           if (cx[i] == -1) { 45               memset(mark, 0, sizeof(mark)); 46               res += path(i); 47  } 48  } 49     return res; 50 } 51 

52 void

53 init(int x) { 54       memset(G, 0, sizeof(G)); 55     for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (A[i][j] == x) { 56           G[i][j] = 1; 57  } 58 } 59 

60 void

61 solve() { 62       int cnt = 0; 63       for (int i = 1; i <= 50; i++) { 64  init(i); 65         if (MaxMatch() > k) ans[cnt++] = i; 66  } 67     if (cnt == 0) { 68         puts("-1"); 69  } 70     else { 71           for (int i = 0; i < cnt; i++) { 72               printf("%d%c", ans[i], i == cnt-1 ? '\n' : ' '); 73  } 74  } 75 } 76 

77 int

78 main(void) { 79       while (~scanf("%d %d", &n, &k) && n+k) { 80           for (int i = 1; i <= n; i++) { 81               for (int j = 1; j <= n; j++) { 82                   scanf("%d", &A[i][j]); 83  } 84  } 85  solve(); 86  } 87 

88     return 0; 89 }

 

你可能感兴趣的:(HDU)