HDU 3277 Marriage Match III

Marriage Match III

Time Limit: 4000ms

Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 3277
64-bit integer IO format: %I64d      Java class name: Main

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

1
4 5 1 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3

Sample Output

3

Source

HDOJ Monthly Contest – 2010.01.02

解题:最大流。。。

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 const int maxn = 260;
  4 struct arc{
  5     int to,flow,next;
  6     arc(int x = 0,int y = 0,int z = 0){
  7         to = x;
  8         flow = y;
  9         next = z;
 10     }
 11 }e[1000010];
 12 int head[maxn*maxn],d[maxn*maxn],cur[maxn*maxn];
 13 int tot,n,m,k,S,T,uf[maxn];
 14 void add(int u,int v,int flow){
 15     e[tot] = arc(v,flow,head[u]);
 16     head[u] = tot++;
 17     e[tot] = arc(u,0,head[v]);
 18     head[v] = tot++;
 19 }
 20 int Find(int x){
 21     if(x != uf[x]) uf[x] = Find(uf[x]);
 22     return uf[x];
 23 }
 24 bool bfs(){
 25     queue<int>q;
 26     q.push(T);
 27     memset(d,-1,sizeof d);
 28     d[T] = 1;
 29     while(!q.empty()){
 30         int u = q.front();
 31         q.pop();
 32         for(int i = head[u]; ~i; i = e[i].next){
 33             if(e[i^1].flow > 0 && d[e[i].to] == -1){
 34                 d[e[i].to] = d[u] + 1;
 35                 q.push(e[i].to);
 36             }
 37         }
 38     }
 39     return d[S] > -1;
 40 }
 41 int dfs(int u,int low){
 42     if(u == T) return low;
 43     int tmp = 0,a;
 44     for(int &i = cur[u]; ~i; i = e[i].next){
 45         if(e[i].flow > 0 && d[e[i].to]+1== d[u]&&(a=dfs(e[i].to,min(e[i].flow,low)))){
 46             e[i].flow -= a;
 47             low -= a;
 48             e[i^1].flow += a;
 49             tmp += a;
 50             if(!low) break;
 51         }
 52     }
 53     if(!tmp) d[u] = -1;
 54     return tmp;
 55 }
 56 int dinic(){
 57     int ret = 0;
 58     while(bfs()){
 59         memcpy(cur,head,sizeof head);
 60         ret += dfs(S,INT_MAX);
 61     }
 62     return ret;
 63 }
 64 bool con[maxn][maxn];
 65 int g[maxn*maxn],b[maxn*maxn];
 66 void build(int mid){
 67     memset(head,-1,sizeof head);
 68     tot = 0;
 69     for(int i = 1; i <= n; ++i){
 70         add(i,i+n,k);
 71         add(S,i,mid);
 72         add(i+2*n,T,mid);
 73     }
 74     for(int i = 1; i <= n; ++i)
 75         for(int j = 1; j <= n; ++j)
 76             if(con[Find(i)][j]) add(i,j+2*n,1);
 77             else add(i+n,j+2*n,1);
 78 }
 79 int main(){
 80     int kase,f,u,v;
 81     scanf("%d",&kase);
 82     while(kase--){
 83         scanf("%d%d%d%d",&n,&m,&k,&f);
 84         S = 0;
 85         T = 3*n + 1;
 86         for(int i = 0; i < maxn; ++i) uf[i] = i;
 87         for(int i = 0; i < m; ++i)
 88             scanf("%d%d",g+i,b+i);
 89         for(int i = 0; i < f; ++i){
 90             scanf("%d%d",&u,&v);
 91             u = Find(u);
 92             v = Find(v);
 93             if(u != v) uf[u] = v;
 94         }
 95         memset(con,false,sizeof con);
 96         for(int i = 0; i < m; ++i)
 97             con[Find(g[i])][b[i]] = true;
 98         int low = 0,high = n,ret = 0;
 99         while(low <= high){
100             int mid = (low + high)>>1;
101             build(mid);
102             if(dinic() == n*mid){
103                 ret = mid;
104                 low = mid+1;
105             }else high = mid - 1;
106         }
107         printf("%d\n",ret);
108     }
109     return 0;
110 }

时间: 2024-10-13 23:28:19

HDU 3277 Marriage Match III的相关文章

HDU 3277 Marriage Match III(二分+最大流)

HDU 3277 Marriage Match III 题目链接 题意:n个女孩n个男孩,每个女孩可以和一些男孩配对,此外还可以和k个任意的男孩配对,然后有些女孩是朋友,满足这个朋友圈里面的人,如果有一个能和某个男孩配对,其他就都可以,然后每轮要求每个女孩匹配到一个男孩,且每轮匹配到的都不同,问最多能匹配几轮 思路,比HDU3081多了一个条件,此外可以和k个任意的男孩配对,转化为模型,就是多了一个结点,有两种两边的方式,一种连向可以配对的,一种连向不能配对的,此外还要保证流量算在一起,这要怎么

HDU 3277 Marriage Match III(拆点+二分+最大流SAP)

这个题目是说,有n个女的和男的找伴侣.然后女的具有主动选择权,每个女的可以选自己喜欢的男的,也可以挑选k个不喜欢的男的,做法就是:把女的拆点,u1->u2建立一条容量为k的边.如果遇见喜欢的男生i->j+2*n建一条容量为1的边,否则i+n->j+2*n建一条容量为1的边.最后将源点和女生相连容量为mid,汇点与男生相连容量为mid.枚举mid,看是否会产生满流. 可能姿势不够优美dinic超时了啊,换成SAP快了很多啊... Marriage Match III Time Limit:

hdu 3277 Marriage Match III【最大流+并查集+二分枚举】

Marriage Match III Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1491    Accepted Submission(s): 440 Problem Description Presumably, you all have known the question of stable marriage match.

HDU 3277 Marriage Match III(并查集+二分答案+最大流SAP)拆点,经典

Marriage Match III Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1581    Accepted Submission(s): 464 Problem Description Presumably, you all have known the question of stable marriage match.

【HDOJ】3277 Marriage Match III

Dinic不同实现的效率果然不同啊. 1 /* 3277 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algori

hdu 3081 hdu 3277 hdu 3416 Marriage Match II III IV //最大流的灵活运用

3081 题意: n个女孩选择没有与自己吵过架的男孩有连边(自己的朋友也算,并查集处理),2分图,有些边,求有几种完美匹配(每次匹配每个点都不重复匹配) 我是建二分图后,每次增广一单位,(一次完美匹配),再修改起点还有终点的边流量,继续增广,直到达不到完美匹配为止.网上很多是用二分做的,我觉得没必要...(网上传播跟风真严重...很多人都不是真正懂最大流算法的...) 3277 : 再附加一条件,每个女孩可以最多与k个自己不喜欢的男孩.求有几种完美匹配(同上). 我觉得:求出上题答案,直接ans

HDU 3416 Marriage Match IV(最短路+最大流)

HDU 3416 Marriage Match IV 题目链接 题意:给一个有向图,给定起点终点,问最多多少条点可以重复,边不能重复的最短路 思路:边不能重复,以为着每个边的容量就是1了,最大流问题,那么问题只要能把最短路上的边找出来,跑一下最大流即可,判断一条边是否是最短路上的边,就从起点和终点各做一次dijstra,求出最短路距离后,如果一条边满足d1[u] + d2[v] + w(u, v) == Mindist,那么这条边就是了 代码: #include <cstdio> #inclu

HDU 3081 Marriage Match II(二分法+最大流量)

HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个女孩n个男孩,每一个女孩能够和一些男孩配对.然后有些女孩是朋友.满足这个朋友圈里面的人,假设有一个能和某个男孩配对,其它就都能够,然后每轮要求每一个女孩匹配到一个男孩.且每轮匹配到的都不同,问最多能匹配几轮 思路:二分轮数k,然后建图为,源点连向女孩,男孩连向汇点容量都为k,然后女孩和男孩之间连边为.有关系的

HDU 3081 Marriage Match II(二分+最大流)

HDU 3081 Marriage Match II 题目链接 题意:n个女孩n个男孩,每个女孩可以和一些男孩配对,然后有些女孩是朋友,满足这个朋友圈里面的人,如果有一个能和某个男孩配对,其他就都可以,然后每轮要求每个女孩匹配到一个男孩,且每轮匹配到的都不同,问最多能匹配几轮 思路:二分轮数k,然后建图为,源点连向女孩,男孩连向汇点容量都为k,然后女孩和男孩之间连边为,有关系的连边容量1,这样一个匹配对应一条边,且不会重复,每次判断最大流是否等于n * k即可 代码: #include <cst