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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章