ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph (贪心或有源汇上下界网络流)
"Oh, There is a bipartite graph.""Make it Fantastic."
X wants to check whether a bipartite graph is a fantastic graph. He has two fantastic numbers, and he wants to let all the degrees to between the two boundaries. You can pick up several edges from the current graph and try to make the degrees of every point to between the two boundaries. If you pick one edge, the degrees of two end points will both increase by one. Can you help X to check whether it is possible to fix the graph?
Input
There are at most 30 test cases.
For each test case,The first line contains three integers N the number of left part graph vertices, M the number of right part graph vertices, and K the number of edges ( 1≤N≤2000,0≤M≤2000,0≤K≤6000). Vertices are numbered from 1 to N.
The second line contains two numbers L,R(0≤L≤R≤300). The two fantastic numbers.
Then K lines follows, each line containing two numbers U, V (1≤U≤N,1≤V≤M). It shows that there is a directed edge from U-th spot to V-th spot.
Note. There may be multiple edges between two vertices.
Output
One line containing a sentence. Begin with the case number. If it is possible to pick some edges to make the graph fantastic, output "Yes" (without quote), else output "No" (without quote).
样例输入
3 3 7
2 3
1 2
2 3
1 3
3 2
3 3
2 1
2 1
3 3 7
3 4
1 2
2 3
1 3
3 2
3 3
2 1
2 1
样例输出
Case 1: Yes
Case 2: No
题意
一个二分图,左边N个点,右边M个点,中间K条边,问你是否可以删掉边使得所有点的度数在[L,R]之间
题解
比赛的时候写的网络流A的,赛后把自己hack了。。
然后写了个贪心,发现还是贪心好写(雾)
考虑两个集合A和B,A为L<=d[i]<=R,B为d[i]>R
枚举每个边
1.如果u和v都在B集合,直接删掉
2.如果u和v都在A集合,无所谓
3.如果u在B,v在A,并且v可删边即d[v]>L
4.如果u在A,v在B,并且u可删边即d[u]>L
最后枚举N+M个点判断是否在[L,R]之间
这个做法虽然不是官方做法,如果有hack的数据可以发评论
最后贴个官方做法,有源汇上下界网络流
代码
#include<bits/stdc++.h>
using namespace std; const int maxn=; int main()
{
int N,M,K,L,R,o=,u[maxn],v[maxn],d[maxn];
while(scanf("%d%d%d",&N,&M,&K)!=EOF)
{
memset(d,,sizeof d);
scanf("%d%d",&L,&R);
int sum=,flag=;
for(int i=;i<K;i++)
{
scanf("%d%d",&u[i],&v[i]);v[i]+=N;
d[u[i]]++,d[v[i]]++;
}
for(int i=;i<K;i++)
{
int uu=u[i],vv=v[i];
if(d[uu]>R&&d[vv]>R)d[uu]--,d[vv]--;
else if(L<=d[uu]&&d[uu]<=R&&L<=d[vv]&&d[vv]<=R)continue;
else if(L+<=d[uu]&&d[uu]<=R&&d[vv]>R)d[uu]--,d[vv]--;
else if(d[uu]>R&&L+<=d[vv]&&d[vv]<=R)d[uu]--,d[vv]--;
}
for(int i=;i<=N+M;i++)if(d[i]<L||d[i]>R)flag=;
printf("Case %d: %s\n",o++,flag?"Yes":"No");
}
return ;
}
给一点测试数据,网上有的贪心过不去这些数据Yes Yes Yes Yes No
官方做法
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn=1e5+;
const int maxm=2e5+;
const int INF=0x3f3f3f3f; int TO[maxm],CAP[maxm],NEXT[maxm],tote;
int FIR[maxn],gap[maxn],cur[maxn],d[maxn],q[];
int n,m,S,T; void add(int u,int v,int cap)
{
//printf("i=%d u=%d v=%d cap=%d\n",tote,u,v,cap);
TO[tote]=v;
CAP[tote]=cap;
NEXT[tote]=FIR[u];
FIR[u]=tote++; TO[tote]=u;
CAP[tote]=;
NEXT[tote]=FIR[v];
FIR[v]=tote++;
}
void bfs()
{
memset(gap,,sizeof gap);
memset(d,,sizeof d);
++gap[d[T]=];
for(int i=;i<=n;++i)cur[i]=FIR[i];
int head=,tail=;
q[]=T;
while(head<=tail)
{
int u=q[head++];
for(int v=FIR[u];v!=-;v=NEXT[v])
if(!d[TO[v]])
++gap[d[TO[v]]=d[u]+],q[++tail]=TO[v];
}
}
int dfs(int u,int fl)
{
if(u==T)return fl;
int flow=;
for(int &v=cur[u];v!=-;v=NEXT[v])
if(CAP[v]&&d[u]==d[TO[v]]+)
{
int Min=dfs(TO[v],min(fl,CAP[v]));
flow+=Min,fl-=Min,CAP[v]-=Min,CAP[v^]+=Min;
if(!fl)return flow;
}
if(!(--gap[d[u]]))d[S]=n+;
++gap[++d[u]],cur[u]=FIR[u];
return flow;
}
int ISAP()
{
bfs();
int ret=;
while(d[S]<=n)ret+=dfs(S,INF);
return ret;
} int ca,N,M,Q,x,y,z,l[][],r[][];
char op[]; void init()
{
tote=;
memset(FIR,-,sizeof FIR);
}
int main()
{
int N,M,C,L,R,u,v,s,t,ca=;
while(scanf("%d%d%d",&N,&M,&C)!=EOF)
{
init();
int in[]={};
s=N+M+,t=s+,S=t+,T=S+,n=T;
add(t,s,INF);
scanf("%d%d",&L,&R);
for(int i=;i<C;i++)
{
scanf("%d%d",&u,&v);
add(u,N+v,);
}
for(int i=;i<=N;i++)
{
add(s,i,R-L);
in[s]-=L;
in[i]+=L;
}
for(int i=;i<=M;i++)
{
add(i+N,t,R-L);
in[i+N]-=L;
in[t]+=L;
}
int sum=;
for(int i=;i<=N+M+;i++)
{
if(in[i]>)
{
add(S,i,in[i]);
sum+=in[i];
}
else
add(i,T,-in[i]);
}
printf("Case %d: %s\n",ca++,sum==ISAP()?"Yes":"No");
}
return ;
}
ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph (贪心或有源汇上下界网络流)的更多相关文章
- ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph
"Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...
- ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph (上下界网络流)
正解: #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN=1 ...
- ACM-ICPC 2018 沈阳赛区网络预赛 F Fantastic Graph(贪心或有源汇上下界网络流)
https://nanti.jisuanke.com/t/31447 题意 一个二分图,左边N个点,右边M个点,中间K条边,问你是否可以删掉边使得所有点的度数在[L,R]之间 分析 最大流不太会.. ...
- ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph(有源上下界最大流 模板)
关于有源上下界最大流: https://blog.csdn.net/regina8023/article/details/45815023 #include<cstdio> #includ ...
- Fantastic Graph 2018 沈阳赛区网络预赛 F题
题意: 二分图 有k条边,我们去选择其中的几条 每选中一条那么此条边的u 和 v的度数就+1,最后使得所有点的度数都在[l, r]这个区间内 , 这就相当于 边流入1,流出1,最后使流量平衡 解析: ...
- ACM-ICPC 2018 沈阳赛区网络预赛-D:Made In Heaven(K短路+A*模板)
Made In Heaven One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. ...
- 图上两点之间的第k最短路径的长度 ACM-ICPC 2018 沈阳赛区网络预赛 D. Made In Heaven
131072K One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. Howe ...
- ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)
https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...
- ACM-ICPC 2018 沈阳赛区网络预赛-K:Supreme Number
Supreme Number A prime number (or a prime) is a natural number greater than 11 that cannot be formed ...
随机推荐
- 注解(annotation)
目录 JAVA注解 SpringMVC注解 RestEasy注解 JSON注解 java注解 SpringMVC注解 restEasy注解 Json注解: @JsonInclude(JsonIncl ...
- C#实现联合体
[StructLayout(LayoutKind.Explicit, Size = )] public struct TypeTransform { [FieldOffset()] public fl ...
- 正则表达式(Kotlin)
课题 使用正则表达式匹配字符串 使用正则表达式 "\d{3}-(\d{4})-\d{2}" 匹配字符串 "123-4567-89" 返回匹配结果:'" ...
- winform中datagridview刷新后的排序记忆
datagridview先点标题排序,但是重新刷新之后,还是变成窗体加载后的样子 我这里用定时器刷新的. 1.先定义三个全局变量 /// <summary> /// 需要排序的列和方向 / ...
- RPM安装命令总结
RPM安装命令总结 在 Linux 操作系统下,几乎所有的软件均通过RPM 进行安装.卸载及管理等操作.RPM 的全称为Redhat Package Manager ,是由Redhat 公司提出的,用 ...
- Pandas数据存取
pd.read_excel('foo.xlsx', 'Sheet1', index_col=None, na_values=['NA']) Pandas数据存取 Pandas可以存取多种介质类型数据, ...
- 源码编译安装Python3及问题解决
https://chowyi.com/%E6%BA%90%E7%A0%81%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85Python3%E5%8F%8A%E9%97%AE%E ...
- vue 巧妙的运用sass px 转 rem
<template> <div id="app"> <!-- <img src="./assets/logo.png"> ...
- 微信小程序 在canvas画布上划动,页面禁止滑动
要实现微信小程序 在canvas画布上划动,页面禁止滑动,不仅要设置disable-scroll="true",还要要给canvas绑定一个触摸事件才能生效. <canvas ...
- redis异常Redis:java.util.NoSuchElementException: Unable to validate object at
前两天项目上线的时候遇到了redis的一个问题,在测试环境的时候项目运行正常,项目一上线redis便开始抛异常. redis.clients.jedis.exceptions.JedisConnect ...