[dinic最大流 + 并查集 + 二分] N - Marriage Match II HDU - 3081

Marriage Match II

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


 

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

 

Source

HDU 2nd “Vegetable-Birds Cup” Programming Open Contest

#include <bits/stdc++.h>
using namespace std;
 
const int inf = 0x7fffffff;
const int mn = 300, mm = 30000;

int lian[110][110];
int p[110];
void init();
int max_flow(int, int);
void addedge(int, int, int);

int par(int x)
{
	if (p[x] == x)
		return x;
	return
		p[x] = par(p[x]);
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("D:\\in.txt", "r", stdin);
	#endif
 
	int T;
	scanf("%d", &T);
	int st = 0, ed = 250;
	while (T--)
	{
		memset(lian, 0, sizeof lian);
		
		int n, m, f;
		scanf("%d %d %d", &n, &m, &f);
		
		for (int i = 1; i <= m; i++)
		{
			int a, b;
			scanf("%d %d", &a, &b);
			lian[a][b] = 1;		// a, b可匹配
		}
		
		for (int i = 1; i <= n; i++)  // 初始化i的父节点
			p[i] = i;
		for (int i = 1; i <= f; i++)  // a, b 是朋友
		{
			int a, b;
			scanf("%d %d", &a, &b);
			int x = par(a), y = par(b);
			if (x != y)
				p[x] = y;  // 合并a, b所在集合
		}
		
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				if (par(i) == par(j))  // i, j同源
				{
					for (int k = 1; k <= n; k++)
						if (lian[j][k])  // j连的i也可以连
							lian[i][k] = 1;
				}
			}
		}
		
		int ans = 0;
		int l = 0, r = 100;
		while (l <= r)  /// 二分查找可能的ans的值
		{	
			init();  /// 重新建图跑最大流
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					if(lian[i][j])
					addedge(i, 100 + j, 1);
			
			int mid = (l + r) / 2;
			
			for (int i = 1; i <= n; i++) 	// 女孩和源点相连
				addedge(st, i, mid);
			for (int i = 1; i <= n; i++) 	// 男孩和汇点相连
				addedge(100 + i, ed, mid);
			
			if (max_flow(st, ed) == mid * n)  // 每个人可进行mid轮1对1匹配
			{
				ans = mid;
				l = mid + 1;
			}
			else
				r = mid - 1;
		}
		
		printf("%d\n", ans);
	}
	return 0;
}


int edge;
int fr[mn];
int lv[mn];
int cur[mn];
struct node
{
	int to, val, nx, fan;
} e[mm];
 
void init()
{
	edge = 0;
	memset(fr, -1, sizeof fr);
}
 
void addedge(int u, int v, int w)
{
	edge++;
	e[edge].to = v, e[edge].val = w, e[edge].nx = fr[u], e[edge].fan = edge + 1;
	fr[u] = edge;
	edge++;
	e[edge].to = u, e[edge].val = 0, e[edge].nx = fr[v], e[edge].fan = edge - 1;
	fr[v] = edge;
}
 
void bfs(int s)
{
	memset(lv, 0, sizeof lv);
	lv[s] = 1;
	queue<int>q;
	q.push(s);
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		for (int i = fr[t]; i != -1; i = e[i].nx)
		{
			if (e[i].val > 0 && !lv[e[i].to])
			{
				lv[e[i].to] = lv[t] + 1;
				q.push(e[i].to);
			}
		}
	}
}
 
int dfs(int s, int t, int f)
{
	if (s == t)
		return f;
    for (int &i = cur[s]; i != -1; i = e[i].nx)  /// 当前弧优化 否则TLE 
	{
		if (e[i].val > 0 && lv[e[i].to] > lv[s])
		{
			int d = dfs(e[i].to, t, min(f, e[i].val));
			if (d > 0)
			{
				e[i].val -= d;
				e[e[i].fan].val += d;
				return d;
			}
		}
	}
	return 0;
}
 
int max_flow(int s, int t)
{
	int flow = 0;
	while (1)
	{
		bfs(s);
		if (!lv[t])
			break;
			
		for (int i = 0; i < mn; i++)
			cur[i] = fr[i];
		
		int f = -1;
        while ((f = dfs(s, t, inf)) > 0)
			flow += f;
	}
	return flow;
}

猜你喜欢

转载自blog.csdn.net/ummmmm/article/details/81414399
今日推荐