"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的数据可以发评论

最后贴个官方做法,有源汇上下界网络流

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxn=;
  5.  
  6. int main()
  7. {
  8. int N,M,K,L,R,o=,u[maxn],v[maxn],d[maxn];
  9. while(scanf("%d%d%d",&N,&M,&K)!=EOF)
  10. {
  11. memset(d,,sizeof d);
  12. scanf("%d%d",&L,&R);
  13. int sum=,flag=;
  14. for(int i=;i<K;i++)
  15. {
  16. scanf("%d%d",&u[i],&v[i]);v[i]+=N;
  17. d[u[i]]++,d[v[i]]++;
  18. }
  19. for(int i=;i<K;i++)
  20. {
  21. int uu=u[i],vv=v[i];
  22. if(d[uu]>R&&d[vv]>R)d[uu]--,d[vv]--;
  23. else if(L<=d[uu]&&d[uu]<=R&&L<=d[vv]&&d[vv]<=R)continue;
  24. else if(L+<=d[uu]&&d[uu]<=R&&d[vv]>R)d[uu]--,d[vv]--;
  25. else if(d[uu]>R&&L+<=d[vv]&&d[vv]<=R)d[uu]--,d[vv]--;
  26. }
  27. for(int i=;i<=N+M;i++)if(d[i]<L||d[i]>R)flag=;
  28. printf("Case %d: %s\n",o++,flag?"Yes":"No");
  29. }
  30. return ;
  31. }

给一点测试数据,网上有的贪心过不去这些数据Yes Yes Yes Yes No

  1.  

 官方做法

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. const int maxn=1e5+;
  7. const int maxm=2e5+;
  8. const int INF=0x3f3f3f3f;
  9.  
  10. int TO[maxm],CAP[maxm],NEXT[maxm],tote;
  11. int FIR[maxn],gap[maxn],cur[maxn],d[maxn],q[];
  12. int n,m,S,T;
  13.  
  14. void add(int u,int v,int cap)
  15. {
  16. //printf("i=%d u=%d v=%d cap=%d\n",tote,u,v,cap);
  17. TO[tote]=v;
  18. CAP[tote]=cap;
  19. NEXT[tote]=FIR[u];
  20. FIR[u]=tote++;
  21.  
  22. TO[tote]=u;
  23. CAP[tote]=;
  24. NEXT[tote]=FIR[v];
  25. FIR[v]=tote++;
  26. }
  27. void bfs()
  28. {
  29. memset(gap,,sizeof gap);
  30. memset(d,,sizeof d);
  31. ++gap[d[T]=];
  32. for(int i=;i<=n;++i)cur[i]=FIR[i];
  33. int head=,tail=;
  34. q[]=T;
  35. while(head<=tail)
  36. {
  37. int u=q[head++];
  38. for(int v=FIR[u];v!=-;v=NEXT[v])
  39. if(!d[TO[v]])
  40. ++gap[d[TO[v]]=d[u]+],q[++tail]=TO[v];
  41. }
  42. }
  43. int dfs(int u,int fl)
  44. {
  45. if(u==T)return fl;
  46. int flow=;
  47. for(int &v=cur[u];v!=-;v=NEXT[v])
  48. if(CAP[v]&&d[u]==d[TO[v]]+)
  49. {
  50. int Min=dfs(TO[v],min(fl,CAP[v]));
  51. flow+=Min,fl-=Min,CAP[v]-=Min,CAP[v^]+=Min;
  52. if(!fl)return flow;
  53. }
  54. if(!(--gap[d[u]]))d[S]=n+;
  55. ++gap[++d[u]],cur[u]=FIR[u];
  56. return flow;
  57. }
  58. int ISAP()
  59. {
  60. bfs();
  61. int ret=;
  62. while(d[S]<=n)ret+=dfs(S,INF);
  63. return ret;
  64. }
  65.  
  66. int ca,N,M,Q,x,y,z,l[][],r[][];
  67. char op[];
  68.  
  69. void init()
  70. {
  71. tote=;
  72. memset(FIR,-,sizeof FIR);
  73. }
  74. int main()
  75. {
  76. int N,M,C,L,R,u,v,s,t,ca=;
  77. while(scanf("%d%d%d",&N,&M,&C)!=EOF)
  78. {
  79. init();
  80. int in[]={};
  81. s=N+M+,t=s+,S=t+,T=S+,n=T;
  82. add(t,s,INF);
  83. scanf("%d%d",&L,&R);
  84. for(int i=;i<C;i++)
  85. {
  86. scanf("%d%d",&u,&v);
  87. add(u,N+v,);
  88. }
  89. for(int i=;i<=N;i++)
  90. {
  91. add(s,i,R-L);
  92. in[s]-=L;
  93. in[i]+=L;
  94. }
  95. for(int i=;i<=M;i++)
  96. {
  97. add(i+N,t,R-L);
  98. in[i+N]-=L;
  99. in[t]+=L;
  100. }
  101. int sum=;
  102. for(int i=;i<=N+M+;i++)
  103. {
  104. if(in[i]>)
  105. {
  106. add(S,i,in[i]);
  107. sum+=in[i];
  108. }
  109. else
  110. add(i,T,-in[i]);
  111. }
  112. printf("Case %d: %s\n",ca++,sum==ISAP()?"Yes":"No");
  113. }
  114. return ;
  115. }

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. css3 - 特性

    伪类选择器 伪类选择器,不用再使用js来控制奇偶不同了 tr:nth-child(even){ background-color: white; } tr:nth-child(odd){ backgr ...

  2. CSS内容简单总结

    day50 1. 内容回顾 1. 伪类和伪元素        1. 伪类            1. :link            2. :visited            3. :hover ...

  3. ElasticSearch match, match_phrase, term区别

    1.term结构化字段查询,匹配一个值,且输入的值不会被分词器分词. 比如查询条件是: { "query":{ "term":{ "foo" ...

  4. Servlet基本_サーブレットのライフサイクル、スレッドセーフ

    1.サーブレットのライフサイクル初期化時 ⇒ init() [初回リクエスト時] ↓リクエスト時 ⇒service() ⇒doGet() [Httpリクエストメソッドにより振り分け] 或は⇒doPos ...

  5. Visual Studio for Mac 使用笔记

    无法打开控制台运行程序 Project -> Project Options -> Run -> Configurations -> Default 勾选Run on exte ...

  6. python3 得到a.txt中有的而b.txt中没有的汉字

    已知两个文本文档,求a.txt中有的而b.txt中没有的汉字 #读取list1中的汉字 f1=open('/Users/tanchao/Documents/pythonwork/tensorflow/ ...

  7. ERROR Couldn't find hvm kernel for Ubuntu tree.

    安装python-virtinst git clone https://github.com/palli/python-virtinst.gitcd python-virtinstpython set ...

  8. Excel批量修改文件

    [1]把下图片放在一个文件目录下面,如E:\SVM_Class\airplanes [2]点击“开始”→“运行”(或按快捷键win+R),在弹出框中输入“cmd”,进入dos操作界面.   [3]do ...

  9. Gradle安装和在IDEA使用 基本操作

    阅读目录 简单介绍 安装 使用idea创建一个web的Gradle项目 如何进行打包 解释build.gradle和settings.gradle 有关gradle的jar冲突 本地jar包位置和修改 ...

  10. CentOS上开启MySQL远程访问权限

    在CentOS上安装完MySQL后,默认不开始远程访问控制.可以进行如下设定开启. 登录MySQL: mysql -uroot -p 如需修改密码,第一次: mysqladmin -u root pa ...