Marriage Match II

题目

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.

样例

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

Sample Output

2

这道题就是最大流加上二分

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string.h>
#include <string>
typedef long long ll;
#define INF 0x3f3f3f3f
using namespace std;
#define MAXN 220


int maze[MAXN][MAXN];
int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
int flow[MAXN][MAXN];
int sap(int start, int end, int nodenum){
	memset(cur, 0, sizeof(cur));
	memset(dis, 0, sizeof(dis));
	memset(gap, 0, sizeof(gap));
	memset(flow, 0, sizeof(flow));
	int u = pre[start] = start, maxflow = 0, aug = -1;
	gap[0] = nodenum;
	while (dis[start] < nodenum){
	loop:
		for (int v = cur[u]; v < nodenum; v++)
			if (maze[u][v] - flow[u][v] && dis[u] == dis[v] + 1)	{
				if (aug == -1 || aug > maze[u][v] - flow[u][v])aug = maze[u][v] - flow[u][v];
				pre[v] = u;
				u = cur[u] = v;
				if (v == end){
					maxflow += aug;
					for (u = pre[u]; v != start; v = u, u = pre[u])	{
						flow[u][v] += aug;
						flow[v][u] -= aug;
					}
					aug = -1;
				}
				goto loop;
			}
		int mindis = nodenum - 1;
		for (int v = 0; v < nodenum; v++)
			if (maze[u][v] - flow[u][v] && mindis > dis[v]){
				cur[u] = v;
				mindis = dis[v];
			}
		if ((--gap[dis[u]]) == 0)break;
		gap[dis[u] = mindis + 1]++;
		u = pre[u];
	}
	return maxflow;
}

int F[110];
int find(int x){
	if (F[x] == -1)
		return x;
	else 
		return F[x] = find(F[x]);
}
void bing(int x, int y){
	int t1 = find(x);
	int t2 = find(y);
	if (t1 != t2)
		F[t1] = t2;
}
int n, m, f;
int a[10010], b[10010];
bool check(int t){
	for (int i = 1; i <= n; i++)
		maze[0][i] = t;
	for (int i = n + 1; i <= 2 * n; i++)
		maze[i][2 * n + 1] = t;
	if (sap(0, 2 * n + 1, 2 * n + 2) == t * n)
		return true;
	else 
		return false;
}
void solve(){
	memset(maze, 0, sizeof(maze));
	for (int i = 0; i < m; i++)
		for (int j = 1; j <= n; j++){
			if (find(j) == find(a[i]))
				maze[j][b[i] + n] = 1;
		}
	int l = 0, r = n;
	int ans = 0;
	while (l <= r){
		int mid = (l + r) / 2;
		if (check(mid))
		{
			ans = mid;
			l = mid + 1;
		}
		else 
			r = mid - 1;
	}
	printf("%d\n", ans);
}

int main(){
	int T;
	scanf("%d", &T);
	while (T--){
		scanf("%d%d%d", &n, &m, &f);
		memset(F, -1, sizeof(F));
		for (int i = 0; i < m; i++)
			scanf("%d%d", &a[i], &b[i]);
		int u, v;
		while (f--){
			scanf("%d%d", &u, &v);
			bing(u, v);
		}
		solve();
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值