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 ...
随机推荐
- jianx vtritualbox 虚拟镜像的体积
https://blog.csdn.net/ganshuyu/article/details/46360271
- js 提示框的实现---组件开发之(一)
自己做了一个简单的提示框,供自己使用,也可以供他人参考,看懂此文,是理解组件开发的入门 思路比较简单: 1.常规写法: 1.1. 创建一个构造函数 1.2. 给构造函数的原型对象添加show 和hid ...
- 2018SDIBT_国庆个人第四场
A - A 这题很巧妙啊,前两天刚好做过,而且就在博客里 Little C Loves 3 I time limit per test 1 second memory limit per test ...
- linux 协议栈tcp的rst报文中,seq的选取问题
之前在<深入理解并行编程>的群里,有个小米的兄弟问了一个问题,服务器A发包给服务器B,Seq是1,但是在未能收到服务器B的报文回复的情况下,发送了rst,但是rst报文中,对应的seq是1 ...
- .NET 基础知识
.net程序基本编写.执行流程(c#) 1>编写c#代码,保存为.cs文件. 2>通过csc.exe程序来将.cs文件编译为.net程序集(.exe或.dll).此 ...
- 5分钟K线图压力线买点怎么看?
某开盘后底开一直呈现形成了一个长时间的箱体振荡的走势,K线在底位振荡时,其波动底点总是在不断抬高的话,这种走势说明有资金在场中积极运作,正是由于资金悄然建仓导致了底点慢慢抬高的走势,在底点不断抬高时, ...
- 16.1 用auth0服务 实现用登录和管理 使用auth版本的2个大坑。
这是三周内容,实现用户登录和管理 回到master分支 切换到 han分支 更新一下 然后工作 开始工作写代码了 安装2个angular端的auth0的lib,也可不安装,后边有不安装的做法 不安装的 ...
- webservice调用dll
今天客户那里报出来,ws通讯不成功.但是在本机和windows2003上都可以测试成功.WS的页面(asmx)可以出来,但是点击接口方法,调用就报http500错误. 网站无法显示该页面 HTT ...
- 在webpack构建的项目中使用vue
一.复习在普通网页中使用vue1.使用script引入vue2.在index中创建 id为app的容器3.通过new vue得到vm实例二.在webpack中尝试使用vue://注意 : 在webpa ...
- Java http请求工具类
该工具类可以调用POST请求或者Get请求,参数以Map的方式传入,支持获获取返回值,返回值接收类型为String HttpRequestUtil.java package com.util; imp ...