您的位置:首页 > 其它

HDOJ-----1498--50 years, 50 colors二分图(最小顶点覆盖)

2016-08-18 10:37 399 查看

50 years, 50 colors

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2293    Accepted Submission(s): 1292


[align=left]Problem Description[/align]
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.



 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
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".

 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

-1
1
2
1 2 3 4 5
-1

输入n, k,然后一个n*n矩阵,数字代表气球,相同的数字代表一类气球,每次可以踩爆一行或一列的气球,把k次不能全部踩爆的种类从小到大输出

话说这个术语叫最小顶点覆盖,用最少的顶点覆盖最多的行列数,覆盖数就是二分图最大匹配数,不过要把模板做一点调整

用vector写的,不喜欢用矩阵

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define ll long long
#define maxn 101
#define inf 0x3f3f3f3f
#define mes(a, b) memset(a, b, sizeof(a))
using namespace std;
int link[maxn], put[maxn];
int map[maxn][maxn];
bool vis[maxn], mark[maxn];
int n;
vector<int > edge[maxn];
bool match(int u, int val){
for(int i = 0; i < edge[u].size(); i++){
int v = edge[u][i];
if(!vis[i] && v == val){//核心就在这里了,把模板的vis标记以及link的连接从edge[u][i]这个值,改为i这个列数
vis[i] = true;      //再加一个v=val做判断,画幅图很好理解
if(link[i] == -1 || match(link[i], val)){
link[i] = u;
return true;
}
}
}
return false;
}
int Getmatch(int c){//获取最大匹配
int ans = 0;
mes(link, -1);
for(int i = 1; i <= n; i++){
mes(vis, false);
if(match(i, c)){//传入c作一个比较值
ans++;
}
}
return ans;
}
int main(){
int t, k;
while(~scanf("%d%d", &n, &k), k || n){
mes(mark, false);
for(int i = 1; i <= n; i++){
edge[i].clear();
for(int j = 1; j <= n; j++){
scanf("%d", &t);
mark[t] = true;
edge[i].push_back(t);
}
}
int cnt = 0;
for(int i = 1; i <= 50; i++){//气球一共50种,遍历出现过的就可以了,mark标记出现
if(mark[i] && Getmatch(i) > k){
put[cnt++] = i;
}
}
if(!cnt){
printf("-1\n");
continue;
}
for(int i = 0; i < cnt; i++){
printf("%d%c", put[i], i == cnt-1 ? '\n' : ' ');
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: