1. 程式人生 > 其它 >hdu----149850 years, 50 colors(最小覆蓋點)

hdu----149850 years, 50 colors(最小覆蓋點)

50 years, 50 colors

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

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.

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校園文化活動月”之“校慶杯”大學生程式設計競賽暨杭州電子科技大學第四屆大學生程式設計競賽

Recommend

看了好幾遍愣是沒看懂題目什麼意思....

看了別人的題解才懂這道題要我們幹啥。

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 const int maxn = 110;
 5 int ma[maxn][maxn];
 6 int line[maxn],res[maxn];
 7 bool vis[maxn],bj[maxn];
 8 int k,n,m,t;
 9 template <class T>
10 void init(T a){
11     memset(a,0,sizeof(a));
12 }
13 int match(int st,int u)
14 {
15     for(int v = 1;v<=n;v++)
16     {
17         if(ma[u][v]== st && !vis[v])
18         {
19             vis[v] = 1;
20             if(line[v]==-1 || match(st,line[v]))
21             {
22                 line[v] = u;
23                 return 1;
24             }
25         }
26     }
27     return 0;
28 }
29 int cmp(const void *a,const void *b){
30     return *(int *)a - *(int *)b;
31 }
32 int K_M(int st)
33 {
34     memset(line,-1,sizeof(line));
35     int ans = 0;
36     for(int i = 1;i<=n;i++){
37         init(vis);
38         ans += match(st,i);
39     }
40     return ans;
41 }
42 int main()
43 {
44     int t;
45     while(~scanf("%d%d",&n,&k))
46     {
47         if(n==0 && k== 0) break;
48         init(ma); init(res); init(bj);
49         for(int i = 1;i<=n;i++)
50         {
51             for(int j = 1;j<=n;j++)
52             {
53                 scanf("%d",&t);
54                 ma[i][j] = t;
55                 if(!bj[t])
56                     bj[t] = 1;
57             }
58         }
59         int cnt,num = 0;
60         for(int i = 1;i<=55;i++)
61         {
62             if(bj[i]){
63                  cnt = K_M(i);
64                  (cnt>k)?(res[num++] = i):(1);
65             }
66         }
67         if(num==0) puts("-1");
68         else{
69             qsort(res,num,sizeof(res[0]),cmp);
70             for(int j = 0;j<num-1;j++)
71                 printf("%d ",res[j]);
72             printf("%dn",res[num-1]);
73         }
74     }
75     return 0;
76 }