【HDU

来源:互联网 发布:淘宝买家查号131458 编辑:程序博客网 时间:2024/06/09 17:21

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. 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 1 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3
Sample Output
3

这道题,同另一道题 Marriage Match II 相似,但是多了个条件,就是女生可以额外多和她不习惯的k个男生作伴,问这样情况下的能够玩多少局。

就建图那里多个限制就好了,详细的看代码吧。
代码

#include<bits/stdc++.h>using namespace std;const int MAXN  =1000+10;const int MAXM = 1e5;const int inf =0x3f3f3f3f;int pre[MAXN];int mp[MAXN][MAXN];int n,m,f,K;void It(){    for(int i=0;i<=n;i++)        pre[i]=i;    memset(mp,0,sizeof(mp));}int Find(int x){ return x==pre[x]?x:pre[x]=Find(pre[x]);}void join(int a,int b){    a=Find(a);b=Find(b);    if(a!=b) pre[a]=b;}void Input(){    int a,b,c;    while(m--){        scanf("%d%d",&a,&b);        mp[a][b]=1;    }    while(f--){        scanf("%d%d",&a,&b);        join(a,b);    }    for(int i=1;i<=n;i++){        for(int j=1;j<=n;j++){            if(i==j) continue;            if(Find(i)==Find(j)) {                for(int k=1;k<=n;k++){                    if(mp[j][k]) mp[i][k]=1;                }            }        }    }}struct Edge {    int from,to,cap,flow,nexts;}edge[MAXM];int head[MAXN],top;void init(){    memset(head,-1,sizeof(head));    top=0;}void addedge(int a,int b,int c){    Edge e={a,b,c,0,head[a]};    edge[top]=e;head[a]=top++;    Edge ee={b,a,0,0,head[b]};    edge[top]=ee;head[b]=top++;}void getmap(int mid){//0是源点,n+n+n+1是汇点    for(int i=1;i<=n;i++){            addedge(0,i,mid);            addedge(i,i+n,K);// 这个边意思就是女生可以额外多k个流去选择不喜欢的男生            addedge(n+n+i,3*n+1,mid);        for(int j=1;j<=n;j++){            if(mp[i][j])                addedge(i,j+n+n,1);            else addedge(n+i,n+n+j,1);//不喜欢的男生要建边        }    }}int vis[MAXN],dis[MAXN];int cur[MAXN];bool bfs(int st,int ed){    memset(vis,0,sizeof(vis));    queue<int>Q; Q.push(st);    vis[st]=1;dis[st]=1;    while(!Q.empty()){        int now=Q.front();Q.pop();        for(int i=head[now];i!=-1;i=edge[i].nexts){            Edge e=edge[i];            if(!vis[e.to]&&e.cap-e.flow>0){                vis[e.to]=1;                dis[e.to]=dis[now]+1;                if(e.to==ed)  return 1;                Q.push(e.to);            }        }    }    return 0;}int dfs(int now,int a,int ed){    if(now==ed||a==0) return a;    int flow=0,f;    for(int& i=cur[now];i!=-1;i=edge[i].nexts){        Edge &e=edge[i];        if(dis[e.to]==dis[now]+1&&(f=dfs(e.to,min(a,e.cap-e.flow),ed))>0){            e.flow+=f;            flow+=f;            edge[i^1].flow-=f;            a-=f;            if(a==0) break;        }    }    return flow;}int max_flow(int st,int ed){    int flow=0;    while(bfs(st,ed)){        memcpy(cur,head,sizeof(head));        flow+=dfs(st,inf,ed);    }    return flow;}void solve(){   int le=0;int ri=n;   int ans=0;    while(le<=ri){        int mid=(le+ri)>>1;        init();        getmap(mid);        if(max_flow(0,n+n+n+1)==mid*n) {ans=mid;le=mid+1;}   //直到不完美匹配        else   ri=mid-1;    }    printf("%d\n",ans);}int main(){    int t;cin>>t;    while(t--){        scanf("%d%d%d%d",&n,&m,&K,&f);        It();        Input();        solve();    }    return 0;}
原创粉丝点击