hdu1498 50 years, 50 colors(最小顶点覆盖)
2016-05-27 19:09
337 查看
50 years, 50 colors
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
**有一个n*n的矩形,矩形内最多有50种颜色的气球,你可以选择一种颜色的气球,每次可以选着一行或一列,然后将这一行或一列这种颜色的气球戳爆,问能否在k次内将任意一种颜色的气球戳爆,可以的话输出-1,不能的话,将不能的实现气球序号按升序输出。
左边横坐标,右边纵坐标,将序号k1的气球所在位置的横坐标与纵坐标连起来, 求最小顶点覆盖
(寻找一个点集,使得图上任意一条边至少一个端点位于这个点集内部。)二分图的|最小点集|=|最大匹配|**
90ms
200+ms
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
**有一个n*n的矩形,矩形内最多有50种颜色的气球,你可以选择一种颜色的气球,每次可以选着一行或一列,然后将这一行或一列这种颜色的气球戳爆,问能否在k次内将任意一种颜色的气球戳爆,可以的话输出-1,不能的话,将不能的实现气球序号按升序输出。
左边横坐标,右边纵坐标,将序号k1的气球所在位置的横坐标与纵坐标连起来, 求最小顶点覆盖
(寻找一个点集,使得图上任意一条边至少一个端点位于这个点集内部。)二分图的|最小点集|=|最大匹配|**
90ms
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; int matching[150150]; int used[150]; int n, k; int a[150][150], plan[150][150]; void getmap(int r); int hungarian(int r); bool dfs(int u, int r); int ans[55]; /*struct node { int n = 0; int v = 0; } ans[120]; bool cmp(const node a, const node b) { if(a.v == b.v) return a.n < b.n; return a.n < b.n; } */ int flog[55]; int main() { while(scanf("%d%d", &n, &k) , n) { memset(flog, 0, sizeof(flog)); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++){ scanf("%d", &a[i][j]); flog[a[i][j]] = 1; } } int num = 0; memset(ans, 0, sizeof(ans)); for(int i = 1; i < 55; i++) { if(flog[i]) { if(hungarian(i) - k > 0) ans[num++] = i; } } if(num == 0) printf("-1\n"); else{ for(int i= 0; i < num - 1; i++) printf("%d ", ans[i]); printf("%d\n", ans[num - 1]); } } } void getmap(int r) { for(int i = 1; i <= n; i ++) { for(int j = 1; j <= n; j++) { if(a[i][j] == r) plan[i][j] = 1; } } } int hungarian(int r) { int ans = 0; memset(matching, -1, sizeof(matching)); for(int i = 1; i <= n; i++) { memset(used, 0, sizeof(used)); if(dfs(i, r)) ans++; } return ans; } bool dfs(int u, int r) { for(int i = 1; i <= n; i++) { if(a[u][i] == r && !used[i]) { used[i] = true; if(matching[i] == -1 || dfs(matching[i], r)) { matching[i] = u; return true; } } } return false; }
200+ms
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; int matching[150150]; int used[150]; int n, k; int a[150][150], plan[150][150]; void getmap(int r); int hungarian(); bool dfs(int u); int ans[55]; /*struct node { int n = 0; int v = 0; } ans[120]; bool cmp(const node a, const node b) { if(a.v == b.v) return a.n < b.n; return a.n < b.n; } */ int main() { while(scanf("%d%d", &n, &k) , n) { for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) scanf("%d", &a[i][j]); } //printf("-1"); int num = 0; memset(ans, 0, sizeof(ans)); for(int i = 1; i <= 50; i++) { memset(plan, 0, sizeof(plan)); getmap(i); // ans[i].v = hungarian() - k; if(hungarian() - k > 0) ans[num++] = i; } // sort(ans + 1, ans + 55 + 1, cmp); // printf("%d\n", ans[55].v); if(num == 0) { printf("-1\n"); } else { for(int i = 0; i < num -1; i++) { // if(ans[i].v > 0) printf("%d ",ans[i]); } printf("%d\n", ans[num - 1]); } } } void getmap(int r) { for(int i = 1; i <= n; i ++) { for(int j = 1; j <= n; j++) { if(a[i][j] == r) plan[i][j] = 1; } } } int hungarian() { int ans = 0; memset(matching, -1, sizeof(matching)); for(int i = 1; i <= n; i++) { memset(used, 0, sizeof(used)); if(dfs(i)) ans++; } return ans; } bool dfs(int u) { for(int i = 1; i <= n; i++) { if(plan[u][i] && !used[i]) { used[i] = true; if(matching[i] == -1 || dfs(matching[i])) { matching[i] = u; return true; } } } return false; }
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe