hdu3081 Marriage Match II(最大流)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Marriage Match II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2410 Accepted Submission(s): 820
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?
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.
题意:
有n个女孩和n个男孩,有m个女孩与男孩的关系,代表女孩喜欢男孩,有f个女孩与女孩的关系,代表女孩与女孩是好朋友。
若女孩i与女孩j是好朋友,而女孩i喜欢男孩k,则女孩j也可以和男孩匹配。即女孩之间的关系是可以传递的,而男孩之间的不可以。
每对男孩与女孩之间只能匹配一次,问可以进行几轮配对。
分析:
先floyd搞好女孩之间的传递关系,然后对于可以配对的女孩与男孩之间连一条容量为1的边,然后二分答案,每次二分之后从源点向各个女孩连容量为二分值的边,从各个男孩向汇点连容量为二分值的边。
1 //##################### 2 //Author:fraud 3 //Blog: http://www.cnblogs.com/fraud/ 4 //##################### 5 #include <iostream> 6 #include <sstream> 7 #include <ios> 8 #include <iomanip> 9 #include <functional> 10 #include <algorithm> 11 #include <vector> 12 #include <string> 13 #include <list> 14 #include <queue> 15 #include <deque> 16 #include <stack> 17 #include <set> 18 #include <map> 19 #include <cstdio> 20 #include <cstdlib> 21 #include <cmath> 22 #include <cstring> 23 #include <climits> 24 #include <cctype> 25 using namespace std; 26 #define XINF INT_MAX 27 #define INF 0x3FFFFFFF 28 #define MP(X,Y) make_pair(X,Y) 29 #define PB(X) push_back(X) 30 #define REP(X,N) for(int X=0;X<N;X++) 31 #define REP2(X,L,R) for(int X=L;X<=R;X++) 32 #define DEP(X,R,L) for(int X=R;X>=L;X--) 33 #define CLR(A,X) memset(A,X,sizeof(A)) 34 #define IT iterator 35 typedef long long ll; 36 typedef pair<int,int> PII; 37 typedef vector<PII> VII; 38 typedef vector<int> VI; 39 #define MAXN 1010 40 struct edge{ 41 int to,cap,rev; 42 edge(int _to,int _cap,int _rev) 43 { 44 to=_to; 45 cap=_cap; 46 rev=_rev; 47 } 48 }; 49 const int MAX_V=5020; 50 vector<edge>G[MAX_V]; 51 int iter[MAX_V]; 52 int head[MAXN]; 53 int _to[510*510]; 54 int _flow[510*510]; 55 int _next[510*510]; 56 int level[MAX_V]; 57 int tot=0; 58 void add_edge(int from,int to,int cap) 59 { 60 G[from].PB(edge(to,cap,G[to].size())); 61 G[to].PB(edge(from,0,G[from].size()-1)); 62 } 63 void Add(int u,int v,int f){ 64 _to[tot]=v; 65 _flow[tot]=f; 66 _next[tot]=head[u]; 67 head[u]=tot++; 68 } 69 void bfs(int s,int t) 70 { 71 CLR(level,-1); 72 queue<int>q; 73 level[s]=0; 74 q.push(s); 75 while(!q.empty()) 76 { 77 int u=q.front(); 78 q.pop(); 79 for(int i=0;i<G[u].size();i++) 80 { 81 edge &e=G[u][i]; 82 if(e.cap>0&&level[e.to]<0) 83 { 84 level[e.to]=level[u]+1; 85 q.push(e.to); 86 } 87 } 88 } 89 } 90 int dfs(int v,int t,int f) 91 { 92 if(v==t)return f; 93 for(int &i=iter[v];i<G[v].size();i++) 94 { 95 edge &e=G[v][i]; 96 if(e.cap>0&&level[v]<level[e.to]) 97 { 98 int d=dfs(e.to,t,min(f,e.cap)); 99 if(d>0) 100 { 101 e.cap-=d;; 102 G[e.to][e.rev].cap+=d; 103 return d; 104 } 105 } 106 } 107 return 0; 108 } 109 int Dinic(int s,int t) 110 { 111 int flow=0; 112 for(;;) 113 { 114 bfs(s,t); 115 if(level[t]<0)return flow; 116 memset(iter,0,sizeof(iter)); 117 int f; 118 while((f=dfs(s,t,INF))>0) 119 { 120 flow+=f; 121 } 122 } 123 } 124 125 int a[210][210]; 126 127 int main() 128 { 129 ios::sync_with_stdio(false); 130 int t; 131 scanf("%d",&t); 132 while(t--){ 133 int n,m,f; 134 scanf("%d%d%d",&n,&m,&f); 135 tot=0; 136 for(int i=0;i<MAXN;i++)head[i]=-1; 137 int u,v; 138 memset(a,0,sizeof(a)); 139 for(int i=0;i<m;i++){ 140 scanf("%d%d",&u,&v); 141 a[u][v+n]=1; 142 } 143 for(int i=0;i<f;i++){ 144 scanf("%d%d",&u,&v); 145 a[u][v]=1; 146 a[v][u]=1; 147 } 148 for(int i=1;i<=n+n;i++)a[i][i]=1; 149 for(int k=1;k<=n+n;k++){ 150 for(int i=1;i<=n+n;i++){ 151 for(int j=1;j<=n+n;j++){ 152 a[i][j]=max(a[i][j],a[i][k]&a[k][j]); 153 } 154 } 155 } 156 for(int i=1;i<=n;i++){ 157 for(int j=1+n;j<=n+n;j++){ 158 if(a[i][j])Add(i,j,1); 159 } 160 } 161 int l=0,r=n; 162 int s=0,t=2*n+1; 163 int ans=0; 164 while(l<=r){ 165 int mid=(l+r)>>1; 166 for(int i=0;i<=2*n+1;i++)G[i].clear(); 167 for(int i=1;i<=2*n;i++){ 168 int now=head[i]; 169 while(now!=-1){ 170 add_edge(i,_to[now],_flow[now]); 171 now=_next[now]; 172 } 173 } 174 for(int i=1;i<=n;i++){ 175 add_edge(s,i,mid); 176 add_edge(n+i,t,mid); 177 } 178 if(Dinic(s,t)==mid*n){ 179 ans=mid; 180 l=mid+1; 181 } 182 else r=mid-1; 183 } 184 printf("%d\n",ans); 185 } 186 187 188 189 return 0; 190 }
代码君
转载于:https://www.cnblogs.com/fraud/p/4354766.html
hdu3081 Marriage Match II(最大流)相关推荐
- HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流...
题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others) ...
- 【HDU - 3081】Marriage Match II(网络流最大流,二分+网络流)
题干: Presumably, you all have known the question of stable marriage match. A girl will choose a boy; ...
- [kuangbin带你飞]专题十一 网络流\N HDU 3081 Marriage Match II
题目描述 Presumably, you all have known the question of stable marriage match. A girl will choose a boy; ...
- HDU 3081 Marriage Match II【并查集+二分图最大匹配】
大意:有n个男孩n个女孩,告诉你每个女孩喜欢哪些男孩,又告诉你女孩之间的存在一些朋友关系 一个女孩可以和她喜欢的男孩结婚也可以和她朋友喜欢的男孩结婚, 并且朋友关系可以传递 Once every gi ...
- HDU - 3081 Marriage Match II(二分+并查集+最大流/匈牙利删边)
题目链接:点击查看 题目大意:n个男生和n个女生配对,配对规则如下: 每个女生都可以选择没有吵过架的男生匹配 若女生A的好朋友是女生B,且女生B没有和男生C吵过架,则女生A也可以和男生C匹配 现在问最 ...
- HDU 3081 Marriage Match II (并查集+二分+最大流 | 并查集+二分图匹配)
题意:n 个男生.n个女生玩游戏,每个女生都可以和她不讨厌的男生结婚,此外她的朋友如果也不讨厌这个男生,也可以和他结婚:对于女生,如果A和B是朋友,B和C是朋友,那么A和C也是朋友.每次游戏女生会找一 ...
- HDU - 3416 Marriage Match IV(最大流+最短路)
题目链接:点击查看 题目大意:给出一个由n个点和m条边组成的无环有向图,有自环,有重边,现在给出起点st和终点ed,问从st到ed共有多少条最短路,每条路最多只能经过一次 题目分析:看完题意后感觉是要 ...
- HDU - 3081 Marriage Match II 【二分匹配】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意 有n对男女 女生去选男朋友 如果女生从来没和那个男生吵架 那么那个男生就可以当她男朋友 女 ...
- HDU 3277 Marriage Match III(并查集+二分+最大流)
题意:和HDU3081一样的题意,只不过多了一个条件,每个女孩除了能选自己喜欢的男生之外,还能选不超过K个自己不喜欢的男生,问游戏最多能进行几轮 思路:除了选喜欢的,还能选任意K个不喜欢的,怎么建图呢 ...
最新文章
- ccot 目标跟踪全称_Siamese:CVPR 2019 接收论文作者为你解读视频跟踪领域 | CVPR 2019...
- DI 之 3.4 Bean的作用域(捌)
- .hpp文件_文件上传漏洞另类绕过技巧及挖掘案例全汇总
- java ip归属地查询_JAVA版IP地址查询调用示例
- 快速上手Google C++ 测试框架googletest
- 北京证券交易所首批三大基本业务规则开始征求意见
- 独立游戏人:像素风格游戏制作分享(转)
- 王道训练营3月13日
- 基于C# winform的操作系统课程设计:SPOOLING假脱机输入输出技术模拟
- ardupilot rover ardurover 电机相关源码 PreArm servo function 33 unassigned
- 使用FOP将xml转换pdf
- 冰蝎Behinder_v4.0
- 观点丨如何让劳动价值像资本一样自由流动与交易
- 斗鱼直播Android开发二面被刷,赶紧收藏!
- 不降价的促销利器--让渡营销
- SELECT高级查询——连接查询、子查询(多表数据查询)
- 技术手段VBA之爬虫
- matlab最后一步出错,最后一条错误消息和相关信息
- 小豆苗服务器维护,小豆苗问题排查方法新版课件.pptx
- 项目奖金一般是多少_MPAcc职业发展|看看国内券商、投行、四大一年能挣多少?...