标签:++ tmp 图片 mem else targe stdout init view
题意:有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 }
HDU 3081 Marriage Match II 二分 + 网络流
标签:++ tmp 图片 mem else targe stdout init view
原文地址:https://www.cnblogs.com/MingSD/p/9738729.html