Marriage Match II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2794 Accepted Submission(s): 938
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
题意:
有 2*n个同学,n男n女,有 m 对男女之间木有吵过架(有关系),f 对女生互为朋友(有关系),现在这 n 个女生要找对象,要求没有和她吵过架或者没有和她朋友吵过架,
当 n 个女生都找到对象的时候算作一轮,然后重新找,满足每个女生都不能找和上次一样的对象,问最多能进行多少轮。(可以理解为经过多少轮游戏不存在完美匹配)。
解析:
首先把女生之间的关系传递闭包一下。有关系的男女之间建边,构造二分图。每次用匈牙利算法求一下最大匹配。若是完美匹配,删去当前的边,接着求最大匹配,直到不是完美匹配为止。
#include <cstdio> #include <cstring> #include <algorithm> #define maxn 110 using namespace std; int map[maxn][maxn];//男女关系 int node[maxn][maxn];//女生之间的朋友关系 int used[maxn]; int link[maxn]; int n, m, f; void init(){ memset(map, 0, sizeof(map)); memset(node, 0, sizeof(node)); } void floyd(){ for(int k = 1; k <= n; ++k) for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) node[i][j] = node[i][j] || (node[i][k] && node[k][j]); } void getmap(){ scanf("%d%d%d", &n, &m, &f); int a, b; while(m--){ scanf("%d%d", &a, &b); map[a][b] = 1; } while(f--){ scanf("%d%d", &a, &b); node[a][b] = node[b][a] = 1; } floyd(); for(int i = 1; i <= n; ++i){//枚举每一个女生 for(int j = 1; j <= n; ++j){ if(node[i][j]){//女生i , j之间为朋友关系 for(int k = 1; k <= n; ++k){ //若女生i 和男生 k 可以做伴侣, //则女生j 和男生 k 也可以做伴侣 if(map[i][k]) map[j][k] = 1; } } } } } bool dfs(int x){ for(int i = 1; i <= n; ++i){ if(map[x][i] && !used[i]){ used[i] = 1; if(link[i] == -1 || dfs(link[i])){ link[i] = x; return true; } } } return false; } int hungary(){ int ans = 0; memset(link, -1, sizeof(link)); for(int i = 1; i <= n; ++i){ memset(used, 0, sizeof(used)); if(dfs(i)) ans++; } return ans; } void solve(){ int num = 0; while(1){ int sum = hungary(); if(sum == n){ num++; for(int i = 1; i <= n; ++i) map[link[i]][i] = 0; } else break; } printf("%d\n", num); } int main (){ int T; scanf("%d", &T); while(T--){ init(); getmap(); solve(); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。