#HDU 3081 Marriage Match II (网络流 + 二分 + floyed)

Marriage Match II

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


 

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

题目大意 : 有N个女孩和N个男孩匹配, 给出他们的匹配关系, 每个女孩一轮只能和一个男孩匹配, 给出k个女孩的友谊关系, 朋友之间可以互相匹配别人可以匹配的男孩, 每一轮匹配结束后所有女孩都要换个男孩匹配, 输出这个游戏最多可以进行多少轮

思路 : 源点为0, 女孩为1 到 N, 男孩为 N + 1到 2 * N,汇点为 2 * N + 1, 女孩到男孩的流量为1,源点到女孩的流量和男孩到汇点的流量就是要二分的答案。 最好的情况, 所有女孩全是朋友, 全部和男孩有一条边, 答案为N,最坏的情况一轮也进行不了, 所以二分的范围为 0 到 N,  每次重新建边, 如果满流, 说明当前轮数一定可以达到, 往大的二分, 否则往小的二分 ,女孩之间的边可以用传递闭包来解决。 注意判重即可

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & (-x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 10;
const int MAXM = 1e3 + 10;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

struct Edge
{
	int v, w, next;
}e[MAXN << 1];
int head[MAXN],  k, n, m, cnt, T, X;
int dep[MAXN], cur[MAXN], sp, tp;
bool vis[MAXM][MAXM], p[MAXM][MAXM]; // vis判重, p为floyed
void init() {
	MEM(head, -1); MEM(vis, 0); MEM(p, 0); 
	cnt = 0;
}
void add(int from, int to, int w) {
	e[cnt].v = to; e[cnt].w = w;
	e[cnt].next = head[from]; head[from] = cnt++;

	e[cnt].v = from; e[cnt].w = 0;
	e[cnt].next = head[to]; head[to] = cnt++;
}
void floyed() {
	for (int s = 1; s <= 2 * n; s++) {  // 传递闭包
		for (int i = 1; i <= 2 * n; i++) {
			for (int j = 1; j <= 2 * n; j++)
				p[i][j] |= (p[i][s] & p[s][j]);  
		}
	}
}
void build(int K) {
	MEM(vis, 0); MEM(head, -1); cnt = 0;  // 重新建边
	for (int i = 1; i <= n; i++) add(sp, i, K), add(i + n, tp, K);
	for (int i = 1; i <= n; i++) {
		for (int j = n + 1; j <= 2 * n; j++) {
			if (p[i][j] && !vis[i][j]) vis[i][j] = 1, add(i, j, 1);
		}
	}
}
bool bfs() {
	queue <int> q; MEM(dep, 0);
	dep[sp] = 1, q.push(sp);
	while (!q.empty()) {
		int now = q.front();
		q.pop();
		for (int i = head[now]; i != -1; i = e[i].next) {
			int vi = e[i].v;
			if (!dep[vi] && e[i].w) {
				dep[vi] = dep[now] + 1;
				q.push(vi);
			}
		}
	}
	if (dep[tp]) return true;
	else return false;
}
int dfs(int x, int dist) {
	if (x == tp || dist == 0) return dist;
	int res = 0, r;
	for (int &i = cur[x]; i != -1; i = e[i].next) {
		int vi = e[i].v;
		if (dep[vi] == dep[x] + 1 && e[i].w) {
			r = dfs(vi, min(e[i].w, dist - res));
			if (r > 0) {
				e[i].w -= r;
				e[i ^ 1].w += r;
				res += r;
				if (res == dist) return dist;
			}
		}
	}
	if (!res) dep[x] = 0;
	return res;
}
int dinic() {
	int flow = 0, ans;
	while (bfs()) {
		for (int i = sp; i <= tp; i++) cur[i] = head[i];
		flow += dfs(sp, INF);
	}
	return flow;
}

int main()
{
	cin >> T;
	while (T--) {
		sc("%d %d %d", &n, &m, &k);
		init(); sp = 0, tp = 2 * n + 1;
		for (int i = 0; i < m; i++) {
			int ui, vi;
			sc("%d %d", &ui, &vi);
			p[ui][vi + n] = 1;  // 女孩和男孩建边
		}
		for (int i = 0; i < k; i++) {
			int ui, vi; 
			sc("%d %d", &ui, &vi);
			p[ui][vi] = p[vi][ui] = 1; // 传递闭包
		}
		floyed();
		int l = 0, r = n, mid, ans = 0;
		while (l <= r) {
			mid = (l + r) >> 1;
			build(mid);
			int tot = dinic();
			if (tot == mid * n) l = mid + 1, ans = mid;  
			else r = mid - 1;
		}
		printf("%d\n", ans);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值