您的位置:首页 > 其它

并查集+最大流-HDU-3038-Marriage Match II

2016-05-19 20:45 218 查看
Marriage Match II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3447 Accepted Submission(s): 1113

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

题意:

一共有2*n个不检点的少男少女,其中n男n女,春天到了, 又到了 * * * * *,这2n个少男少女要玩一个丧心病狂的游戏,即配对游戏,一男一女算一对,每次他们都会找没有与自己吵过架并且非前男(女)友的人来配对,同时,女生之间总是比较诡异,如果一个男生与一个女生的某个朋友没有吵过架,那么这个男生就可以与这个女生配对(当然,如果是前男友就不行了。)每轮结束他们都会break掉,现在给出女生两两的朋友关系,并且这个朋友关系是可传递的,再给出没有吵过架的男女搭配,问一共能进行多少轮游戏。

题解:

首先女生之间的朋友关系用一个并查集就可以搞定,然后也就得到了每个女生所能够配对的男生,之后再建一个网,从源点引上限为1的边到每个女生,再从男生引上限为1的边到汇点,将男女之间可配对的连边(女->男),然后不断用剩余网络跑最大流,每次如果最大流等于n就继续,并把源点到女生、男生到汇点的边重置,直到最大流不足n。循环次数就是答案了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <utility>
using namespace std;
const int MAXN=250;
int maze[MAXN][MAXN];
int n,m,f;
int father[105];
int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN];
int sap(int start,int end,int nodenum)
{
memset(cur,0,sizeof(cur));
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
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] && dis[u]==dis[v]+1)
{
if(aug==-1 || aug>maze[u][v])aug=maze[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])
{
maze[u][v]-=aug;
maze[v][u]+=aug;
}
aug=-1;
}
goto loop;
}
int mindis=nodenum-1;
for(int v=0; v<nodenum; v++)
if(maze[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;
}
bool can[105][105];
int F(int num)
{
int root,now=num,tmp;
while (father[now]!=now)
now=father[now];
root=now;
now=num;
while (father[now]!=now) {
tmp=father[now];
father[now]=root;
now=tmp;
}
return root;
}
void J(int a,int b)
{
int fa=F(a),fb=F(b);
if(fa!=fb)
father[fa]=father[fb];
}
int main()
{
ios::sync_with_stdio(false);
int t,a,b,out,fi;
cin >> t;
while(t--)
{
out=0;
memset(maze,0,sizeof(maze));
memset(can,0,sizeof(can));
cin >> n >> m >> f;
for(int i=1;i<=n;i++)
father[i]=i;
for(int i=1;i<=m;i++)
{
cin >> a >> b;
can[a][b]=1;
}
for(int i=1;i<=f;i++)
{
cin >> a >> b;
J(a,b);
}
for(int i=1;i<=n;i++)
{
maze[0][i]=1;
maze[i+n][2*n+1]=1;
fi=F(i);
if(fi==i)
continue;
for(int k=1;k<=n;k++)
if(can[i][k])
can[fi][k]=1;
}
for(int i=1;i<=n;i++)
{
int fi=father[i];
for(int j=1;j<=n;j++)
if(can[fi][j])
maze[i][j+n]=1;
}
while(sap(0,2*n+1,2*n+2)==n)
{
for(int i=1;i<=n;i++)
maze[0][i]=maze[i+n][2*n+1]=1;
out++;
}
cout << out << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: