题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361

来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26760#problem/B

Beloved Sons


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

题意:

就是一个匹配问题。
国王有N个儿子,有 N 个漂亮的女生和他们匹配。
但是不能够满足每个儿子都娶到中意的姑娘,所以要最大限度的让最少的儿子失望。
首先输入多组测试数据。T
然后对于个测试数据,输入 N 表示有几个儿子。
下面一行有 N 个数,表示国王对相应编号儿子的喜爱程度。
【这里有个猫腻,国王肯定是最宠爱他最喜欢的儿子了,所以会先满足最喜欢的儿子】

剩下 N 行,相应代表第一个儿子到第 N个儿子的喜好
每一行第一个数据表示喜换的姑娘的个数 num,后面 num 个数表示喜欢的姑娘的编号

最后让你输出第一个儿子到最后一个儿子娶到的姑娘的编号

注意:王子们对他们喜欢的姑娘们的喜欢程度都是相同的,如果没有娶到喜欢的姑娘则不娶,输出 0

算法:二分匹配


思路:

不能完全套用原来的二分匹配的模板,自己要改一下。
首先要把王子们按照国王对他们的喜欢程度排序【从大到小排序,最喜欢的先选,注意记录王子的编号】
然后建图就是一般的简单的建图,map[i][j] = 0 表示王子 i 不喜欢姑娘 j ; 相应的 map[i][j] = 1就表示喜欢了没有什么好说的了。
再就是按照排序好的顺序依次匹配了。
最后输出的时候注意:match[i] = index; 表示的是姑娘匹配的王子,而题目要求的是输出王子匹配的姑娘编号,自己调整下就好了。

原有的二分匹配模板:【每一个点都要匹配完】
#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【二分匹配】的更多相关文章

  1. zoj 1002 Fire Net (二分匹配)

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  2. ZOJ 3156 Taxi (二分匹配+二分查找)

    题目链接:Taxi Taxi Time Limit: 1 Second      Memory Limit: 32768 KB As we all know, it often rains sudde ...

  3. ZOJ 3646 Matrix Transformer 二分匹配,思路,经典 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4836 因为要使对角线所有元素都是U,所以需要保证每行都有一个不同的列上有U,设 ...

  4. ZOJ 1654 二分匹配基础题

    题意: 给你一副图, 有草地(*),空地(o)和墙(#),空地上可以放机器人, 机器人向上下左右4个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数. 思路:这是一类经 ...

  5. POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24081   Accepted: 106 ...

  6. [kuangbin带你飞]专题十 匹配问题 二分匹配部分

    刚回到家 开了二分匹配专题 手握xyl模板 奋力写写写 终于写完了一群模板题 A hdu1045 对这个图进行 行列的重写 给每个位置赋予新的行列 使不能相互打到的位置 拥有不同的行与列 然后左行右列 ...

  7. BZOJ 1189 二分匹配 || 最大流

    1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1155  Solved: 420[Submi ...

  8. Kingdom of Obsession---hdu5943(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5943 题意:给你两个数n, s 然后让你判断是否存在(s+1, s+2, s+3, ... , s+n ...

  9. poj 2060 Taxi Cab Scheme (二分匹配)

    Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5710   Accepted: 2393 D ...

随机推荐

  1. ElasticSearch测试数据

    curl命令数据 curl -XPUT http://127.0.0.1:9200/us/user/1 -d "{\"email\":\"john@smith. ...

  2. Java 堆内存模型

    堆内存 Java 中的堆是 JVM 所管理的最大的一块内存空间,主要用于存放各种类的实例对象. 在 Java 中.堆被划分成两个不同的区域:新生代 ( Young ).老年代 ( Old ).新生代 ...

  3. 查找文件命令find总结以及查找大文件

    find / -name *** 示例如下: [dinpay@zk-spark-01 spark]$ find /home/ll -name slaves /home/ll/spark/conf/sl ...

  4. EffectiveJava(11)Java中的clone

    java中的clone clone构造器及其静态工厂的变形 优点:它们不依赖于某一种很有风险的,语言之外的对象创建机制; 它们不要求遵守尚未制定好文档的规范 他们不会于final域的正常使用发生冲突 ...

  5. Solidworks草图或者特征无法删除怎么办

      单击重新建模之后即可删除.

  6. vscode - emmet失效?

    把emmet设置覆盖为用户.

  7. HttpURLConnection读取网页文件

    一.关键代码 public class MainActivity extends Activity { TextView content; ; Handler handler = new Handle ...

  8. Php如何使用curl

    cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特性,以及在PHP中如 ...

  9. 【BIEE】05_启动BIEE时,无法启动BI_SERVER

    本地修改资料库后,重新启动BIEE,结果报错: 点击[查看日志信息]后 从这里是没看出是什么问题造成的,那么我们就要去查询启动日志了 问题解决 找到日志路径:D:\obiee\instances\in ...

  10. 文件json

    import jsondef op_data(filename,dic=None): if dic:#写入进去 with open(filename,'w',encoding='utf-8') as ...