Marriage Match II HDU - 3081 (二分答案+最大流+并查集)

16 篇文章 1 订阅
16 篇文章 0 订阅

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<nn,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

模板大法好
因为 答案小于人数,才100,二分一下,嵌套一个最大流,人和人的关系就并查集了

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=205;
const int maxx=20405;
int edge;
int to[maxx],flow[maxx],nex[maxx];
int head[maxn];

void addEdge(int v,int u,int cap)
{
    to[edge]=u,flow[edge]=cap,nex[edge]=head[v],head[v]=edge++;
    to[edge]=v,flow[edge]=0,nex[edge]=head[u],head[u]=edge++;
}
int vis[maxn];
int pre[maxn];
bool bfs(int s,int e)
{
    queue<int> que;
    pre[s]=-1;
    memset(vis,-1,sizeof(vis));
    que.push(s);
    vis[s]=0;
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        for(int i=head[u];~i;i=nex[i])
        {
            int v=to[i];
            if(vis[v]==-1&&flow[i])
            {
                vis[v]=vis[u]+1;
                if(v==e)
                    return true;
                que.push(v);
            }

        }
    }
    return false;
}
int dfs(int s,int t,int f)
{
    if(s==t||!f)
        return f;
    int r=0;
    for(int i=head[s];~i;i=nex[i])
    {
        int v=to[i];
        if(vis[v]==vis[s]+1&&flow[i])
        {
            int d=dfs(v,t,min(f,flow[i]));
            if(d>0)
            {
                flow[i]-=d;
                flow[i^1]+=d;
                r+=d;
                f-=d;
                if(!f)
                    break;
            }
        }
    }
    if(!r)
        vis[s]=INF;
    return r;
}
int maxFlow(int s ,int e)//然后直接调用这个即可
{
    int ans=0;
    while(bfs(s,e))
        ans+=dfs(s,e,INF);
    return ans;
}
int fa[105];
int findd(int x)
{
    return fa[x]==x?x:fa[x]=findd(fa[x]);
}
bool dist[maxn][maxn];
void init()//记得每次使用前初始化
{
    memset(head,-1,sizeof(head));
    edge=0;
}
bool check(int n,int limit)
{
    init();
    for(int i=1;i<=n;i++)
        for(int j=n+1;j<=n*2;j++)
            if(dist[i][j])
                addEdge(i,j,1);
    for(int i=1;i<=n;i++)
        addEdge(0,i,limit);
    for(int i=1;i<=n;i++)
        addEdge(i+n,n+n+1,limit);
    return maxFlow(0,n+n+1)==n*limit;
}
int main()
{
    int t;
    int n,m,f;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&f);
        for(int i=1;i<=n;i++)
            fa[i]=i;
        memset(dist,false,sizeof(dist));
        while(m--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            dist[u][v+n]=true;
        }
        while(f--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            int uu=findd(u);
            int vv=findd(v);
            if(uu!=vv)
                fa[uu]=vv;
        }
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                if(findd(i)==findd(j))
                    for(int k=n+1;k<=n*2;k++)
                        dist[i][k]=dist[j][k]=(dist[i][k]||dist[j][k]);
        int l=0,r=100;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(check(n,mid))
                l=mid+1;
            else
                r=mid-1;
        }
        printf("%d\n",r);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值