HDU 3081 Marriage Match II
来源:互联网 发布:喷涂机器人及软件编程 编辑:程序博客网 时间:2024/06/05 04:14
Marriage Match II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3837 Accepted Submission(s): 1255
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?
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
二分求轮数,Dinic算法模板
1 #include <iostream> 2 #include <cstring> 3 #include <vector> 4 #include <queue> 5 #define maxn 210 6 #define INF 0X3fffffff 7 using namespace std; 8 struct Edge 9 { 10 int from,to,cap,flow; 11 Edge(){} 12 Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){} 13 }; 14 15 struct Dinic 16 { 17 int n,m,s,t; 18 vector<Edge> edges; 19 vector<int> G[maxn]; 20 bool vis[maxn]; 21 int cur[maxn]; 22 int d[maxn]; 23 24 void init(int n,int s,int t) 25 { 26 this->n=n, this->s=s, this->t=t; 27 edges.clear(); 28 for(int i=0;i<=n;++i) G[i].clear(); 29 } 30 31 void AddEdge(int from,int to,int cap) 32 { 33 edges.push_back( Edge(from,to,cap,0) ); 34 edges.push_back( Edge(to,from,0,0) ); 35 m=edges.size(); 36 G[from].push_back(m-2); 37 G[to].push_back(m-1); 38 } 39 40 bool BFS() 41 { 42 queue<int> Q; 43 memset(vis,0,sizeof(vis)); 44 vis[s]=true; 45 d[s]=0; 46 Q.push(s); 47 while(!Q.empty()) 48 { 49 int x=Q.front(); Q.pop(); 50 for(int i=0;i<G[x].size();++i) 51 { 52 Edge& e=edges[G[x][i]]; 53 if(!vis[e.to] && e.cap>e.flow) 54 { 55 vis[e.to]=true; 56 d[e.to]=d[x]+1; 57 Q.push(e.to); 58 } 59 } 60 } 61 return vis[t]; 62 } 63 64 int DFS(int x,int a) 65 { 66 if(x==t || a==0) return a; 67 int flow=0, f; 68 for(int &i=cur[x];i<G[x].size();++i) 69 { 70 Edge &e=edges[G[x][i]]; 71 if(d[e.to]==d[x]+1 && (f=DFS(e.to,min(a,e.cap-e.flow) ) )>0) 72 { 73 e.flow +=f; 74 edges[G[x][i]^1].flow -=f; 75 flow +=f; 76 a -=f; 77 if(a==0) break; 78 } 79 } 80 return flow; 81 } 82 83 int max_flow() 84 { 85 int ans=0; 86 while(BFS()) 87 { 88 memset(cur,0,sizeof(cur)); 89 ans +=DFS(s,INF); 90 } 91 return ans; 92 } 93 }DC; 94 int parnet[maxn]; 95 bool flag[maxn][maxn]; 96 int find(int n){ 97 if(n==parnet[n]){ 98 return n; 99 }100 else return parnet[n]=find(parnet[n]);101 }102 void Union(int x,int y){103 int xx=find(x);104 int yy=find(y);105 if(xx!=yy){106 parnet[xx]=yy;107 }108 }109 bool solve(int n,int limit){110 int start=0,end=2*n+1;111 DC.init(n*2+2,start,end);112 for(int i=1;i<=n;i++){113 for(int j=1;j<=n;j++){114 if(flag[i][j]){115 DC.AddEdge(i,j+n,1);116 }117 }118 }119 for(int i=1;i<=n;i++){120 DC.AddEdge(i+n,end,limit);121 }122 for(int i=1;i<=n;i++){123 DC.AddEdge(start,i,limit);124 }125 return DC.max_flow()==n*limit;126 }127 void init(int n){128 for(int i=1;i<=n;i++){129 parnet[i]=i;130 }131 memset(flag,0,sizeof(flag));132 }133 int main(){134 int n,m,f,T,a,b;135 cin.sync_with_stdio(false);136 cin>>T;137 while(T--){138 cin>>n>>m>>f;139 init(n);140 while(m--){141 cin>>a>>b;142 flag[a][b]=true;143 }144 while(f--){145 cin>>a>>b;146 Union(a,b);147 }148 for(int i=1;i<=n;i++){149 for(int j=i+1;j<=n;j++){150 if(find(i)==find(j)){151 for(int k=1;k<=n;k++){152 flag[i][k]=flag[j][k]=(flag[i][k]||flag[j][k]);153 }154 }155 }156 }157 int l=0,r=n;158 while(l<r){159 int mid=(l+r+1)/2;160 if(solve(n,mid)){161 l=mid;162 }163 else{164 r=mid-1;165 }166 }167 cout<<l<<endl;168 }169 return 0;170 }
2017-03-06 12:12:51
阅读全文
1 0
- HDU 3081 Marriage Match II
- HDU 3081 Marriage Match II
- hdu 3081 Marriage Match II
- HDU-3081-Marriage Match II
- HDU 3081 Marriage Match II
- HDU 3081 Marriage Match II
- hdu 3081 Marriage Match II
- HDU-3081 Marriage Match II
- 【HDU】3081 Marriage Match II
- HDU 3081 Marriage Match II
- Marriage Match II HDU
- HDU 3081 Marriage Match II 并查集加网络流
- HDU--3081--Marriage Match II--最大匹配,匈牙利算法
- HDU 3081 Marriage Match II 二分+最大流
- HDU 3081 Marriage Match II(二分+最大流)
- hdu 3081 Marriage Match II isap+二分+并查集
- HDU - 3081 Marriage Match II(二分+最大流)
- HDU 3081 Marriage Match II 网络流+二分
- 之江学院2017ACM 校赛Problem G: qwb去面试
- JDBC2
- HDU 2236 无题II(二分匹配模板题,匈牙利算法)
- HDU 1269 迷宫城堡(强连通模板题)
- HDU 3594 Cactus(仙人掌图模板)
- HDU 3081 Marriage Match II
- HDU 1533 Going Home(KM算法模板样例)
- VJ Pots
- Jxnu Group Programming Ladder Tournament 2017题解
- Recycleview的实例及于listview的强弱分析
- C++栈的应用——后缀表达式求值、中缀表达式到后缀表达式的转换
- Qt按键值与Windows Virtual-Key Codes映射表
- BrnShop开源网上商城第一讲:架构设计
- 23种设计模式汇总整理