HDU 3081 并查集 二分枚举 最大流

Marriage Match II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3029    Accepted Submission(s): 1002


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
 

Author
starvae
 

Source
 




#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define inf 0x0f0f0f0f
#define LL long long  
using namespace std;



/************************************************

Desiner:hl
time:2015/11/02
Exe.Time:171MS
Exe.Memory:4668K

题意:女生选男生配对 
女生会选择自己心仪的男生或者是她朋友心仪的男生配对(好像有点邪恶)
这里要注意。女生朋友的朋友也是这个女生的朋友(并查集)

题解:并查集找出女生的fa[]
二分枚举最大流 
然后女生和女生的朋友和这个男生连线。流量为1
超级源点和女生连 流量 mid
男生和超级汇点连 流量 mid
************************************************/


using namespace std;  
const int MAXN = 100010 ; //点数最大值    
const int MAXM = 400010 ; //边数最大值    
const int INF = 0x3f3f3f3f;    
  
int S,V,N,M;  
struct Edge{    
    int to,next,cap,flow;    
}edge[MAXM];//注意是MAXM    
int tol;    
int head[MAXN];    
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];    
int aa[55555],bb[55555]; 
int fa[555],imap[555][555];
void floyd(){  
    int i,j,k;  
    for(k=0;k<V;k++){  
        for(i=0;i<V;i++){  
            for(j=0;j<V;j++){  
                imap[i][j]=min(imap[i][j],imap[i][k]+imap[k][j]);  
            }  
        }  
    }  
      
      
/* 
    printf("测试输出\n"); 
    for(i=0;i<V;i++){ 
        for(j=0;j<V;j++){ 
            printf("%d  ",imap[i][j]); 
        } 
        printf("\n"); 
    }*/  
}  


void fainit(){
	for(int i=1;i<=N;i++){
		fa[i] = i;
	}
}

int find(int x)  
{  
    int r=x;  
    while(fa[r]!=r)  
    r=fa[r];  
    fa[x]=r;  
    return fa[x];  
}  
void Union(int x,int y)  
{  
    int xx,yy;  
    xx=find(x);  
    yy=find(y);  
    if(xx!=yy)  
    fa[xx]=yy;  
}


void init(){    
    tol = 0;    
    memset(head,-1,sizeof(head));    
}     
  
void addedge(int u,int v,int w,int rw=0){    
    edge[tol].to = v;    
    edge[tol].cap = w;    
    edge[tol].next = head[u];    
    edge[tol].flow = 0;    
    head[u] = tol++;    
    edge[tol].to = u;    
    edge[tol].cap = rw;    
    edge[tol].next = head[v];    
    edge[tol].flow = 0;    
    head[v] = tol++;    
}    
  //最大流开始   
int sap(int start,int end,int N){    
    memset(gap,0,sizeof(gap));    
    memset(dep,0,sizeof(dep));    
    memcpy(cur,head,sizeof(head));    
    int u = start;    
    pre[u] = -1;    
    gap[0] = N;    
    int ans = 0;    
    while(dep[start] < N){    
        if(u==end){    
            int Min = INF;    
            for(int i=pre[u];i!= -1; i=pre[edge[i^1].to])    
                if(Min > edge[i].cap - edge[i].flow)    
                    Min = edge[i].cap - edge[i].flow;    
                        
            for(int i=pre[u];i!=-1;i=pre[edge[i^1].to]){    
                edge[i].flow += Min;    
                edge[i^1].flow -=Min;    
            }    
            u=start;    
            ans +=Min;    
            continue;    
        }    
        bool flag = false;    
        int v;    
        for(int i= cur[u];i!=-1;i=edge[i].next){    
            v=edge[i].to;    
            if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){    
                flag=true;    
                cur[u]=pre[v]=i;    
                break;    
            }    
        }    
        if(flag){    
            u=v;    
            continue;    
        }    
        int Min = N;    
        for(int i=head[u];i!= -1;i=edge[i].next)    
            if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min){    
                Min=dep[edge[i].to];    
                cur[u] = i;    
            }    
        gap[dep[u]]--;    
        if(!gap[dep[u]]) return ans;    
        dep[u] = Min +1;    
        gap[dep[u]]++;    
        if(u!=start) u = edge[pre[u]^1].to;    
    }    
    return ans;    
}    
//最大流结束   
  
bool solve(int mid){  
    init();
	memset(imap,0,sizeof(imap));  
    int i,j,k;   
    //超级汇点   
    for(i=1;i<=N;i++){  
        addedge(S,i,mid);    //源点到女生  流量mid 
        addedge(i+N,V,mid);	 //男生到汇点  流量mid 
    } 
    
	for(i=1;i<=M;i++){
		int a=aa[i];
		int b=bb[i];
		for(j=1;j<=N;j++){
			if(fa[a]==fa[j]&&imap[j][b]==0){    //女孩和女孩朋友连接到男生的一条线 流量1 
				imap[j][b]=1;
				addedge(j,b+N,1);
			}
		}
	}
	
    int ans=sap(S,V,V+1); 
	//printf("mid=  %d   ans=  %d\n",mid,ans); 
    return ans==N*mid;       //如果最大匹配场数为mid 那么有N*mid 的流量 
}  
  
int main(){    
    int f,q,p;    
    int i,j,k,a,b,c,T;    
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d",&N,&M,&f);
		fainit();
		for(i=1;i<=M;i++){
			scanf("%d%d",&aa[i],&bb[i]);
		}
		for(i=1;i<=f;i++){
			scanf("%d%d",&a,&b);
			Union(a,b);
		}
		for(i=1;i<=N;i++)	fa[i]=find(i);
		
		//测试输出 并查集 

		//for(i=1;i<=N;i++){
		//	printf("%d ",fa[i]);
		//}
		//puts("");
		
		S=0;
		V=2*N+1;
		int l,r,mid,ans;
		l=ans=0;
		r=N;
		//二分枚举最大匹配场数 
		while(r>=l){
			mid=(l+r)>>1;
			if(solve(mid)){
				l=mid+1;
				ans=mid;
			}
			else{
				r=mid-1;
			}
		}
		printf("%d\n",ans);
		
		
		
		
	
	}
	return 0;
}    

发布了80 篇原创文章 · 获赞 0 · 访问量 2万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章