您的位置:首页 > 其它

HDU 3081 Marriage Match II —并查集,最大流

2014-08-04 16:22 274 查看
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081


Marriage Match II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1922 Accepted Submission(s): 679



Problem Description

Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the game of playing house we used to play when we are kids. What a happy time as so many friends playing together. And it is normal that a fight
or a quarrel breaks out, but we will still play together after that, because we are kids.

Now, there are 2n kids, n boys numbered from 1 to n, and n girls numbered from 1 to n. you know, ladies first. So, every girl can choose a boy first, with whom she has not quarreled, to make up a family. Besides, the girl X can also choose boy Z to be her boyfriend
when her friend, girl Y has not quarreled with him. Furthermore, the friendship is mutual, which means a and c are friends provided that a and b are friends and b and c are friend.

Once every girl finds their boyfriends they will start a new round of this game—marriage match. At the end of each round, every girl will start to find a new boyfriend, who she has not chosen before. So the game goes on and on.

Now, here is the question for you, how many rounds can these 2n kids totally play this game?

Input

There are several test cases. First is a integer T, means the number of test cases.

Each test case starts with three integer n, m and f in a line (3<=n<=100,0<m<n*n,0<=f<n). n means there are 2*n children, n girls(number from 1 to n) and n boys(number from 1 to n).

Then m lines follow. Each line contains two numbers a and b, means girl a and boy b had never quarreled with each other.

Then f lines follow. Each line contains two numbers c and d, means girl c and girl d are good friends.

Output

For each case, output a number in one line. The maximal number of Marriage Match the children can play.

Sample Input

1
4 5 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3


Sample Output

2


Author

starvae

题意:有N个妹纸和N个汉纸。给出M组关系,表示妹纸A和汉纸B绝对不会吵架。再给出F组关系,表示妹纸C和妹纸D是好伙伴。如果C D是好伙伴的话,那么不会和C吵架的汉纸也不会和D吵架,同理不会和D吵架的汉纸也不会和C吵架。这个关系可以传递。即ABCD时好伙伴,那么她们其中一个人不与汉纸M吵架,其他人则也不会和M吵架。不吵架的男女就可配对。已知每一轮游戏,都需要每个妹纸分别和一个汉纸配对,而每个妹纸在每轮中的配对对象不能重复,问最多能进行多少轮游戏。游戏不能进行当且仅当有妹纸找不到在之前游戏中没有配对过的汉纸。

解题思路:考察网络流和构图,也用到了并查集。首先妹纸们的传递关系可以用并查集解决。就可以求得每个妹纸可以和哪些汉纸配对。最多可以多少轮用二分。然后就就是构图了。源点向每个妹纸连容量为mid的边,这样就可以确保每个妹子都要参加mid轮,每个汉纸向汇点连容量为mid的边,同样道理。然后每个妹纸向不会和她吵架的每一个汉纸连容量是1的边。跑最大流看是否满流。接下来就是模板了。

AC代码:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: