codeforces 847J Students Initiation 网络流+二份答案
来源:互联网 发布:w7怎么添加网络打印机 编辑:程序博客网 时间:2024/06/13 17:31
Soon the first year students will be initiated into students at the University of Berland. The organizers of the initiation come up with a program for this holiday. In their opinion, it would be good if the first-year students presented small souvenirs to each other. When they voiced this idea to the first-year students, they found out the following:
- some pairs of the new students already know each other;
- each new student agrees to give souvenirs only to those with whom they are already familiar;
- each new student does not want to present too many souvenirs.
The organizers have written down all the pairs of first-year friends who are familiar with each other and now want to determine for each new student, whom they should give souvenirs to. In their opinion, in each pair of familiar students exactly one student must present a souvenir to another student.
First year students already decided to call the unluckiest the one who will have to present the greatest number of souvenirs. The organizers in return promised that the unluckiest will be unlucky to the minimum possible degree: of course, they will have to present the greatest number of souvenirs compared to the other students, but this number will be as small as possible.
Organizers are very busy, and they asked you to determine for each pair of first-year friends who and to whom should present a souvenir.
The first line contains two integers n and m (1 ≤ n ≤ 5000, 0 ≤ m ≤ min(5000, n·(n - 1) / 2)) — the number of the first year students and the number of pairs of the students that know each other. The students are numbered from 1 to n.
Each of the following m lines contains two integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi) — the students in each pair.
It is guaranteed that each pair is present in the list exactly once. It is also guaranteed that if there is a pair (xi, yi) in the list, then there is no pair (yi, xi).
Print a single integer into the first line — the smallest number of souvenirs that the unluckiest student will have to present.
Following should be m lines, each containing two integers — the students which are familiar with each other. The first number in the pair must be the student that will present the souvenir to the second student in the pair.
Pairs can be printed in any order. If there are many solutions, print any of them.
题解:先随便构建一张有向图,边的容量为1,然后二份答案ans。
对于所有的点,如果该点的outdegree > ans 那么,从源点向这个点建立一条容量为outdegree - ans的边。
如果该点的outdegree < ans 那么,从这个点向汇点建立一条容量为ans-outdegree的边的边。
然后跑一边最大流,如果满流,说明判定成功。
含义:如果一个边上有流量,代表该边被反向。依据这个性质,可以打印出最后图中边的指向。
代码:
#include <bits/stdc++.h>using namespace std;const int inf=1e9;const int maxm=21111;const int maxn=5001;int node,src,dest,edge;int ver[maxm],flow[maxm],nxt[maxm];int head[maxn],work[maxn],dis[maxn],q[maxn];void prepare(int _node,int _src,int _dest){ node=_node,src=_src,dest=_dest; for(int i=0; i<node; ++i)head[i]=-1; edge=0;}void add_edge(int u,int v,int c){ ver[edge]=v,flow[edge]=c,nxt[edge]=head[u],head[u]=edge++; ver[edge]=u,flow[edge]=0,nxt[edge]=head[v],head[v]=edge++;}bool Dinic_bfs(){ int i,u,v,l,r=0; for(i=0; i<node; ++i)dis[i]=-1; dis[q[r++]=src]=0; for(l=0; l<r; ++l) for(i=head[u=q[l]]; i>=0; i=nxt[i]) if(flow[i]&&dis[v=ver[i]]<0) { dis[q[r++]=v]=dis[u]+1; if(v==dest)return 1; } return 0;}int Dinic_dfs(int u,int exp){ if(u==dest)return exp; for(int &i=work[u],v,tmp; i>=0; i=nxt[i]) if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0) { flow[i]-=tmp; flow[i^1]+=tmp; return tmp; } return 0;}int Dinic_flow(){ int i,ret=0,delta; while(Dinic_bfs()) { for(i=0; i<node; ++i)work[i]=head[i]; while(delta=Dinic_dfs(src,inf))ret+=delta; } return ret;}typedef pair<int,int> P;P ps[maxm];int n,m;int indeg[maxn],outdeg[maxn];bool check(int mid){int exp = 0;memset(indeg,0,sizeof(indeg));memset(outdeg,0,sizeof(outdeg));prepare(n+2,0,n+1);for(int i = 0;i < m;++i){int u = ps[i].first,v = ps[i].second;outdeg[u]++;indeg[v]++;add_edge(u,v,1);}for(int i = 1;i <= n;++i){if(outdeg[i] > mid){add_edge(0,i,outdeg[i] - mid);exp += outdeg[i] - mid;}if(outdeg[i] < mid){add_edge(i,n+1,mid - outdeg[i]);}}int f = Dinic_flow();return exp == f;}void printpath(){for(int i = 1;i <= n;i++){for(int e = head[i];e != -1;e = nxt[e]){if(e&1) continue;int v = ver[e];if(v < 1 || v > n || v < i) continue;if(flow[e]){printf("%d %d\n",i,v);}else{printf("%d %d\n",v,i);}}}}int main(){scanf("%d%d",&n,&m);for(int i = 0;i < m;++i){int a,b;scanf("%d%d",&a,&b);if(a > b) swap(a,b);ps[i] = make_pair(a,b);}int l = -1,r = n;while(r - l > 1){int mid = (l+r)/2;if(check(mid)){r = mid;}else{l = mid;}}if(check(l)) {cout<<l<<endl;}else{check(r);cout<<r<<endl;}printpath();return 0;}
- codeforces 847J Students Initiation 网络流+二份答案
- poj2455 Secret Milking Machine 网络流 最大流 二份答案
- UVA 11107 Life Forms (后缀数组 + 二份答案)
- CodeForces 332C Students' Revenge
- UVALive 3211 Now or later(二份答案+2-SAT)
- 又收到几份答案
- Codeforces Round #454 D. Seating of Students
- CodeForces 44J Triminoes
- CodeForces - 630J Divisibility
- CodeForces-630 J. Divisibility
- Codeforces 630J Divisibility
- codeforces 589J
- codeforces J. Spelling Check
- codeforces 589J
- codeforces 730 J. Bottles
- codeforces 730J - Bottles
- codeforces 730 J Bottles
- Codeforces 630J
- Linux学习高级篇(二)
- java中栈、堆、静态区的区别
- Linux学习高级篇(四)
- 知道mysql的索引吗?
- Linux学习高级篇(六)
- codeforces 847J Students Initiation 网络流+二份答案
- leetcode Merge Sorted Array 合并有序数组
- cmake初探
- linux系统安装pip,numpy,matplotlib,scipy,tensorflow命令
- tomcat启动容器失败报error in opening zip file错误
- 值得学习的C/C++开源项目
- 最简单的 RabbitMQ 监控方法
- Linux学习高级篇(五)
- 谈谈我对懒加载的一点看法 | Objective-C