2014
03-01

# Marriage Match II

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?

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.

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.

1
4 5 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3

2

HDU 3081

2*N个节点代表所有的人,1~N是女孩子,N+1~2N代表男孩子,每个女孩子到她喜欢和她朋友喜欢的男孩子之间有连线.

#include"stdio.h"
#include"iostream"
#include"string.h"
#include"stdlib.h"
#include"queue"
using namespace std;
int t,m,n,f;
void make_()
{
for(int i=1;i<=n;i++) rank[i]=0,fa[i]=i;
}
int find_(int x)
{
if(fa[x]!=x) fa[x]=find_(fa[x]);
return fa[x];
}
void union_(int x,int y)
{
int fx,fy;
fx=find_(x);
fy=find_(y);
if(fx==fy) return ;
if(rank[fx]>rank[fy])
{
fa[fy]=fx;
}
else
{
fa[fx]=fy;
if(rank[fx]==rank[fy]) rank[fy]++;
}
}
int xiongyali(int i)
{
int j,k;
for(j=1;j<=n;j++)
{
if(!vy[j] && map[i][j])
{
vy[j]=1;
{
return 1;
}
}
}
return 0;
}
void del()
{
int i;
for(i=1;i<=n;i++)
}
int solve()
{
int i,j,k,temp,ans=0;
while(1)
{
temp=0;
for(i=1;i<=n;i++)
{
memset(vy,0,sizeof(vy));
if(xiongyali(i)) temp++;
}
//printf("%d\n",temp);
if(temp==n)
{
ans++;
del();
}
else break;
}
return ans;
}
int main()
{
int i,j,k,l,p;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&f);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=0;
for(i=1;i<=m;i++)
{
scanf("%d%d",&j,&k);
map[j][k]=1;
}
make_();
for(i=1;i<=f;i++)
{
scanf("%d%d",&j,&k);
union_(j,k);
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(j==i) continue;
else
if(find_(i)==find_(j))
for(k=1;k<=n;k++)
if(map[j][k]) map[i][k]=1;
int ans=solve();
printf("%d\n",ans);
}
return 0;
}

#include"stdio.h"
#include"iostream"
#include"string.h"
#include"stdlib.h"
#include"queue"

#define inf 200000000
using namespace std;
queue<int> q;
int t,m,n,f,K,c,N;
struct pp
{
int u,v,flow,next;
}map[200000],mat[200000];
int minn(int x,int y){return x<y?x:y;}

void make_(){ for(int i=0;i<=n;i++) rank[i]=0,fa[i]=i; }
int find_(int x)
{
if(fa[x]!=x) fa[x]=find_(fa[x]);
return fa[x];
}
void union_(int x,int y)
{
int fx,fy;
fx=find_(x);
fy=find_(y);
if(fx==fy) return ;
if(rank[fx]>rank[fy])
{
fa[fy]=fx;
}
else

{
fa[fx]=fy;
if(rank[fx]==rank[fy]) rank[fy]++;
}
}

{
mat[c].u=u;
mat[c].v=v;
mat[c].flow=flow;

mat[c].u=v;
mat[c].v=u;
mat[c].flow=0;
}
void bfs()
{

int i,j,u,v;
for(i=0;i<N;i++) {dis[i]=inf;vis[i]=0;}
dis[N]=0;
vis[N]=1;
q.push(N);
while(!q.empty())
{
u=q.front();
q.pop();
{
v=mat[i].v;
if(!vis[v])
{
dis[v]=dis[u]+1;
vis[v]=1;
q.push(v);
}
}

}
}
int dfs(int u,int flow)
{
int pos,temp,f,i,v;
if(u==N) return flow;
pos=N;
temp=flow;
{
v=map[i].v;
if(d[u]==d[v]+1 && map[i].flow)
{
f=dfs(v,minn(temp,map[i].flow));
temp-=f;

map[i].flow-=f;
map[i^1].flow+=f;
if(temp==0 || d[0]>=N) return flow-temp;
}
if(map[i].flow && d[v]<pos) pos=d[v];
}
if(temp==flow)
{
num[d[u]]--;
if(num[d[u]]==0) d[0]=N;
else
{
d[u]=pos+1;
num[d[u]]++;
}
}
return flow-temp;

}
int SAP()
{
int i,ans=0;
for(i=0;i<=N;i++)
{
d[i]=dis[i];
num[i]=0;
}
while(d[0]<N)
{
ans+=dfs(0,inf);

}
return ans;
}
int main()
{
int i,j,k,l,p,r,mid,max_flow,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&n,&m,&K,&f);
N=3*n+1;
memset(a,0,sizeof(a));
for(i=1;i<=m;i++)
{
scanf("%d%d",&j,&k);

a[j][k]=1;
}
make_();
for(i=1;i<=f;i++)
{
scanf("%d%d",&j,&k);
union_(j,k);
}

for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(j!=i && find_(j)==find_(i))
for(k=1;k<=n;k++)
if(a[j][k]) a[i][k]=1;
c=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
if(a[i][j])
else
}

p=c;
for(i=1;i<=n;i++)
{
}
bfs();
l=0;r=n;
while(l<=r)
{
mid=(l+r)>>1;
for(i=p;i<c;i+=2) mat[i].flow=mid;
for(i=0;i<c;i++) map[i]=mat[i];
max_flow=SAP();
if(max_flow==mid*n) {l=mid+1;ans=mid;}
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}

1. 很高兴你会喜欢这个网站。目前还没有一个开发团队，网站是我一个人在维护，都是用的开源系统，也没有太多需要开发的部分，主要是内容整理。非常感谢你的关注。