HDU3081(KB11-N 二分答案+最大流)

Marriage Match II

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

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

起初做法:

S-girl和boy-T连边,容量n;girl和所有能匹配的boy连边,容量1;然后跑一次最大流,答案为S-girl和boy-T的边中流量的最小值。

但是,发现反例:

1
3 4 0
1 2
2 1
2 3
3 2
答案应该是0,但用上述方法答案为1。

所以需要二分答案,即二分S-girl和boy-T连边的容量mid,建图跑最大流,若mid*n == maxflow,表示可以进行mid轮游戏,继续二分。

  1 //2017-08-25
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <queue>
  7 #include <vector>
  8 #define mid ((l+r)>>1)
  9
 10 using namespace std;
 11
 12 const int N = 510;
 13 const int M = 100000;
 14 const int INF = 0x3f3f3f3f;
 15 int head[N], tot;
 16 struct Edge{
 17     int next, to, w;
 18 }edge[M];
 19
 20 void add_edge(int u, int v, int w){
 21     edge[tot].w = w;
 22     edge[tot].to = v;
 23     edge[tot].next = head[u];
 24     head[u] = tot++;
 25
 26     edge[tot].w = 0;
 27     edge[tot].to = u;
 28     edge[tot].next = head[v];
 29     head[v] = tot++;
 30 }
 31
 32 struct Dinic{
 33     int level[N], S, T;
 34     void init(int _S, int _T){
 35         S = _S;
 36         T = _T;
 37         tot = 0;
 38         memset(head, -1, sizeof(head));
 39     }
 40     bool bfs(){
 41         queue<int> que;
 42         memset(level, -1, sizeof(level));
 43         level[S] = 0;
 44         que.push(S);
 45         while(!que.empty()){
 46             int u = que.front();
 47             que.pop();
 48             for(int i = head[u]; i != -1; i = edge[i].next){
 49                 int v = edge[i].to;
 50                 int w = edge[i].w;
 51                 if(level[v] == -1 && w > 0){
 52                     level[v] = level[u]+1;
 53                     que.push(v);
 54                 }
 55             }
 56         }
 57         return level[T] != -1;
 58     }
 59     int dfs(int u, int flow){
 60         if(u == T)return flow;
 61         int ans = 0, fw;
 62         for(int i = head[u]; i != -1; i = edge[i].next){
 63             int v = edge[i].to, w = edge[i].w;
 64             if(!w || level[v] != level[u]+1)
 65               continue;
 66             fw = dfs(v, min(flow-ans, w));
 67             ans += fw;
 68             edge[i].w -= fw;
 69             edge[i^1].w += fw;
 70             if(ans == flow)return ans;
 71         }
 72         if(ans == 0)level[u] = 0;
 73         return ans;
 74     }
 75     int maxflow(){
 76         int flow = 0;
 77         while(bfs())
 78           flow += dfs(S, INF);
 79         return flow;
 80     }
 81 }dinic;
 82
 83 bool G[N][N];
 84 int T, n, m, f;
 85 int never_quarreled[M][2], friends[M][2];
 86 void build_graph(int cap){
 87     int s = 0, t = 2*n+1;
 88     dinic.init(s, t);
 89     memset(G, 0, sizeof(G));
 90     int a, b;
 91     for(int i = 0; i < m; i++){
 92         a = never_quarreled[i][0];
 93         b = never_quarreled[i][1];
 94         add_edge(a, n+b, 1);
 95         G[a][b] = 1;
 96     }
 97     for(int i = 0; i < f; i++){
 98         a = friends[i][0];
 99         b = friends[i][1];
100         for(int i = head[a]; i != -1; i = edge[i].next){
101             int v = edge[i].to;
102             if(!G[b][v-n] && v != s && v != t){
103                 add_edge(b, v, 1);
104                 G[b][v-n] = 1;
105             }
106         }
107         for(int i = head[b]; i != -1; i = edge[i].next){
108             int v = edge[i].to;
109             if(!G[a][v-n] && v != s && v != t){
110                 add_edge(a, v, 1);
111                 G[a][v-n] = 1;
112             }
113         }
114     }
115     for(int i = 1; i <= n; i++){
116         add_edge(s, i, cap);
117         add_edge(n+i, t, cap);
118     }
119 }
120
121 int main()
122 {
123     std::ios::sync_with_stdio(false);
124     //freopen("inputN.txt", "r", stdin);
125     cin>>T;
126     while(T--){
127         cin>>n>>m>>f;
128         for(int i = 0; i < m; i++)
129               cin>>never_quarreled[i][0]>>never_quarreled[i][1];
130         for(int i = 0; i < f; i++)
131               cin>>friends[i][0]>>friends[i][1];
132         int l = 0, r = n, ans = 0;
133         while(l <= r){
134             build_graph(mid);
135             int flow = dinic.maxflow();
136             if(flow == mid*n){
137                 ans = mid;
138                 l = mid+1;
139             }else{
140                 r = mid-1;
141             }
142         }
143         cout<<ans<<endl;
144     }
145     return 0;
146 }
时间: 2024-10-26 09:04:13

HDU3081(KB11-N 二分答案+最大流)的相关文章

POJ 2112 Optimal Milking 二分答案+最大流

首先二分最长的边,然后删去所有比当前枚举的值长的边,算最大流,看是否能满足所有的牛都能找到挤奶的地方 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

BZOJ 1305 CQOI2009 dance跳舞 二分答案+最大流

题目大意:给定n个男生和n个女生,一些互相喜欢而一些不.举行几次舞会,每次舞会要配成n对.不能有同样的组合出现.每一个人仅仅能与不喜欢的人跳k次舞,求最多举行几次舞会 将一个人拆成两个点.点1向点2连一条流量为k的边.两个人若互相喜欢则点1之间连边,不喜欢则点2之间连边 对于每个要验证的x值 将每个人的点1向源或汇连一条流量为x的边 然后二分答案跑最大流就可以 #include<cstdio> #include<cstring> #include<iostream> #

BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 shelter 点, 然后对于每个 farm x : S -> cow( x ) = cow( x ) 数量 , shelter( x ) -> T = shelter( x ) 容量 ; 对于每个dist( u , v ) <= m 的 cow( u ) -> shelter( v

BZOJ 1189 HNOI 2007 紧急疏散 evacuate 二分答案 最大流

题目大意:紧急疏散.有一张地图,'.'表示人,'D'表示门,人需要走曼哈顿距离的单位时间才1能到达门.一个门一个时刻只能通过一个人.求多长时间能疏散完毕. 思路:二分答案+最大流满流判定.先BFS处理出每个人与门的距离.二分最小时间,然后连边.S向每个人连流量为1的边,每个人向二分的时间之内能到达的门连流量为1的边.每个门向T连流量为t的边.然后最大流判定是否满流. (数组大小我是瞎开的,写代码的时候要算好了在开!) CODE: #include <queue> #include <cs

BZOJ 1189 HNOI2007 紧急疏散evacuate 二分答案+最大流

题目大意:给定一个m*n的地图,每个点有可能是空地.墙或者出口,每个空地初始站着一个人,每一时刻可以向周围走1格,门每一时刻只能通过一个人,求最短多少时间后所有人可以撤离 首先从每个出口出发开始广搜,得到每个空地到所有出口的距离 然后二分答案,每次建图如下: 从源点向每个空地一条流量为1的边 如果一个空地能在规定时间到达某个出口,就从这个空地出发向该出口链接一条流量为1的边 每个出口向汇点连接一条流量为时间的边 然后跑最大流验证即可 注意图有不连通的情况 所以广搜要清初值(这个没人会忘吧QAQ

luoguP1401 城市(二分答案+最大流)

题意 N(2<=n<=200)个城市,M(1<=m<=40000)条无向边,你要找T(1<=T<=200)条从城市1到城市N的路,使得最长的边的长度最小,边不能重复用. 题解 简单的网络流判定. 一看问法想到二分答案.然后边不能重复直接上网络流. (用边长小于mid的边建图然后跑最大流,最后比较流量和k) (然而不知为何写挂了) 1 #include<cstdio> 2 #include<cstring> 3 #include<queue&

Gym - 101908G 二分答案+最大流

After the end of the truck drivers' strike, you and the rest of Nlog?nia logistics specialists now have the task of planning the refueling of the gas stations in the city. For this, we collected information on stocks of R refineries and about the dem

BZOJ 3993 Sdoi2015 星际战争 二分答案+最大流

题目大意:有n个机器人和m个激光武器,每个武器有一个威力和能打的集合,同一时刻只能打一个机器人,问最少多久可以全灭 二分答案+网络流= = 注意二分上界 #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> #define M 110 #define S 0 #define T (M-1) #define E

【BZOJ1189】【HNOI2007】紧急疏散evacuate 二分答案+最大流check

#include <stdio.h> int main() { puts("转载请注明出处"); puts("地址:blog.csdn.net/vmurder/article/details/43666807"); } 题解: 首先floyd或者bfs求出每个'.'到各个'D'的最短路 然后注意一个点不能经过一个门去另一个门,所以可以看到我的floyd略有一点点点点不同... 然后这个时间限制可以转化为对每个门的拆点,可以证明拆400个就够了. 然后分别