zoj 2362 Beloved Sons【二分匹配】
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361
来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26760#problem/B
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
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);
}
}
B | Accepted | 844 KB | 260 ms | C++ (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 1002 Fire Net (二分匹配)
Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we have a square city with s ...
- ZOJ 3156 Taxi (二分匹配+二分查找)
题目链接:Taxi Taxi Time Limit: 1 Second Memory Limit: 32768 KB As we all know, it often rains sudde ...
- ZOJ 3646 Matrix Transformer 二分匹配,思路,经典 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4836 因为要使对角线所有元素都是U,所以需要保证每行都有一个不同的列上有U,设 ...
- ZOJ 1654 二分匹配基础题
题意: 给你一副图, 有草地(*),空地(o)和墙(#),空地上可以放机器人, 机器人向上下左右4个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数. 思路:这是一类经 ...
- POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24081 Accepted: 106 ...
- [kuangbin带你飞]专题十 匹配问题 二分匹配部分
刚回到家 开了二分匹配专题 手握xyl模板 奋力写写写 终于写完了一群模板题 A hdu1045 对这个图进行 行列的重写 给每个位置赋予新的行列 使不能相互打到的位置 拥有不同的行与列 然后左行右列 ...
- BZOJ 1189 二分匹配 || 最大流
1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1155 Solved: 420[Submi ...
- Kingdom of Obsession---hdu5943(二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5943 题意:给你两个数n, s 然后让你判断是否存在(s+1, s+2, s+3, ... , s+n ...
- poj 2060 Taxi Cab Scheme (二分匹配)
Taxi Cab Scheme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5710 Accepted: 2393 D ...
随机推荐
- P6 EPPM 16 R1安装和配置文档
白桃花心木P6企业项目组合管理文档库 描述 链接 下载 零件号 16 R1用户和集成文档 查看库 下载 E68199-01 16 R1安装和配置文档 查看库 下载 E68198-01 描述 链接 ...
- 通过Fsharp探索Enterprise Library Exception
Exception怎么生成是一回事,怎么展示又是还有一回事了. Exception Block主要关注的点在于Exception信息的展示.Exception不同于一般的log信息,是系统设计者未考虑 ...
- 关于ionic中几个问题
第一.每个页面的独立样式style标签不能写在ion-view外面,否则会出现路由问题,建议写在ion-content后面,例如下面的例子中,如果style但在ion-view中的话会出想路由问题,显 ...
- jmap命令(Java Memory Map)的使用
jmap的使用能够參考: 官方文档 http://docs.oracle.com/javase/6/docs/technotes/tools/share/jmap.html 和这篇博客 http:// ...
- lodash 展平数组 flatten flattenDeep
_.flatten(array) 向上一级展平数组嵌套 <!DOCTYPE html> <html lang="zh"> <head> < ...
- HDU 3732 Ahui Writes Word(多重背包)
HDU 3732 Ahui Writes Word(多重背包) http://acm.hdu.edu.cn/showproblem.php? pid=3732 题意: 初始有N个物品, 每一个物品有c ...
- 去除inline-block元素间间距的N种方法-zhangxinxu
张鑫旭原文:点这里进入原文 另外附上大漠老师的如何解决inline-block元素的空白间距地址!!! 去除inline-block元素间间距的N种方法: 一.现象描述 真正意义上的inline-bl ...
- java web 通配符* ? $1 $2 $3
匹配通配符 * 匹配0-n个字符,但不包括“/”.即,“*”只匹配一级目录或文件中的零个或多个字符. ** 匹配0-n个字符,包括“/”.即,“**”匹配多级目录或文件. ? 匹配0-1个字符,但不包 ...
- hdu 4601 Letter Tree
不easy啊.. 一个小错误让我wa死了.找了一个晚上,怎么都找不到 最后是对拍代码找到的错误.发现当步数比較小的时候答案就是对的,比較大的时候就不正确了 想到一定是什么地方越界了.. . power ...
- MFC错误集锦
MFC中相关报错及其解决的方法: (1)0x00000005: 解决的方法:看是哪里的 数组越界: (2)0xCCCCCCCC:在类中声明指针,但没有赋初值之类的错误. 解决的方法:在类的构造函数中给 ...