"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 (贪心或有源汇上下界网络流)的更多相关文章

  1. ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph

    "Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...

  2. ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph (上下界网络流)

    正解: #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN=1 ...

  3. ACM-ICPC 2018 沈阳赛区网络预赛 F Fantastic Graph(贪心或有源汇上下界网络流)

    https://nanti.jisuanke.com/t/31447 题意 一个二分图,左边N个点,右边M个点,中间K条边,问你是否可以删掉边使得所有点的度数在[L,R]之间 分析 最大流不太会.. ...

  4. ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph(有源上下界最大流 模板)

    关于有源上下界最大流: https://blog.csdn.net/regina8023/article/details/45815023 #include<cstdio> #includ ...

  5. Fantastic Graph 2018 沈阳赛区网络预赛 F题

    题意: 二分图 有k条边,我们去选择其中的几条 每选中一条那么此条边的u 和 v的度数就+1,最后使得所有点的度数都在[l, r]这个区间内 , 这就相当于 边流入1,流出1,最后使流量平衡 解析: ...

  6. 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. ...

  7. 图上两点之间的第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 ...

  8. ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)

    https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...

  9. 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 ...

随机推荐

  1. UI5-学习篇-1-Eclipse开发工具及环境搭建

    最近研究SAP-UI5好几个月了,将相关学习经历及问题点做个记录. 1.先了解学习资料相关站点 SAP官网:https://www.sap.com/china/index.html SAP开发工具:h ...

  2. homebrew, carthage以及redis的安装和启动

    homebrew的介绍以及redis的安装   brew install redis https://www.cnblogs.com/xd502djj/p/6923690.html redis的启动, ...

  3. pandas 笔记

    删除: del df["A"]  # 原地修改 df.drop("a")  # 返回修改后的新对象 df.drop(["a", " ...

  4. happyxiaofan的程序员书单

    转自   happyxiaofan 读书的看法 从15年7月至今,研究生期间读了不少书,读书让我学到了很多,也是提升技术能力的一个重要手段.可能很多人嫌读书太花时间,曾经的我一度也是这么认为的,觉得一 ...

  5. A New Year, A New Accent!

    A New Year, A New Accent! Share Tweet Share Happy New Year!  Does your list of resolutions include i ...

  6. java解决查找问题

    1.给定一个字符串,找到里面的大写字母和小写字母以及其他字母的个数: 代码: package test; public class Stringclass { public static void m ...

  7. Python unindent dese not match any out indentation level 问题

    今天写个小程序出现 “unindent dese not  match any out indentation level”. 一直没找到原因,经过仔细对比发现实际上是缩进的问题. 上下两行的缩进用的 ...

  8. Oracle 导入大量数据

    环境是这样的: 需要导入大量数据到Oracle,目前Oracle已建立索引和触发器了,导入的数据是树型结构,需要关联. 采用的方法是: 删除以前数据库的索引和触发器,用OracleBulkCopy批量 ...

  9. UVA11572-Unique Snowflakes-(最长不同连续子序列)

    题意:给n个数,求最长不同连续子序列.n<=1e6. 解题过程: 1.记录数据存于数组 2.用左右指针l和r指向这段连续区间 3.右指针往右走,如果遇到没有存在于set集合的数就插入集合 否则左 ...

  10. 拓扑排序-有向无环图(DAG, Directed Acyclic Graph)

    条件: 1.每个顶点出现且只出现一次. 2.若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面. 有向无环图(DAG)才有拓扑排序,非DAG图没有拓扑排序一说. 一 ...