HDU 3081 marriage match II [query set + bipartite graph maximum match]

Source: Internet
Author: User
Tags rounds

N boys and N girls, tell you which boys each girl likes, and tell you some friends between girls.

A girl can marry a boy she likes or a boy that her friends like, and the relationship between her friends can be transmitted.

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?

How many rounds of game can be played

Analysis:

Use and check the set to process the relationship between friends.

When the maximum match is = N, rounds ++ deletes the matched edge.

Find the number of round

 

Code:

  1 #include <iostream>  2 #include <cstdio>  3 #include <cstring>  4 #include <vector>  5 using namespace std;  6   7 const int maxn = 105;  8 const int INF = 1000000000;  9  10 int fa[maxn]; 11 void init(int x) { 12     for(int i = 0; i <= x; i++) { 13         fa[i] = i; 14     } 15 } 16  17 int find(int u) { 18     if(fa[u] == u) return u; 19     return fa[u] = find(fa[u]); 20 } 21  22 void unin(int u, int v) { 23     int fu = find(u); 24     int fv = find(v); 25     if(fu != fv) { 26         fa[fu] = fv; 27     } 28 } 29  30  31 int Link[maxn]; 32 int W[maxn][maxn]; 33 int vis[maxn]; 34 int n; 35  36 int Find(int i) { 37     for(int j = 1; j <= n; j++) { 38         if(W[i][j] && !vis[j]) { 39             vis[j] = 1; 40             if(Link[j] == -1 || Find(Link[j]) ) { 41                 Link[j] = i; 42                 return true; 43             } 44         } 45     } 46     return false; 47 } 48  49 bool Match() { 50     memset(Link, -1, sizeof(Link)); 51     for(int i = 1; i <= n; i++) { 52         memset(vis, 0, sizeof(vis)); 53         if(!Find(i)) return false; 54     } 55     return true; 56 } 57  58 int Solve() { 59     int cnt = 0; 60     while(1) { 61         if(Match() ) { 62             cnt ++; 63             for(int j = 1; j <= n; j++) { 64                 W[Link[j]][j] = 0; 65             } 66         } else break; 67     } 68     return cnt; 69 } 70  71 vector<int> v[maxn * maxn]; 72  73 int main() { 74     int m, f; 75     int a, b; 76     int c, d; 77     int t; 78     scanf("%d", &t); 79     while(t--) { 80         scanf("%d %d %d", &n, &m, &f); 81         memset(W, 0, sizeof(W)); 82         init(n); 83         for(int i = 0; i <= n; i++) v[i].clear(); 84         for(int i = 1; i <= m; i++) { 85             scanf("%d %d", &a, &b); 86             v[a].push_back(b); 87         } 88         for(int i = 1; i <= f; i++) { 89             scanf("%d %d",&c, &d); 90             unin(c, d); 91         } 92         for(int i = 1; i <= n; i++) { 93             for(int j = 1; j <= n; j++) { 94                 if(find(i) == find(j) ) { 95                     for(int k = 0; k < v[j].size(); k++) { 96                         W[i][v[j][k]] = 1; 97                     } 98                 } 99             }100         }101         printf("%d\n", Solve() ) ;102     }103     return 0;104 }
View code

 

HDU 3081 marriage match II [query set + bipartite graph maximum match]

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.