Marriage Match III (hdu 3277 网络流+并查集+二分)

来源:互联网 发布:战地之王刷枪软件 编辑:程序博客网 时间:2024/05/22 06:16

Marriage Match III

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1523    Accepted Submission(s): 456


Problem Description
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the ever game of play-house . What a happy time as so many friends play 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. As 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. On the other hand, in order to play more times of marriage match, every girl can accept any K boys. If a girl chooses a boy, the boy must accept her unconditionally whether they had quarreled before or not. 

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 an integer T, means the number of test cases. 
Each test case starts with three integer n, m, K and f in a line (3<=n<=250, 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
14 5 1 21 12 33 24 24 41 42 3
 

Sample Output
3
 

Author
starvae
 

Source
HDOJ Monthly Contest – 2010.01.02
 

Recommend
chenheng   |   We have carefully selected several similar problems for you:  3416 1569 1565 3046 3061 
 

题意:hdu3081的加强版,n个女孩n个男孩,女孩选男朋友,如果两个女孩认识,那她们可以在自己认识的或对方认识的男孩中任选一个,每一轮中,每个女孩还可以额外选择K个自己原本不打算和她配对的男孩配对(这是与hdu3081的不同之处)。选完一轮后打乱重新选,已经配对过的不能再选在一起,问最后最多能选几轮。

思路:添加源点和汇点,将每个女孩拆点 i 和 i‘ ,从i到i’连一天权为k的边,源点s与每个女孩连一条权为mid的边,每个男孩与汇点连一条权为mid的边,若女孩i和男孩j可以配对则从女孩i向男孩j连一条权为1的边,否则从i‘向j连一条权为1的边,二分法得mid,判断是否满流。

另外下面的代码写好后开始用的C++交的一直WA,快死心时用G++交了一发竟然过了。。。求大神告知原因。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define MAXN 1005#define MAXM 100000#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b)  for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b)  for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v)   memset ((t) , v, sizeof(t))#define sf(n)       scanf("%d", &n)#define sff(a,b)    scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf          printf#define DBG         pf("Hi\n")typedef long long ll;using namespace std;struct Edge{    int to,next,cap,flow;}edge[MAXM];int n,m,K,f;int tol;int head[MAXN];int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];int father[MAXN];int mp[MAXN][MAXN];void init(){    memset(mp,0,sizeof(mp));    for (int i=0;i<n*n+100;i++)        father[i]=i;}//加边,单向图三个参数,双向图四个参数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;}int find_father(int x){    if (x!=father[x])        father[x]=find_father(father[x]);    return father[x];}void Union(int a,int b){    int fa=find_father(a);    int fb=find_father(b);    if (fa!=fb)        father[fa]=fb;    return;}void build_graph(int mid)   //网络流建图{    tol=0;    memset(head,-1,sizeof(head));    for (int i=1;i<=n;i++)    {        addedge(0,i,mid);        addedge(i,n+i,K);        addedge(2*n+i,3*n+1,mid);    }    for (int i=1;i<=n;i++)    {        for (int j=1;j<=n;j++)        {            if (mp[i][j])                addedge(i,2*n+j,1);            else                addedge(i+n,2*n+j,1);        }    }}void solve()    //二分{    int l=0,r=n,ans;    while (l<=r)    {        int mid=(l+r)>>1;        build_graph(mid);        if (sap(0,3*n+1,3*n+2)==n*mid)        {            ans=mid;            l=mid+1;        }        else            r=mid-1;    }    printf("%d\n",ans);}int main(){    int i,j,k,t,a,b;    scanf("%d",&t);    while (t--)    {        init();        scanf("%d%d%d%d",&n,&m,&K,&f);        for (i=0;i<m;i++)        {            scanf("%d%d",&a,&b);            mp[a][b]=1;        }        for (int i=0;i<f;i++)        {            scanf("%d%d",&a,&b);            Union(a,b);     //并查集连接        }        for (i=1;i<=n;i++)  //根据题目构建女孩到男孩的关系        {            int x=find_father(i);            for (j=1;j<=n;j++)  //i,j代表女孩编号            {                if (j!=i&&find_father(j)==x)    //女孩i和女孩j可以共用男孩                {                    for (k=1;k<=n;k++)  //k代表男孩编号                    {                        if (mp[i][k])   //若女孩i可以与男孩k配对                            mp[j][k]=1; //则女孩j也可以与男孩k配对                    }                }            }        }        solve();    }    return 0;}



1 0
原创粉丝点击