zoj 2362 Beloved Sons【二分匹配】
来源:互联网 发布:java接口管理系统 编辑:程序博客网 时间:2024/05/01 21:49
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361
来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26760#problem/B
Once upon a time there lived a king and he had N sons. And the king wanted to marry his beloved sons on the girls that they did love. So one day the king asked his sons to come to his room and tell him whom do they love.
But the sons of the king were all young men so they could not tell exactly whom they did love. Instead of that they just told him the names of the girls that seemed beautiful to them, but since they were all different, their choices of beautiful girls also did not match exactly.
The king was wise. He did write down the information that the children have provided him with and called you, his main wizard.
"I want all my kids to be happy, you know," he told you, "but since it might be impossible, I want at least some of them to marry the girl they like. So please, prepare the marriage list."
"I want all my kids to be happy, you know," he told you, "but since it might be impossible, I want at least some of them to marry the girl they like. So please, prepare the marriage list."
So, go on, make a list to maximize the king's happiness.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
The first line of the input file contains N - the number of king's sons (1 <= N <= 400). The second line contains N integer numbers Airanging from 1 to 1000 - the measures of king's love to each of his sons.
Next N lines contain lists of king's sons' preferences - first Ki - the number of the girls the i-th son of the king likes, and then Ki integer numbers - the girls he likes (all potentially beautiful girls in the kingdom were numbered from 1 to N, you know, beautiful girls were rare in those days).
Output
Output N numbers - for each son output the number of the beautiful girl he must marry or 0 if he must not marry the girl he likes.
Denote the set of sons that marry a girl they like by L, then you must maximize the value of
Sample Input
1
4
1 3 2 4
4 1 2 3 4
2 1 4
2 1 4
2 1 4
Sample Output
2 1 0 4
Author: Andrew Stankevich
Source: Andrew Stankevich's Contest #3
题意:
算法:二分匹配
思路:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 410;int map[maxn][maxn];int match[maxn];bool vis[maxn];int uN, vN;bool dfs(int u){ for(int v = 1; v <= vN; v++) { if(!vis[v] && map[u][v]) { vis[v] = true; if(match[v] == -1 || dfs(match[v])) { match[v] = u; return true; } } } return false;}bool hungary(){ int sum = 0; memset(match, -1, sizeof(match)); for(int j = 1 ; j <= uN; j++) { memset(vis, false, sizeof(vis)); if(dfs(i)) sum++; } if(sum == uN) return true; else return false;}
推荐题目:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 410;int map[maxn][maxn];int match[maxn];bool vis[maxn];int uN, vN;struct Son{ int index; int a;}son[maxn];bool dfs(int u){ for(int v = 1; v <= vN; v++) { if(!vis[v] && map[u][v]) { vis[v] = true; if(match[v] == -1 || dfs(match[v])) { match[v] = u; return true; } } } return false;}void hungary(){ int sum = 0; memset(match, -1, sizeof(match)); for(int j = 1 ; j <= uN; j++) //按照国王喜爱程度匹配 { int i = son[j].index; memset(vis, false, sizeof(vis)); dfs(i); }}
BAccepted844 KB260 msC++ (g++ 4.4.5)
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 410;int map[maxn][maxn];int match[maxn];bool vis[maxn];int uN, vN;struct Son{ int index; //编号 int a; //国王喜爱程度}son[maxn];int ans[maxn];bool cmp(Son A, Son B){ return A.a >= B.a;}bool dfs(int u){ for(int v = 1; v <= vN; v++) { if(!vis[v] && map[u][v]) { vis[v] = true; if(match[v] == -1 || dfs(match[v])) { match[v] = u; return true; } } } return false;}void hungary(){ int sum = 0; memset(match, -1, sizeof(match)); for(int j = 1 ; j <= uN; j++) //按照国王的喜爱程度依次匹配 { int i = son[j].index; memset(vis, false, sizeof(vis)); dfs(i); }}int main(){ int n; int T; scanf("%d", &T); while(T--) { scanf("%d", &n); uN = vN = n; memset(map, 0, sizeof(map)); for(int i = 1; i <= n; i++) { scanf("%d", &son[i].a); son[i].index = i; } sort(son+1,son+(n+1),cmp); // 按照喜爱程度排序 int num; for(int i = 1; i <= n; i++) //简单建图 { scanf("%d", &num); int index; for(int j = 1; j <= num; j++) { scanf("%d", &index); map[i][index] = 1; } } hungary(); memset(ans,0,sizeof(ans)); for(int i = 1; i <= n; i++) //调整结果 map[i] 表示的是第 i 个姑娘匹配的王子编号 { if(match[i] != -1) ans[match[i]] = i; } for(int i = 1; i <= n; i++) { if(i == 1) printf("%d", ans[i]); else printf(" %d", ans[i]); } printf("\n"); } return 0;}
- zoj 2362 Beloved Sons【二分匹配】
- zoj - 2362 - Beloved Sons(二分图最大匹配)
- zoj 3038 Beloved Sons(二分图匹配)
- ZOJ 2362 Beloved Sons
- Acdream 1129 Beloved Sons 二分图匹配
- acdream 1227 Beloved Sons 二分匹配
- SGU 210. Beloved Sons(二分图匹配)
- ACdream 1227Beloved Sons【二分图最佳匹配】
- ACdream 1227 Beloved Sons 二分图最大权匹配
- SGU 210. Beloved Sons(二分图匹配)
- sgu-210 Beloved Sons
- Beloved Sons(zju1338,KM)
- SGU210——Beloved Sons
- SGU 210 Acdream 1227 Beloved Sons KM
- SGU 210 Beloved Sons (KM)
- ZOJ 4825(二分匹配)
- ZOJ 1516 二分匹配
- ZOJ 3460 二分 + 二分匹配
- 如何制作一款像超级玛丽兄弟一样基于平台的游戏-第一部分 (xcode,物理引擎,TMXTiledMap相关应用)
- uRxvt 字体配置及时生效命令
- android webview 常见问题
- Spring数据源配置
- 各种蹂躏——基础dp
- zoj 2362 Beloved Sons【二分匹配】
- poj 2676 Sudoku
- C++与Java的语法区别
- 如何制作一款像超级玛丽兄弟一样基于平台的游戏-第二部分 (xcode,物理引擎,TMXTiledMap相关应用)
- 人生规划第一步--以此为证,时刻鞭笞自己!
- POJ 2739题解---小菜
- 不刷机让越狱后的iphone恢复出厂设置
- web服务器boa的移植
- sgu 105 Div 3