1. 程式人生 > >並查集+最大流-HDU-3038-Marriage Match II

並查集+最大流-HDU-3038-Marriage Match II

Marriage Match II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3447 Accepted Submission(s): 1113

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

題意:
一共有2*n個不檢點的少男少女,其中n男n女,春天到了, 又到了 * * * * *,這2n個少男少女要玩一個喪心病狂的遊戲,即配對遊戲,一男一女算一對,每次他們都會找沒有與自己吵過架並且非前男(女)友的人來配對,同時,女生之間總是比較詭異,如果一個男生與一個女生的某個朋友沒有吵過架,那麼這個男生就可以與這個女生配對(當然,如果是前男友就不行了。)每輪結束他們都會break掉,現在給出女生兩兩的朋友關係,並且這個朋友關係是可傳遞的,再給出沒有吵過架的男女搭配,問一共能進行多少輪遊戲。

題解:
首先女生之間的朋友關係用一個並查集就可以搞定,然後也就得到了每個女生所能夠配對的男生,之後再建一個網,從源點引上限為1的邊到每個女生,再從男生引上限為1的邊到匯點,將男女之間可配對的連邊(女->男),然後不斷用剩餘網路跑最大流,每次如果最大流等於n就繼續,並把源點到女生、男生到匯點的邊重置,直到最大流不足n。迴圈次數就是答案了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <utility>
using namespace std;
const int MAXN=250;
int maze[MAXN][MAXN];
int n,m,f;
int father[105];
int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN];
int sap(int start,int end,int nodenum)
{
    memset(cur,0,sizeof(cur));
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    int u=pre[start]=start,maxflow=0,aug=-1;
    gap[0]=nodenum;
    while(dis[start]<nodenum)
    {
loop:
        for(int v=cur[u]; v<nodenum; v++)
            if(maze[u][v] && dis[u]==dis[v]+1)
            {
                if(aug==-1 || aug>maze[u][v])aug=maze[u][v];
                pre[v]=u;
                u=cur[u]=v;
                if(v==end)
                {
                    maxflow+=aug;
                    for(u=pre[u]; v!=start; v=u,u=pre[u])
                    {
                        maze[u][v]-=aug;
                        maze[v][u]+=aug;
                    }
                    aug=-1;
                }
                goto loop;
            }
        int mindis=nodenum-1;
        for(int v=0; v<nodenum; v++)
            if(maze[u][v]&&mindis>dis[v])
            {
                cur[u]=v;
                mindis=dis[v];
            }
        if((--gap[dis[u]])==0)break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return maxflow;
}
bool can[105][105];
int F(int num)
{
    int root,now=num,tmp;
    while (father[now]!=now)
        now=father[now];
    root=now;
    now=num;
    while (father[now]!=now) {
        tmp=father[now];
        father[now]=root;
        now=tmp;
    }
    return root;
}
void J(int a,int b)
{
    int fa=F(a),fb=F(b);
    if(fa!=fb)
        father[fa]=father[fb];
}
int main()
{
    ios::sync_with_stdio(false);
    int t,a,b,out,fi;
    cin >> t;
    while(t--)
    {
        out=0;
        memset(maze,0,sizeof(maze));
        memset(can,0,sizeof(can));
        cin >> n >> m >> f;
        for(int i=1;i<=n;i++)
            father[i]=i;
        for(int i=1;i<=m;i++)
        {
            cin >> a >> b;
            can[a][b]=1;
        }
        for(int i=1;i<=f;i++)
        {
            cin >> a >> b;
            J(a,b);
        }
        for(int i=1;i<=n;i++)
        {
            maze[0][i]=1;
            maze[i+n][2*n+1]=1;
            fi=F(i);
            if(fi==i)
                continue;
            for(int k=1;k<=n;k++)
                if(can[i][k])
                    can[fi][k]=1;
        }
        for(int i=1;i<=n;i++)
        {
            int fi=father[i];
            for(int j=1;j<=n;j++)
                if(can[fi][j])
                    maze[i][j+n]=1;
        }
        while(sap(0,2*n+1,2*n+2)==n)
        {
            for(int i=1;i<=n;i++)
                maze[0][i]=maze[i+n][2*n+1]=1;
            out++;
        }
        cout << out << endl;
    }
    return 0;
}