Marriage Match II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3486 Accepted Submission(s): 1129
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
Author
starvae
n个男生n个女生,先给出m对女生喜欢男生的关系,然后是L对女生之间的朋友关系,然后进行配对,配对的时候女生可以选择自己喜欢的男生,也可以选择自己朋友喜欢的男生,问有多少种完美匹配,完美匹配跟最大流差不多,都是找增广路
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 2000
#define INF 0x3f3f3f3f
int map[MAXN][MAXN],pipei[MAXN],used[MAXN],pre[MAXN];
int n,m,l;
int find(int x)
{
return pre[x]==x?pre[x]:(pre[x]=find(pre[x]));
}
int dfs(int x)
{
for(int i=1;i<=n;i++)
{
if(map[x][i]&&used[i]==0)
{
used[i]=1;
if(pipei[i]==-1||dfs(pipei[i]))
{
pipei[i]=x;
return 1;
}
}
}
return 0;
}
int cout()
{
int ans=0;
memset(pipei,-1,sizeof(pipei));
for(int i=1;i<=n;i++)
{
memset(used,0,sizeof(used));
ans+=dfs(i);
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&l);
for(int i=0;i<=n;i++)
pre[i]=i;
memset(map,0,sizeof(map));
int x,y;
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
map[x][y]=1;
}
for(int i=0;i<l;i++)
{
scanf("%d%d",&x,&y);
int fx=find(x);
int fy=find(y);
pre[fx]=fy;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i!=j&&find(i)==find(j))
{
for(int k=1;k<=n;k++)
{
if(map[i][k])
map[j][k]=1;
}
}
}
}
int ans=0;
while(true)
{
if(cout()==n)
{
ans++;
for(int i=1;i<=n;i++)
{
map[pipei[i]][i]=0;
}
}
else break;
}
printf("%d\n",ans);
}
return 0;
}
二分+最大流
<pre class="cpp" name="code">#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 2000
#define MAXM 400000
#define INF 0x3f3f3f3f
struct node
{
int u,v,cap,flow,next;
}edge[MAXM];
int head[MAXN],dis[MAXN],cur[MAXN],map[MAXN][MAXN],pre[MAXN],vis[MAXN];
int n,m,l,cnt;
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
int find(int x)
{
return pre[x]==x?pre[x]:(pre[x]=find(pre[x]));
}
void add(int u,int v,int w)
{
node E={u,v,w,0,head[u]};
edge[cnt]=E;
head[u]=cnt++;
node E1={v,u,0,0,head[v]};
edge[cnt]=E1;
head[v]=cnt++;
}
void getmap(int x)
{
for(int i=1;i<=n;i++)
{
add(0,i,x);
add(i+n,2*n+1,x);
for(int j=1;j<=n;j++)
{
if(map[i][j])
add(i,j+n,1);
}
}
}
bool bfs(int s,int t)
{
memset(dis,-1,sizeof(dis));
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(s);
vis[s]=1;
dis[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(E.cap>E.flow&&!vis[E.v])
{
dis[E.v]=dis[E.u]+1;
if(E.v==t) return true;
vis[E.v]=true;
q.push(E.v);
}
}
}
return false;
}
int DFS(int x,int a,int e)
{
if(x==e||a==0) return a;
int flow=0,f;
for(int &i=cur[x];i!=-1;i=edge[i].next)
{
node &E=edge[i];
if(dis[E.v]==dis[x]+1&&(f=DFS(E.v,min(E.cap-E.flow,a),e))>0)
{
flow+=f;
a-=f;
edge[i].flow+=f;
edge[i^1].flow-=f;
if(a==0) break;
}
}
return flow;
}
int MAXflow(int s,int t)
{
int flow=0;
while(bfs(s,t))
{
memcpy(cur,head,sizeof(cur));
flow+=DFS(s,INF,t);
}
return flow;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&l);
memset(map,0,sizeof(map));
for(int i=0;i<=n;i++)
{
pre[i]=i;
}
int x,y;
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
map[x][y]=1;
}
for(int i=0;i<l;i++)
{
scanf("%d%d",&x,&y);
int fx=find(x);
int fy=find(y);
pre[fx]=fy;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i!=j&&find(i)==find(j))
{
for(int k=1;k<=n;k++)
{
if(map[i][k])
map[j][k]=1;
}
}
}
}
int ans=0,L=0,R=n;
while(L<=R)
{
init();
cnt=0;
int mid=(L+R)/2;
getmap(mid);
if(MAXflow(0,2*n+1)==n*mid)
{
ans=mid;
L=mid+1;
}
else
R=mid-1;
}
printf("%d\n",ans);
}
return 0;
}