Marriage Match III

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1581    Accepted Submission(s): 464

Problem Description
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the ever game of play-house . What a happy time as so many friends play 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. As 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. On the other hand, in order to play more times of marriage match, every girl can accept any K boys. If a girl chooses a boy, the boy must accept her unconditionally whether they had quarreled before or not.

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 an integer T, means the number of test cases.
Each test case starts with three integer n, m, K and f in a line (3<=n<=250, 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 1 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3

Sample Output
3

Author
starvae

Source

这题和HDU3081 非常类似。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define captype int

const int MAXN = 100010;   //点的总数
const int MAXM = 4000100;    //边的总数
const int INF = 1<<30;
struct EDG{
int to,next;
captype cap,flow;
}edg[MAXM];
int gap[MAXN];
int dis[MAXN];
int cur[MAXN];
int pre[MAXN];

void init(){
eid=0;
}
void addEdg(int u,int v,captype c,captype rc=0){

}
captype maxFlow_sap(int s,int t,int n){
memset(gap,0,sizeof(gap));
memset(dis,0,sizeof(dis));
pre[s]=-1;
gap[0]=n;

captype ans=0;
int u=s;
while(dis[s]<n){
if(u==t){
captype mint=INF;
int id;
for(int i=pre[u]; i!=-1; i=pre[edg[i^1].to])
if(mint>edg[i].cap-edg[i].flow){
mint=edg[i].cap-edg[i].flow;
id=i;
}
for(int i=pre[u]; i!=-1; i=pre[edg[i^1].to]){
edg[i].flow+=mint;
edg[i^1].flow-=mint;
}
ans+=mint;
u=edg[id^1].to;
continue;
}
bool flag=0;
for(int i=cur[u]; i!=-1; i=edg[i].next)
if(edg[i].cap>edg[i].flow&&dis[u]==dis[edg[i].to]+1){
cur[u]=pre[edg[i].to]=i;
flag=true;
break;
}
if(flag){
u=edg[cur[u]].to;
continue;
}
int minh=n;
if(edg[i].cap>edg[i].flow && minh>dis[edg[i].to]){
cur[u]=i; minh=dis[edg[i].to];
}
gap[dis[u]]--;
if(!gap[dis[u]]) return ans;
dis[u]=minh+1;
gap[dis[u]]++;
if(u!=s)
u=edg[pre[u]^1].to;
}
return ans;
}

int fath[MAXN];
int findroot(int x){
if(x!=fath[x])
fath[x]=findroot(fath[x]);
return fath[x];
}
void setroot(int x,int y){
x=findroot(x);
y=findroot(y);
fath[x]=y;
}
void rebuildMap(int mapt[255][255],int n){//处理朋友之间的关系
int mp[255][255]={0};
for(int i=1; i<=n; i++)
fath[i]=findroot(i);
for(int i=1; i<=n; i++){
int j=fath[i];
for(int e=1; e<=n; e++)
mp[j][e]|=mapt[i][e];
}
for(int i=1; i<=n; i++){
int j=fath[i];
for(int e=1; e<=n; e++)
mapt[i][e]=mp[j][e];
}
}
int main()
{
int T,n,m,k,f,mapt[255][255];
int u,v;
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d",&n,&m,&k,&f);

init();
memset(mapt,0,sizeof(mapt));
for(int i=1; i<=n; i++)
fath[i]=i;

while(m--){
scanf("%d%d",&u,&v);
mapt[u][v]=1;
}
while(f--){
scanf("%d%d",&u,&v);
setroot(u,v);
}
rebuildMap(mapt,n);

int s=0, t=3*n+1;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++)
if(mapt[i][j])
else

}

int ans=0 , l=0 , r=n ,mid;
while(l<=r){
mid=(l+r)>>1;

for(int i=0; i<eid; i++)
edg[i].flow=0;
edg[i].cap=mid;
edg[i^1].cap=mid;

if(n*mid==maxFlow_sap(s,t,t+1))
ans=mid,l=mid+1;
else
r=mid-1;
}

printf("%d\n",ans);
}
}


posted on 2017-08-20 18:19  yutingliuyl  阅读(228)  评论(0编辑  收藏  举报