codeforces 847J Students Initiation 网络流+二份答案
2017-09-30 10:52
357 查看
J. Students Initiation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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-ye
4000
ar 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.
Input
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).
Output
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;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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-ye
4000
ar 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.
Input
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).
Output
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;
}
相关文章推荐
- poj2455 Secret Milking Machine 网络流 最大流 二份答案
- [欧拉回路 构造 || 网络流] Codeforces 723E #375 (Div. 2) E. One-Way Reform
- Codeforces 866C Gotta Go Fast - 动态规划 - 概率与期望 - 二分答案
- BZOJ 2406: 矩阵 [上下界网络流 二分答案]
- codeforces 812 C. Sagheer and Nubian Market(二分答案)
- Codeforces 670D2 Magic Powder - 2 二分答案
- bzoj 2406: 矩阵 二分答案+上下界网络流
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
- 洛谷P4382 [八省联考2018]劈配(网络流,二分答案)
- 网络流(最大流):CodeForces 499E Array and Operations
- CodeForces 204A - Little Elephant and Interval 算答案..易错..暴力打表对拍
- Codeforces 653D Delivery Bears【二分+网络流】
- CodeForces - 659E New Reform (网络流)求最少的入度为0的点的个数
- Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案
- 【暴力/网络流】[Codeforces - 739E]Gosha is hunting
- [二分答案 2-SAT验证 前后缀优化建图] Codeforces 587D #326 (Div. 1) D. Duff in Mafia
- codeforces 498C Array and Operations 网络流
- 网络流(最大流) CodeForces 546E:Soldier and Traveling
- bzoj3993 [SDOI2015]星际战争 二分答案+网络流检验
- CodeForces 785C Anton and Fairy Tale【二分答案+思维】