Hdu 1498 二分匹配

本文介绍了一款庆祝HDU50周年纪念日的游戏——“撞色气球”。该文详细解析了游戏规则及背景,并提供了一份完整的C++代码实现,用于解决游戏中学生在限定次数内无法完成的特定颜色气球碰撞的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

 

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: sudaweitong@gmail.com
 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 }

 

转载于:https://www.cnblogs.com/Stomach-ache/p/3843689.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值