Marriage Match II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4550 Accepted Submission(s): 1506
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?
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.
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个女孩,女孩选男孩,每轮游戏都必须选择不同的男孩。每轮游戏中,每个女孩可以选择和自己没有争吵的男孩,也可以选择和自己的好朋友没有争吵过的男孩。问最多可以进行几轮游戏。思路:将0和2*n+1定位超级源点和汇点,1-n为女生,n+1-2*n为男生,二分枚举可以进行的轮数mid,女生和男生建边为1,源点和女生建边为mid,男生和汇点建边为mid,如果最大流等于n*mid,说明能进行mid轮游戏。代码:#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include <cmath> #include <queue> #include <vector> using namespace std; const int maxn=10005; const int maxm=1000000; const int inf=0x3f3f3f3f; int head[maxn]; int Next[maxm]; int depth[maxn]; int p[110]; int g[maxn],b[maxn]; int map[110][110]; int m,n,f; int s,t; int cnt; struct edge { int u,v,w; }e[maxm]; void addedge(int u,int v,int w) { cnt++; Next[cnt]=head[u]; head[u]=cnt; e[cnt].u=u; e[cnt].v=v; e[cnt].w=w; cnt++; Next[cnt]=head[v]; head[v]=cnt; e[cnt].u=v; e[cnt].v=u; e[cnt].w=0; } int findx(int x) { if(x==p[x])return x; return p[x]=findx(p[x]); } void unio(int x,int y) { int xx=findx(x); int yy=findx(y); if(xx>yy)p[xx]=yy; else p[yy]=xx; } int bfs() { queue<int>q; memset(depth,0,sizeof(depth)); depth[s]=1; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=Next[i]) { int v=e[i].v; if(depth[v]==0&&e[i].w>0) { depth[v]=depth[u]+1; q.push(v); } } } if(depth[t]==0) return 0; return 1; } int dfs(int u,int w) { if(u==t)return w; int f=0; for(int i=head[u];i!=-1;i=Next[i]) { int v=e[i].v; if(depth[v]==depth[u]+1&&e[i].w>0) { int d=dfs(v,min(w-f,e[i].w)); e[i].w-=d; e[i^1].w+=d; f+=d; if(f==w)break; } } if(!f)depth[u]=0; return f; } int buildedge(int x) { memset(head,-1,sizeof(head)); memset(map,0,sizeof(map)); cnt=-1; int i; for(i=1;i<=n;i++) { addedge(s,i,x); addedge(i+n,t,x); } for(i=0;i<m;i++) { int u=g[i],v=b[i]; for(int j=1;j<=n;j++) { if(findx(j)==findx(u)) { if(map[j][v]==0) { map[j][v]=1; addedge(j,v+n,1); } } } } } int main() { int T; scanf("%d",&T); while(T--) { int i; scanf("%d%d%d",&n,&m,&f); for(i=0;i<=n;i++) { p[i]=i; } s=0; t=2*n+1; for(i=0;i<m;i++) { scanf("%d%d",&g[i],&b[i]); } int a,b; for(i=0;i<f;i++) { scanf("%d%d",&a,&b); unio(a,b); } int l=0,r=n,mid,ans=0; int sum; while(l<=r) { mid=(l+r)/2; buildedge(mid); sum=0; while(bfs()) { while(int d=dfs(s,inf)) sum+=d; } if(sum==n*mid) { ans=mid; l=mid+1; } else r=mid-1; } printf("%d\n",ans); } return 0; }