Hdu3081 binary match

Source: Internet
Author: User

Question:

Marriage match II Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2249 accepted submission (s): 773


Problem descriptionpresumably, 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 far many DS playing together. and it is normal that a fight or a quarrel breaks out, but we will still play together after that, because we are kids.
Now, there are 2n kids, N boys numbered from 1 to n, and N Girls numbered from 1 to n. you know, ladies first. so, every girl can choose a boy first, with whom she has not quarreled, to make up a family. besides, the girl X can also choose boy Z to be her boyfriend when her friend, girl y has not quarreled with him. furthermore, the friendship is mutual, Which Means A and C are friends provided that A and B are friends and B and C are friend.
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?
 
Inputthere are several test cases. First is a integer T, means the number of test cases.
Each test case starts with three integer n, m and F in a line (3 <= n <= 100,0 <m <n * n, 0 <= F <n ). N means there are 2 * n Children, N girls (number from 1 to n) and N boys (number from 1 to n ).
Then M lines follow. Each line contains two numbers a and B, means Girl A and boy B had never quarreled with each other.
Then f lines follow. Each line contains two numbers C and D, means girl C and girl d are good friends.
 
Outputfor each case, output a number in one line. The maximal number of marriage match the children can play.
Sample Input
14 5 21 12 33 24 24 41 42 3
 
Sample output
2
 
Authorstarvae
Sourcehdu 2nd "vegetable-birds Cup" programming open contest
The question is relatively simple:

It is to give you n boys and N girls to pair with each other (men and women) to play home games. The pairing condition is that a woman and a man are friends or that A and B are friends. A and A are friends. A can also be paired. After playing a family game, the two cannot be paired together. Try to play a few rounds of home games at most.

Ideas:

At the beginning of this question, I couldn't solve the problem of friendship between female and female. Then I came up with other people's ideas to use and check the set. After each pairing, check whether N pairs can be reached. If yes, add 1 to the number of pairs, delete the paired edge, and then perform the next pairing, if the number of Matched persons is less than N, the matching exists.


The code and explanations are as follows:

# Include <cstdio> # include <cstring> # include <iostream> # include <sstream> # include <algorithm> # include <vector> # include <bitset> # include <set> # include <queue> # include <stack> # include <map> # include <cstdlib> # include <cmath> # define Pi 2 * asin (1.0) const int mod = 1e9 + 7; const int n = 1E2 + 15; const int INF = (1 <30)-1; using namespace STD; int N, M, F; int A, B; int G [N] [N]; int father [N]; // combine int find (INT x) with the query set) {If (x = Father [x]) return X; return x = find (father [x]);} void Union (int A, int B) {int AA = find (a); int BB = find (B); If (Aa! = Bb) Father [BB] = AA;} void Init () {for (INT I = 1; I <= N; I ++) Father [I] = I; memset (G, 0, sizeof (g);} // perform binary matching for int CX [N], CY [N]; /// match int SX [N], Sy [N]; int path (int u) {SX [u] = 1; for (INT I = 1; I <= N; I ++) {If (G [u] [I] &! Sy [I]) {Sy [I] = 1; // If I does not match, or I matches, however, from CY [I], we can find the augmented path if (CY [I] =-1 | path (CY [I]). {// find CX [u] = I, CY [I] = u; return 1; // find augmented path }}return 0 ;} /// Delete the matched edge void del () {for (INT I = 1; I <= N; I ++) G [I] [CX [I] = 0;} int maxflow () {int ans = 0; while (1) {memset (CX,-1, sizeof (CX); memset (CY,-1, sizeof (CY); int flow = 0; For (INT I = 1; I <= N; I ++) {memset (sx, 0, sizeof (SX); memset (SY, 0, sizeof (SY); If (CX [I] =-1) flow + = path (I); // no matching results} // If (flow = N) {ans ++; del ();} else break;} return ans;} int main () {int TC; scanf ("% d", & TC); While (TC --) {scanf ("% d", & N, & M, & F); Init (); For (INT I = 0; I <m; I ++) {scanf ("% d", & A, & B); G [a] [B] = 1 ;}// combine friend relationships, for better search (INT I = 0; I <F; I ++) {scanf ("% d", & A, & B ); union (a, B) ;}for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {if (I = J) continue; If (find (I) = find (j) {// friend relationship, shared for (int K = 1; k <= N; k ++) {If (G [I] [k]) G [J] [k] = 1 ;}} printf ("% d \ n", maxflow () ;}return 0 ;}

This is a good question. It involves and searches for binary matching. You can also use the network stream to solve this problem.

Hdu3081 binary 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.