Marriage Match II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5882 Accepted Submission(s): 1872

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<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


简单的稳定婚姻匹配问题,利用并查集+二分图 或者 传递闭包+二分图(想用最大流增加难度也可以) 即可完成。
数据很小,并不需要二分。


这里我采用了 并查集+二分图 的做法。

先根据题目的输入,把女孩和可以结婚的男孩连起来;再使用并查集将是朋友的女孩,相互连起来;在对于每一个男孩,如果这个女孩可以与这个男孩结婚,再把与这个女孩是朋友的女孩与男孩相连。这样跑二分图匹配,只要匹配不上了,就退出循环。
如果能匹配则匹配完,答案加一,再把这次的女孩与男孩的匹配标记,下次就不能匹配了。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=110;
int T,n,m,k,g[N][N],f[N],boy[N],res,flag,vis[N];
int find(int x){
	return x==f[x]?x:f[x]=find(f[x]);
}
bool dfs(int x,int id){
	for(int i=1;i<=n;i++){
		if(vis[i]!=id&&g[x][i]){
			vis[i]=id;
			if(!boy[i]||dfs(boy[i],id)){
				boy[i]=x;	return true;
			}
		}
	}
	return false;
}
signed main(){
	cin>>T;
	while(T--){
		cin>>n>>m>>k;
		res=flag=0;	memset(g,0,sizeof g);
		for(int i=1;i<=n;i++)	f[i]=i;
		while(m--){
			int a,b;	cin>>a>>b;	g[a][b]=1;
		}
		while(k--){
			int c,d;	cin>>c>>d;	f[find(c)]=find(d);
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(g[j][i]){
					for(int k=1;k<=n;k++){
						if(find(k)==find(j))	g[k][i]=1;
					}
				}
			}
		}
		while(!flag){
			memset(boy,0,sizeof boy);	memset(vis,0,sizeof vis);
			for(int i=1;i<=n;i++){
				if(!dfs(i,i))	flag=1;
			}
			if(!flag)	res++;
			for(int i=1;i<=n;i++){
				g[boy[i]][i]=0;
			}
		}
		cout<<res<<endl;
	}
	return 0;
}