HDU 3081 Marriage Match II (dichotomy + maximum flow)

Source: Internet
Author: User

HDU 3081 Marriage Match II

pid=3081 "target=" _blank "style=" "> Topic Links

Test instructions: N Girls n boys, every girl can be paired with some boys. Then some girls are friends. Meet the people in this circle of friends, assuming that there is a match with one of the boys, the others will be able to, and then each round requires each girl to be matched to a boy. And each round match to the different, ask can match a few rounds

Idea: The two-wheel number k, then the map is, the source point to the girl, the boy even to the meeting point capacity is K, and then between the girl and boy with the edge. There is a relationship between the edge capacity 1, so that one matches the corresponding edge. And does not repeat, each time the maximum flow is inferred equal to n * k can be

Code:

#include <cstdio> #include <cstring> #include <queue> #include <algorithm>using namespace std; const int maxnode = 205;const int MAXEDGE = 100005;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 = 105;int T, N, M, F, G[n][n], parent[n];int find (int x) {return x = = Parent[x]? x:parent[x] = find (par Ent[x]);} BOOL Judge (int k) {int s = 0, t = n * 2 + 1;gao.init (t + 1); for (int i = 1; I <= n; i++) {Gao.add_edge (S, I, k); Gao.add _edge (i + N, T, k); for (int j = 1; j <= N;J + +) if (G[i][j]) Gao.add_edge (i, J + N, 1);} Return Gao. Maxflow (s, t) = = n * k;} int main () {scanf ("%d", &t), while (t--) {scanf ("%d%d%d", &n, &m, &f), memset (g, 0, sizeof (g)); for (int i = 1; I <= N; i++) Parent[i] = I;int u, v;while (m--) {scanf ("%d%d", &u, &v); g[u][v] = 1;} while (f--) {scanf ("%d%d", &u, &v), int pa = find (u), int pb = Find (v), if (PA! = PB) Parent[pa] = PB;} for (int i = 1, i <= N; i++) for (int j = 1; J <= N; j + +) {if (find (i) = = Find (j)) {for (int k = 1; k <= N; k++) g[ I][K] |= g[j][k];} 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;}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

HDU 3081 Marriage Match II (dichotomy + maximum flow)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.