50 years, 50 colors HDU - 1498(最小点覆盖或者说最小顶点匹配)
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,501,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
//题意在刚接触的时候很难理解,和别人讨论过后才真正理解是什么意思。。。
//在给你一个n*n的矩阵之后,再给一个k代表你有几次机会,颜色相同的为一类气球,不同数字代表不同类气球
//对于描述中的最后一句话“ If there is no choice, print "-1". ”,有点不好理解,
//其实意思是对于每一类气球,你都有k次机会,如果在k次机会用完之后,看**哪一类的气球不能全部被弄破**
//然后把这些不能在k次机会全部弄破的气球的代表数字按照从小到大输出,如果每一类的气球都能在k次机会被弄破,
//那么则输出-1。
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std;
const int INF=1e9+7;
const int maxn=510;
typedef long long ll;
int n,k;
int map[maxn][maxn];//存放整个图 map[i][j]的值 就是该方格的颜色
int link[maxn];
int color[maxn],vis[maxn];//color[i]为真 表示i号颜色在整个map中出现
int dfs(int i,int co)//本次dfs过程中 是用 第i行 颜色为 co 去匹配,
{
for(int j = 0; j < n; j ++)
{
if(!vis[j] && map[i][j] == co)
{
vis[j] = 1;
if(link[j] == -1 || dfs(link[j],co))
{
link[j] = i;//把点的编号记录下来
return 1;
}
}
}
return 0;
}
int KM(int co)
{
memset(link,-1,sizeof(link));//记录点的编号
int ans=0;
for(int i = 0; i < n ; i ++)
{
memset(vis,0,sizeof(vis));
if(dfs(i,co)) ans++;
}
return ans;
}
int main()
{
int i,j;
int ans[51],tol;//ans[] 存放的是要输出的不能消灭的颜色号,tol表示不能消灭的颜色数量
while(scanf("%d%d",&n,&k),n+k)
{
memset(color,0,sizeof(color));
memset(map,0,sizeof(map));
for(i = 0; i < n; i ++)
{
for(j = 0; j < n; j ++)
{
scanf("%d",&map[i][j]);
color[map[i][j]] = 1;
}
}
tol=0;
for(i = 1; i <= 50; i ++)
{
if(color[i])//注意这里表示 i 号颜色是不是有的
{
if(KM(i) > k)//如果匹配最小消除i号颜色用到的次数大于k 意思就是不能消灭
ans[tol++]=i;
}
}
sort(ans,ans+tol);//注意排序
if(tol == 0)
printf("-1");
else
for(i = 0; i < tol; i ++)
i == 0 ? printf("%d",ans[i]):printf(" %d",ans[i]);//这里是简单的格式控制,两数据中有空格
printf("\n");
}
return 0;
}