HDU_3277_Marriage Match III(最大流)
来源:互联网 发布:网络成瘾症如何治疗 编辑:程序博客网 时间:2024/06/05 08:26
Marriage Match III
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1721 Accepted Submission(s): 502
Total Submission(s): 1721 Accepted Submission(s): 502
Problem Description
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the ever game of play-house . What a happy time as so many friends play 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. As 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. On the other hand, in order to play more times of marriage match, every girl can accept any K boys. If a girl chooses a boy, the boy must accept her unconditionally whether they had quarreled before or not.
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. As 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. On the other hand, in order to play more times of marriage match, every girl can accept any K boys. If a girl chooses a boy, the boy must accept her unconditionally whether they had quarreled before or not.
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 an integer T, means the number of test cases.
Each test case starts with three integer n, m, K and f in a line (3<=n<=250, 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, K and f in a line (3<=n<=250, 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
14 5 1 21 12 33 24 24 41 42 3
Sample Output
3
题意:跟HDU 3081差不多,就是多了一个条件,每个女孩可以另外选K个不喜欢的男生。
分析:既然题目就多了一个条件,那么建图的话自然和上一题差不多,【点击这里查看HDU 3081构图法】,只要把每个女生拆成两个女生girl_1,girl_2,若girl_1对boy_1不排斥,则连边gilr_1 -> boy_1,容量为1;若girl_1不喜欢boy_1,则连边girl_2 -> boy_1,容量为1,girl_1 -> girl_2,容量为K。然后二分找答案,最大流判可行度即可。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3277
代码清单:
#include<map>#include<set>#include<cmath>#include<queue>#include<stack>#include<cctype>#include<string>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespace std;#define end() return 0typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;const int maxN = 250 + 5;const int maxn = 1000 + 5;const int maxv = 100000 + 5;const int INF = 0x7f7f7f7f;struct Edge{ int from,to,cap,flow; Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}};struct dinic{ int n,m,s,t; //结点数,边数(包括反向弧),源点,汇点 vector<Edge>edge;//边表。edge[e]和edge[e^1]互为反向弧 vector<int>G[maxn];//邻接表。G[i][j]表示结点i的第j条边在e数组的序号 bool vis[maxn]; //bfs用 int d[maxn]; //从起点到i的距离 int cur[maxn]; //当前弧下标 void init(int n,int s,int t){ this -> n = n; this -> s = s; this -> t = t; for(int i=0;i<=n;i++) G[i].clear(); edge.clear(); } void addEdge(int from,int to,int cap){ edge.push_back(Edge(from,to,cap,0)); edge.push_back(Edge(to,from,0,0)); m=edge.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool bfs(){ memset(vis,false,sizeof(vis)); queue<int>q; q.push(s); d[s]=0; vis[s]=true; while(!q.empty()){ int x=q.front();q.pop(); for(int i=0;i<G[x].size();i++){ Edge& e=edge[G[x][i]]; if(!vis[e.to]&&e.cap>e.flow){ //只考虑残量网络中的弧 vis[e.to]=true; d[e.to]=d[x]+1; q.push(e.to); } } } return vis[t]; } int dfs(int x,int a){ if(x==t||a==0) return a; int flow=0,f; for(int& i=cur[x];i<G[x].size();i++){ // & -> 从上次考虑的弧 Edge& e=edge[G[x][i]]; if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0){ e.flow+=f; edge[G[x][i]^1].flow-=f; flow+=f; a-=f; if(a==0) break; } } return flow; } int maxflow(){ int flow=0; while(bfs()){ memset(cur,0,sizeof(cur)); flow+=dfs(s,INF); } return flow; }};int T;int N,M,K,F,X,Y;int a[maxv],b[maxv];bool vis[maxN][maxN];int tail;dinic dc;int father[maxN];int Find(int x){ return father[x]!=x ? father[x]=Find(father[x]) : father[x]; }void init(){ for(int i=0;i<maxN;i++){ father[i]=i; } memset(vis,false,sizeof(vis));}void input(){ scanf("%d%d%d%d",&N,&M,&K,&F); for(int i=1;i<=M;i++){ scanf("%d%d",&a[i],&b[i]); } for(int i=0;i<F;i++){ scanf("%d%d",&X,&Y); father[Find(Y)]=Find(X); }}int work(){ for(int i=1;i<=M;i++){ for(int j=1;j<=N;j++){ if(Find(j)==Find(a[i])&&!vis[j][b[i]]){ //判重 vis[j][b[i]]=true; } } } tail=3*N+1; int le=0,ri=N; int mid,ans=0; while(le<=ri){ mid=(le+ri)>>1; dc.init(tail+1,0,tail); for(int i=1;i<=N;i++){ dc.addEdge(0,i,mid); dc.addEdge(i,i+2*N,K); dc.addEdge(i+N,tail,mid); for(int j=1;j<=N;j++){ if(!vis[i][j]) dc.addEdge(i+2*N,j+N,1); else dc.addEdge(i,j+N,1); } } int flow=dc.maxflow(); if(flow==mid*N){ ans=mid; le=mid+1; } else ri=mid-1; } return ans;}void solve(){ printf("%d\n",work());}int main(){ scanf("%d",&T); while(T--){ init(); input(); solve(); }end();}
0 0
- HDU_3277_Marriage Match III(最大流)
- 【hdu】3277 Marriage Match III【最大流】
- HDU 3277 Marriage Match III(二分+最大流)
- HDU - 3277 Marriage Match III(并查集+最大流)
- hdoj 3277 Marriage Match III 【最大流经典建图】【二分 + 最大流 + 并查集】
- hdu 3081 hdu 3277 hdu 3416 Marriage Match II III IV //最大流的灵活运用
- HDU 3277 Marriage Match III(拆点+二分+最大流SAP)
- HDU 3277 Marriage Match III(并查集+二分+最大流)
- hdu 3277 Marriage Match III【最大流+并查集+二分枚举】
- HDU 3277 Marriage Match III (最大流+拆点+并查集)
- HDU 3277 Marriage Match III(并查集+二分答案+最大流SAP)拆点,经典
- HDU -- 3277 Marriage Match III(最大流+二分+并查集+拆点)
- HDU 3277 Marriage Match III(中等,好题)[二分最大流]+拆点
- HDU 3277 Marriage Match III(并查集+二分+最大流)
- hdu 3277 Marriage Match III(最大流,二分,并查集)
- hdu 3277 Marriage Match III【并查集+最大流Dinic+拆点+建图+二分查找】
- HDU 3277 Marriage Match III(并查集+二分+最大流)
- 【网络流】hdu3277 Marriage Match III
- 使用InjectView和findViewById说拜拜
- Sort List
- Linux 学习笔记一 :文件拆分和后台执行
- 青蛙跳台阶
- Linux与android进程间的通信及android Binder机制详解
- HDU_3277_Marriage Match III(最大流)
- android的内容提供者范例
- Volley网络通信框架、XUtils
- swift入门
- XHTML
- 为什么会出现死锁
- uva 11995 I Can Guess the Data Structure!
- hdu 5437Alisha’s Party(优先队列)
- [HDU 2544] 最短路 · 堆优化dijkstra