HDU 3081 Marriage Match II 二分 + 网络流

Marriage Match II

题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女生配对,每轮配对过的人在接下来中都不能配对,求这个k最大是多少。

题解:二分 + 网络流check 。

代码:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
  4 #define LL long long
  5 #define ULL unsigned LL
  6 #define fi first
  7 #define se second
  8 #define pb emplace_back
  9 #define lson l,m,rt<<1
 10 #define rson m+1,r,rt<<1|1
 11 #define lch(x) tr[x].son[0]
 12 #define rch(x) tr[x].son[1]
 13 #define max3(a,b,c) max(a,max(b,c))
 14 #define min3(a,b,c) min(a,min(b,c))
 15 typedef pair<int,int> pll;
 16 const int inf = 0x3f3f3f3f;
 17 const LL INF = 0x3f3f3f3f3f3f3f3f;
 18 const LL mod =  (int)1e9+7;
 19 const int N = 300;
 20 const int M = N * N;
 21 int head[N], deep[N], cur[N];
 22 int w[M], to[M], nx[M];
 23 int tot;
 24 void add(int u, int v, int val){
 25     w[tot]  = val; to[tot] = v;
 26     nx[tot] = head[u]; head[u] = tot++;
 27
 28     w[tot] = 0; to[tot] = u;
 29     nx[tot] = head[v]; head[v] = tot++;
 30 }
 31 int bfs(int s, int t){
 32     queue<int> q;
 33     memset(deep, 0, sizeof(deep));
 34     q.push(s);
 35     deep[s] = 1;
 36     while(!q.empty()){
 37         int u = q.front();
 38         q.pop();
 39         for(int i = head[u]; ~i; i = nx[i]){
 40             if(w[i] > 0 && deep[to[i]] == 0){
 41                 deep[to[i]] = deep[u] + 1;
 42                 q.push(to[i]);
 43             }
 44         }
 45     }
 46     return deep[t] > 0;
 47 }
 48 int Dfs(int u, int t, int flow){
 49     if(u == t) return flow;
 50     for(int &i = cur[u]; ~i; i = nx[i]){
 51         if(deep[u]+1 == deep[to[i]] && w[i] > 0){
 52             int di = Dfs(to[i], t, min(w[i], flow));
 53             if(di > 0){
 54                 w[i] -= di, w[i^1] += di;
 55                 return di;
 56             }
 57         }
 58     }
 59     return 0;
 60 }
 61
 62 int Dinic(int s, int t){
 63     int ans = 0, tmp;
 64     while(bfs(s, t)){
 65         for(int i = 0; i <= t; i++) cur[i] = head[i];
 66         while(tmp = Dfs(s, t, inf)) ans += tmp;
 67     }
 68     return ans;
 69 }
 70
 71 void init(){
 72     memset(head, -1, sizeof(head));
 73     tot = 0;
 74 }
 75 int pre[N];
 76 int link[N][N];
 77 int edge[M][2];
 78 int Find(int x){
 79     if(x == pre[x]) return x;
 80     return pre[x] = Find(pre[x]);
 81 }
 82 int T, n, m, f, s, t, u, v;
 83 void GG(int val){
 84     for(int i = head[s]; ~i; i = nx[i]){
 85         if(i&1);
 86         else w[i] = val, w[i+1] = 0;
 87     }
 88     for(int i = n+1; i <= n+n; i++){
 89         for(int j = head[i]; ~j; j = nx[j]){
 90             if(j&1);
 91             else w[j] = val, w[j+1] = 0;
 92         }
 93     }
 94     for(int i = 1; i <= n; i++){
 95         for(int j = head[i]; ~j; j = nx[j]){
 96             if(j&1);
 97             else w[j] = 1, w[j+1] = 0;
 98         }
 99     }
100 }
101 int main(){
102     scanf("%d", &T);
103     while(T--){
104         init();
105         scanf("%d%d%d", &n, &m, &f);
106         for(int i = 1; i <= n; i++){
107             pre[i] = i;
108             for(int j = 1; j <= n; j++)
109                 link[i][j] = 0;
110         }
111         for(int i = 1; i <= m; i++)
112             scanf("%d%d", &edge[i][0], &edge[i][1]);
113         for(int i = 1; i <= f; i++){
114             scanf("%d%d", &u, &v);
115             u = Find(u);
116             v = Find(v);
117             if(v == u) continue;
118             pre[u] = v;
119         }
120         for(int i = 1; i <= m; i++){
121             int z = Find(edge[i][0]);
122             link[z][edge[i][1]] = 1;
123         }
124         s = 0, t = 2 * n + 1;
125         for(int i = 1; i <= n; i++){
126             add(s, i, 1);
127             int z = Find(i);
128             for(int j = 1; j <= n; j++){
129                 if(link[z][j])
130                     add(i, j+n, 1);
131             }
132             add(i+n,t,1);
133         }
134         int l = 1, r = n;
135         while(l <= r){
136             int mid = l+r >> 1;
137             GG(mid);
138             if(Dinic(s,t) == mid*n) l = mid + 1;
139             else r = mid - 1;
140         }
141         printf("%d\n", l-1);
142     }
143     return 0;
144 }

原文地址:https://www.cnblogs.com/MingSD/p/9738729.html

时间: 2024-07-30 10:17:44

HDU 3081 Marriage Match II 二分 + 网络流的相关文章

HDU 3081 Marriage Match II 二分+最大流

题目来源:HDU 3081 Marriage Match II 题意: 思路: 错误代码 纠结不知道哪错了 先放一放 #include <cstdio> #include <queue> #include <vector> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1010; const int INF = 999999999; st

HDU 3081 Marriage Match II(网络流+并查集+二分答案)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题目: 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. W

HDU 3081 Marriage Match II &lt;&lt;二分最大流 + 并查集

题意 n个女孩子跟n个男孩子过家家,女孩子选男孩子,告诉你每个女孩子可选的男孩子与女孩子之间的好友关系,好友关系是互相的而且是传递的,然后如果两个女孩子是好友,他们可选的男孩子也是可以合并的.然后每一轮进行匹配,匹配成功后开始下一轮,每个女孩子只能选同一个男孩子一次,问最多能玩几轮. 思路 首先,好友关系的建立显然就直接想到了用并查集处理. 然后在建图时,可以选择是二分图,然后跑完备匹配,每次匹配完后删除匹配边进行下一次匹配. 当然,不会二分图的我就选择直接跑网络流啦,但是建图时候发现需要知道流

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

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

hdu 3081 Marriage Match II (二分+最大流+并查集)

hdu 3081 Marriage Match II 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 p

hdu 3081 Marriage Match II(最大流 + 二分 + 并查集)

Marriage Match II                                                                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Presumably, you all have known the question of stable

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

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

HDU 3081 Marriage Match II

Marriage Match II Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 308164-bit integer IO format: %I64d      Java class name: Main Presumably, you all have known the question of stable marriage match. A girl wi

HDU 3081 Marriage Match II 二分图最大匹配

Marriage Match II 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 pl