HDU 3081 Marriage Match II

  • 2018-08-23
  • 26
  • 0

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<nn,0<=f<n). n means there are 2n 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

题目链接

N个女生和N个男生进行匹配,每个女生有其对应有好感的男生,若两个女生是好朋友的话他们那么对对方有好感的男生也有好感,每轮匹配女生选择的男生不能出现重复,求最多可以进行多少轮匹配。

存储女生和男生的关系网,在读取女生之间关系数据时对女生利用并查集进行合并,之后根据并查集完善女生和男生的关系网,然后二分答案,利用二分的答案进行建图跑最大流。

建图时把源点与女生相连,流量为二分值,再把男生与汇点相连,流量也为二分值,之后利用男生与女生的关系网在有好感的男生女生之间建立一条流量为1的边,根据最大流结果与二分值*N的关系更新二分边界。

若当前二分值可以满足匹配轮数的话源点和汇点所连接的所有边全部满流,最大流结果就是二分值*N,当前二分值还可以再增大,若不满足则最大流结果不等于二分值*N,缩小二分右边界(二分值)。

AC代码:

评论

还没有任何评论,你来说两句吧