hdu3081二分并查集最大流

Marriage Match II

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


 

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

HDU 2nd “Vegetable-Birds Cup” Programming Open Contest

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  3416 1532 1533 3277 3376 

 

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
#define INF 1e9
using namespace std;
const int maxn= 2020+5;

struct Edge
{
    int from,to,cap,flow;
    Edge(){}
    Edge(int f,int t,int c,int flow):from(f),to(t),cap(c),flow(flow){}
};

struct Dinic
{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> G[maxn];
    int d[maxn];
    bool vis[maxn];
    int cur[maxn];

    void init(int n,int s,int t)
    {
        this->n=n, this->s=s, this->t=t;
        edges.clear();
        for(int i=0;i<n;i++) G[i].clear();
    }

    void addedge(int from,int to,int cap)
    {
        edges.push_back( Edge(from,to,cap,0) );
        edges.push_back( Edge(to,from,0,0) );
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BFS()
    {
        queue<int> Q;
        memset(vis,0,sizeof(vis));
        vis[s]=true;
        d[s]=0;
        Q.push(s);
        while(!Q.empty())
        {
            int x= Q.front(); Q.pop();
            for(int i=0;i<G[x].size();++i)
            {
                Edge& e=edges[G[x][i]];
                if(!vis[e.to] && e.cap>e.flow)
                {
                    vis[e.to]=true;
                    d[e.to]=d[x]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x,int a)
    {
        if(x==t || a==0) return a;
        int flow=0,f;
        for(int& i=cur[x]; i<G[x].size();++i)
        {
            Edge& e=edges[G[x][i]];
            if(d[e.to]==d[x]+1 && (f=DFS(e.to,min(a,e.cap-e.flow) ) )>0)
            {
                e.flow +=f;
                edges[G[x][i]^1].flow -=f;
                flow +=f;
                a-=f;
                if(a==0) break;
            }
        }
        return flow;
    }

    int maxflow()
    {
        int ans=0;
        while(BFS())
        {
            memset(cur,0,sizeof(cur));
            ans +=DFS(s,INF);
        }
        return ans;
    }
}DC;
 int father[maxn];
 int find(int x)
 {
     if(father[x]!=x)
        father[x]=find(father[x]);
     return father[x];
 }
 void unionn(int x,int y)
 {
     int fa=find(x);
     int fb=find(y);
     if(fa!=fb)
        father[fa]=fb;
 }
 bool dis[maxn][maxn];
 bool solve(int n,int limit)
 {
     int src=0,dst=2*n+1;
     DC.init(n*2+2,src,dst);
     for(int i=1;i<=n;i++)
        for(int j=n+1;j<=2*n;j++)
        if(dis[i][j])
        DC.addedge(i,j,1);
     for(int i=1;i<=n;i++)
        DC.addedge(src,i,limit);
     for(int j=n+1;j<=2*n;j++)
        DC.addedge(j,dst,limit);
     return DC.maxflow()==n*limit;
 }
 int main()
 {
     int t;
     scanf("%d",&t);
     while(t--)
     {
         int n,m,f;
         scanf("%d%d%d",&n,&m,&f);
         for(int i=0;i<=n;i++)
            father[i]=i;
         memset(dis,0,sizeof(dis));
         while(m--)
         {
             int u,v;
             scanf("%d%d",&u,&v);
             dis[u][v+n]=true;
         }
         while(f--)
         {int u,v;
         scanf("%d%d",&u,&v);

             unionn(u,v);
         }
         for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
            if(find(i)==find(j))
            for(int k=n+1;k<=2*n;k++)
            dis[i][k]=dis[j][k]=(dis[i][k]||dis[j][k]);
         int l=0,r=100;
         while(l<r)
         {
             int mid=l+(r-l+1)/2;
             if(solve(n,mid))
                l=mid;
             else
                r=mid-1;
         }
         printf("%d\n",l);
     }
     return 0;
 }

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/85175531
今日推荐