Marriage Match II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2455 Accepted Submission(s): 837
Problem Description
Presumably, 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 many friends 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?
Input
There 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.
Output
For each case, output a number in one line. The maximal number of Marriage Match the children can play.
Sample Input
1 4 5 2 1 1 2 3 3 2 4 2 4 4 1 4 2 3
Sample Output
2
Author
starvae
Source
HDU 2nd “Vegetable-Birds Cup” Programming
Open Contest
Recommend
lcy | We have carefully selected several similar problems for you: 3416 3277 3376 3491 1569
题意:n个女孩n个男孩,女孩选男朋友,如果两个女孩认识,那她们可以在自己认识的或对方认识的男孩中任选一个,选完一轮后打乱重新选,已经配对过的不能再选在一起,问最后最多能选几轮。
思路:先用并查集,再建图进行二分匹配,一次匹配完后将匹配的边去掉再进行匹配,直到匹配数<n。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 105 #define MAXN 2005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FREE(i,a,b) for(i = a; i >= b; i--) #define FRL(i,a,b) for(i = a; i < b; i++) #define FRLL(i,a,b) for(i = a; i > b; i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; int n,m,f; int g[maxn][maxn]; int linker[maxn]; bool used[maxn]; int father[maxn]; void init() { memset(g,0,sizeof(g)); for (int i=0;i<maxn;i++) father[i]=i; } int find_father(int x) { if (x!=father[x]) father[x]=find_father(father[x]); return father[x]; } void Union(int a,int b) { int fa=find_father(a); int fb=find_father(b); if (fa!=fb) father[fa]=fb; } bool dfs(int u) { for (int v=1;v<=n;v++) { if (g[u][v]&&!used[v]) { used[v]=true; if (linker[v]==-1||dfs(linker[v])) { linker[v]=u; return true; } } } return false; } int solve() { int ans=0; while (1) { int res=0; memset(linker,-1,sizeof(linker)); for (int u=1;u<=n;u++) { memset(used,false,sizeof(used)); if (dfs(u)) res++; } // printf("res=%d\n",res); if (res<n) break; for (int i=1;i<=n;i++) g[linker[i]][i]=0; ans++; } return ans; } int main() { int i,j,t,a,b,k; scanf("%d",&t); while (t--) { init(); scanf("%d%d%d",&n,&m,&f); for (i=0;i<m;i++) { scanf("%d%d",&a,&b); g[a][b]=1; } for (i=0;i<f;i++) { scanf("%d%d",&a,&b); Union(a,b); } for (i=1;i<=n;i++) { int x=find_father(i); for (j=1;j<=n;j++) { if (j!=i&&find_father(j)==x) { for (k=1;k<=n;k++) { if (g[j][k]) g[i][k]=1; } } } } printf("%d\n",solve()); } return 0; }