HDU 3081 Marriage Match II
Marriage Match II
Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 308164-bit integer IO format: %I64d Java class name: Main
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?
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.
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
Source
解题:求最大匹配数的种数,就是求最大匹配的方案数,姿势?二分+最大流。。。二超级源点到女生的弧的容量,表示女生最多可以同几名男生。。。oo...
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 300; 18 struct arc { 19 int to,flow,next; 20 arc(int x = 0,int y = 0,int z = -1) { 21 to = x; 22 flow = y; 23 next = z; 24 } 25 }; 26 arc e[100000]; 27 bool g[maxn][maxn]; 28 int head[maxn],d[maxn],cur[maxn]; 29 int S,T,tot,n,m,f; 30 void add(int u,int v,int flow) { 31 e[tot] = arc(v,flow,head[u]); 32 head[u] = tot++; 33 e[tot] = arc(u,0,head[v]); 34 head[v] = tot++; 35 } 36 void Floyd() { 37 for(int k = 1; k <= n+n; ++k) { 38 for(int i = 1; i <= n+n; ++i) { 39 for(int j = 1; j <= n+n; ++j) { 40 if(i == j || i == k || j == k) continue; 41 if(!g[i][j]) g[i][j] = g[i][k]&&g[k][j]; 42 } 43 } 44 } 45 } 46 bool bfs() { 47 queue<int>q; 48 memset(d,-1,sizeof(d)); 49 d[S] = 1; 50 q.push(S); 51 while(!q.empty()) { 52 int u = q.front(); 53 q.pop(); 54 for(int i = head[u]; ~i; i = e[i].next) { 55 if(e[i].flow && d[e[i].to] == -1) { 56 d[e[i].to] = d[u] + 1; 57 q.push(e[i].to); 58 } 59 } 60 } 61 return d[T] > -1; 62 } 63 int dfs(int u,int low) { 64 if(u == T) return low; 65 int tmp = 0,a; 66 for(int &i = cur[u]; ~i; i = e[i].next) { 67 if(e[i].flow && d[e[i].to] == d[u] + 1 &&(a=dfs(e[i].to,min(e[i].flow,low)))) { 68 e[i].flow -= a; 69 e[i^1].flow += a; 70 low -= a; 71 tmp += a; 72 if(!low) break; 73 } 74 } 75 if(!tmp) d[u] = -1; 76 return tmp; 77 } 78 int dinic() { 79 int tmp = 0; 80 while(bfs()) { 81 memcpy(cur,head,sizeof(head)); 82 tmp += dfs(S,INF); 83 } 84 return tmp; 85 } 86 void build(int delta) { 87 memset(head,-1,sizeof(head)); 88 tot = 0; 89 for(int i = 1; i <= n; ++i) { 90 add(S,i,delta); 91 add(i+n,T,delta); 92 } 93 for(int i = 1; i <= n; ++i){ 94 for(int j = n+1; j <= n+n; ++j){ 95 if(g[i][j]) add(i,j,1); 96 } 97 } 98 } 99 int main() { 100 int cs,u,v; 101 scanf("%d",&cs); 102 while(cs--) { 103 scanf("%d %d %d",&n,&m,&f); 104 S = 0; 105 T = n<<1|1; 106 memset(g,false,sizeof(g)); 107 for(int i = 0; i < m; ++i) { 108 scanf("%d %d",&u,&v); 109 g[u][v+n] = true;//g[v+n][u] = true; 110 //居然是有向的。。。为什么呢? 111 } 112 for(int i = 0; i < f; ++i) { 113 scanf("%d %d",&u,&v); 114 g[u][v] = g[v][u] = true; 115 } 116 Floyd(); 117 int low = 0,high = n,ans = 0; 118 while(low <= high){ 119 int mid = (low + high) >>1; 120 build(mid); 121 int tmp = dinic(); 122 if(tmp == n*mid){ 123 ans = mid; 124 low = mid+1; 125 }else high = mid-1; 126 } 127 printf("%d\n",ans); 128 } 129 return 0; 130 }
夜空中最亮的星,照亮我前行