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

 

    emmm这道题其实二分+并查集+网络流想出来不是很难。。。但我又忘了这种情况下可能会建重边得!!

 

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 100010;
const int maxm = 1000100;
struct *** {
	int v, w, ne, u;
}ed[maxm];
int vis[300][300];
int n, m, cnt, f, ncnt;
int head[maxn], dis[maxn], cur[maxn], fa[maxn], cop[maxn];
void init() {
	cnt = 0;
	memset(head, -1, sizeof(head));
}
void add(int u, int v, int w) {
	ed[cnt].w = w; ed[cnt].v = v; ed[cnt].u = u;
	ed[cnt].ne = head[u]; head[u] = cnt++;
	ed[cnt].w = 0; ed[cnt].v = u; ed[cnt].u = v;
	ed[cnt].ne = head[v]; head[v] = cnt++;
}
int bfs(int st, int en) {
	queue<int>q;
	memset(dis, 0, sizeof(dis));
	dis[st] = 1;
	q.push(st);
	while (!q.empty()) {
		int u = q.front(); q.pop();
		if (u == en)return 1;
		for (int s = head[u]; ~s; s = ed[s].ne) {
			int v = ed[s].v;
			if (dis[v] == 0 && ed[s].w > 0) {
				dis[v] = dis[u] + 1; q.push(v);
			}
		}
	}
	return dis[en] != 0;
}
int dfs(int st, int en, int flow) {
	int ret = flow, a;
	if (st == en || flow == 0)return flow;
	for (int &s = cur[st]; ~s; s = ed[s].ne) {
		int v = ed[s].v;
		if (dis[v] == dis[st] + 1 && (a = dfs(v, en, min(ret, ed[s].w)))) {
			ed[s].w -= a;
			ed[s ^ 1].w += a;
			ret -= a;
			if (!ret)break;
		}
	}
	if (ret == flow)dis[st] = 0;
	return flow - ret;
}
int dinic(int st, int en,int n ) {
	int ans = 0;
	while (bfs(st, en)) {
		for (int s = 0; s <= n + 1; s++)
			cur[s] = head[s];
		ans += dfs(st, en, inf);
	}
	return ans;
}
int find(int x) {
	if (x != fa[x]) {
	     fa[x] = find(fa[x]);
	}
	return fa[x];
}
void unin(int a, int b) {
	a = find(a);
	b = find(b);
	if(a!=b)
		fa[a] = b;
}
struct link {
	int x, y;
}no[maxm];
void build(int mid,int end)
{
	memset(vis, 0, sizeof(vis));
	init();
	for (int s = 0; s < m; s++) {
		int x = find(no[s].x), y = no[s].y;
		for (int s = 1; s <= n; s++)
			if (find(s) == x&&!vis[s][y]) {
				add(s, y + n, 1); vis[s][y] = 1;
			}
	}
	for (int s = 1; s <= n; s++) {
		add(0, s, mid);
		add(s + n, end, mid);
	}
}
int main() {
	int te;
	scanf("%d", &te);
	while (te--) {
		scanf("%d%d%d", &n, &m, &f);
		for (int s = 0; s <= 2 * n; s++)
			fa[s] = s;
		int end = 2 * n + 1;
		for(int s=0;s<m;s++)
			scanf("%d%d", &no[s].x, &no[s].y);
		for (int s = 0; s < f; s++) {
			int a, b;scanf("%d%d", &a, &b);
			unin(a, b);
		}
		int li = 0, ri = n;
		while (li < ri) {
			int mid = (li + ri+1) >> 1;
			build(mid, end);
			int ans = dinic(0, end, end);
			if (ans == n * mid) {
				li = mid;
			}
			else {
				ri = mid - 1;
			}
		}
		cout << li << endl;
	}
}