HDU 3277 Marriage Match III
Topic links
Test instructions: N girls n Boys, each girl can be paired with some boys, and also can be paired with K random boys. Then some girls are friends and meet the people in this circle of friends. Let's say one of them can be paired with a boy. Then each round was asked to match each girl to a boy, and each round matched to a different one. Can match up to a few rounds
Idea, more than HDU3081 a condition, in addition to be able to and K random boys paired. To the model, there is one more node, two ways on both sides. A pair of connected pairs that cannot be paired. In addition to ensure that the flow is counted together, how to do it.
In fact, the demolition of a point can be, a girl split into two points. One can match, one cannot match, the source point is connected to a point, the capacity is mid, and then between two points, in a side. Connected, so that the total flow will not exceed mid, the other and the previous problem is not much. It's just that the data is bigger than the previous one, so pay attention to the practice of preprocessing.
Code:
#include <cstdio> #include <cstring> #include <queue> #include <algorithm>using namespace std; const int maxnode = 805;const int MAXEDGE = 200005;typedef int type;const Type INF = 0x3f3f3f3f;struct Edge {int u, v; Type cap, Flow; Edge () {}edge (int u, int v, type cap, type flow) {This->u = U;this->v = V;this->cap = Cap;this->flow = flow;} };struct dinic {int n, m, S, t; Edge edges[maxedge];int first[maxnode];int Next[maxedge];bool Vis[maxnode]; Type d[maxnode];int cur[maxnode];vector<int> cut;void init (int n) {this->n = N;memset (First,-1, sizeof (first)) ; m = 0;} void Add_edge (int u, int v, Type cap) {Edges[m] = Edge (U, V, cap, 0); Next[m] = first[u];first[u] = M++;edges[m] = Edge (V, U, 0, 0); Next[m] = first[v];first[v] = m++;} BOOL BFs () {memset (Vis, false, sizeof (VIS));queue<int> Q; Q.push (s);d [s] = 0;vis[s] = True;while (! Q.empty ()) {int u = q.front (); Q.pop (); for (int i = first[u]; I! =-1; i = Next[i]) {edge& e = Edges[i];if (!VIS[E.V] && E.cap > E.flow) {vis[e.v] = true;d[e.v] = D[u] + 1; Q.push (E.V);}}} return vis[t];} Type DFS (int u, Type a) {if (U = = T | | a = = 0) return A; Type flow = 0, f;for (int &i = Cur[u]; i = 1; i = Next[i]) {edge& e = edges[i];if (D[u] + 1 = d[e.v] && (f = DFS (e.v, Min (A, e.cap-e.flow))) > 0) {e.flow + = F;edges[i^1].flow = F;flow + F;a = f;if (A = = 0) break;} return flow;} Type maxflow (int s, int t) {this->s = s; this->t = t; Type flow = 0;while (BFS ()) {for (int i = 0; i < n; i++) Cur[i] = First[i];flow + = DFS (s, INF);} return flow;} void Mincut () {cut.clear (); for (int i = 0; i < m; i + = 2) {if (vis[edges[i].u] &&!vis[edges[i].v]) Cut.push_bac K (i);}}} gao;const int N = 205;int T, N, M, K, F, G[n][n], parent[n];int u[n * N], V[n * n];int find (int x) {return x = = Parent[x] ? X:PARENT[X] = find (Parent[x]);} BOOL Judge (int mid) {int s = 0, t = n * 3 + 1;gao.init (3 * n + 2); for (int i = 1; I <= n; i++) {Gao.add_edge (S, I, mid) ; Gao.add_edge (i, i + N, k); Gao.add_edge (i + 2 * N, T, mid);} for (int i = 1; I <= n; i++) {for (int j = 1; J <= N; j + +) {if (G[i][j]) Gao.add_edge (i, j + 2 * N, 1); else gao.add_ Edge (i + N, j + 2 * N, 1);}} Return Gao. Maxflow (s, t) = = n * MID;} int main () {scanf ("%d", &t), while (t--) {scanf ("%d%d%d%d", &n, &m, &k, &f), memset (g, 0, sizeof (g)); fo R (int i = 1; I <= n; i++) Parent[i] = i;for (int i = 0; i < m; i++) scanf ("%d%d", &u[i], &v[i]); int A, B;WHI Le (f--) {scanf ("%d%d", &a, &b); int pa = find (a); int PB = find (b); if (PA! = PB) Parent[pa] = PB;} for (int i = 0; i < m; i++) G[find (U[i])][v[i] [1;for (int i = 1; I <= n; i++) for (int j = 1; J <= N; j + +) G[i][j ] |= g[find (i)][j];int L = 1, R = n + 1;while (L < r) {int mid = (L + R)/2;if (judge (mid)) L = mid + 1;else r = Mid;} printf ("%d\n", L-1);} return 0;}
HDU 3277 Marriage Match III (two points + maximum flow)