Problem Statement

We define the density of a non-empty simple undirected graph as $\displaystyle\frac{(\text{number of edges})}{(\text{number of vertices})}$.

You are given positive integers $N$, $D$, and a simple undirected graph $G$ with $N$ vertices and $DN$ edges.
The vertices of $G$ are numbered from $1$ to $N$, and the $i$-th edge connects vertex $A_i$ and vertex $B_i$.
Determine whether $G$ satisfies the following condition.

Condition: Let $V$ be the vertex set of $G$.
For any non-empty proper subset $X$ of $V$, the density of the induced subgraph of $G$ by $X$ is strictly less than $D$.

There are $T$ test cases to solve.

What is an induced subgraph?

For a vertex subset $X$ of graph $G$, the induced subgraph of $G$ by $X$ is the graph with vertex set $X$ and edge set containing all edges of $G$ that connect two vertices in $X$.
In the above condition, note that we only consider vertex subsets that are neither empty nor the entire set.

Constraints

  • $T \geq 1$
  • $N \geq 1$
  • $D \geq 1$
  • The sum of $DN$ over the test cases in each input file is at most $5 \times 10^4$.
  • $1 \leq A_i < B_i \leq N \ \ (1 \leq i \leq DN)$
  • $(A_i, B_i) \neq (A_j, B_j) \ \ (1 \leq i < j \leq DN)$

Input

The input is given from Standard Input in the following format:

  1. $T$
  2. $\mathrm{case}_1$
  3. $\mathrm{case}_2$
  4. $\vdots$
  5. $\mathrm{case}_T$

Each test case $\mathrm{case}_i \ (1 \leq i \leq T)$ is in the following format:

  1. $N$ $D$
  2. $A_1$ $B_1$
  3. $A_2$ $B_2$
  4. $\vdots$
  5. $A_{DN}$ $B_{DN}$

Output

Print $T$ lines.
The $i$-th line should contain Yes if the given graph $G$ for the $i$-th test case satisfies the condition, and No otherwise.


Sample Input 1

  1. 2
  2. 3 1
  3. 1 2
  4. 1 3
  5. 2 3
  6. 4 1
  7. 1 2
  8. 1 3
  9. 2 3
  10. 3 4

Sample Output 1

  1. Yes
  2. No
  • The first test case is the same as Sample Input 1 in Problem D, and it satisfies the condition.
  • For the second test case, the edge set of the induced subgraph by the non-empty proper subset $\{1, 2, 3\}$ of the vertex set $\{1, 2, 3, 4\}$ is $\{(1, 2), (1, 3), (2, 3)\}$, and its density is $\displaystyle\frac{3}{3} = 1 = D$.
    Therefore, this graph does not satisfy the condition.

新套路

要判定每个子图是否都满足这个要求,挺难弄的,但是移一下项。\(\frac mn<D,m<nD\),想到了 Hall定理

Hall定理内容:一个二分图存在满流,当且仅当对于任意一个左端点集 S,设 T 为所有和 S 有连边的店,那么 \(|T|\ge |S|\)

那么可以尝试构图,使得判断 \(m\) 是否小于等于 \(D\) 改为判断这个二分图是否存在满流。

把一个点拆成 \(D\) 个点,然后把每条边当成一个点,一条边 \((u,v)\) 向 \(u\) 点拆出来的和 \(v\) 点拆出来的所有点连边。这样,这个二分图满流就代表所有的 \(m\le nD\)。直接跑网络流,可以不用真拆点,把一个点向汇点的流量调为 \(D\) 就行了。

但是我们还需要判断 \(m\) 是否等于 \(nD\),Editorial里写的结论是,给原图每条边定向。如果二分图中边 \(i\) 向点 \(u\) 流出,则从 \(u\) 连到点 \(v\),否则从 \(v\) 连向点 \(u\)。易得一个点出度为 \(D\)。如果得到的图是强连通的,那么条件成立。

如果有两个强连通分量,那么关注没有出度的那一个,由于他没有出度,所有所有的边都在强连通分块内,又因为每个点都有 \(D\) 条出边,所以他的密度等于 \(D\)。

然后看是强连通分量的是不是一定满足要求,发现如果存在一个点集密度等于 \(D\),那么他没有出度,只能是整个强连通分量。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=5e4+5,T=(N<<1)-1;
  4. int t,n,d,mxf,to[N],tme,idx,dfn[N],low[N],q[N<<1],v[N<<1],hd[N<<1],id[N],tp,st[N];
  5. template<int N,int M>struct graph{
  6. int hd[N],e_num;
  7. struct edge{
  8. int u,v,nxt,f;
  9. }e[M<<1];
  10. void add_edge(int u,int v,int f)
  11. {
  12. e[++e_num]=(edge){u,v,hd[u],f};
  13. hd[u]=e_num;
  14. e[++e_num]=(edge){v,u,hd[v],0};
  15. hd[v]=e_num;
  16. }
  17. void clear()
  18. {
  19. for(int i=2;i<=e_num;i++)
  20. hd[e[i].u]=0;
  21. e_num=1;
  22. }
  23. };
  24. graph<N,N>g;
  25. graph<N<<1,N<<2>fl;
  26. int bfs()
  27. {
  28. int l,r;
  29. for(int i=0;i<=n+n*d;i++)
  30. fl.hd[i]=hd[i],v[i]=0;
  31. fl.hd[T]=hd[T],v[T]=0;
  32. v[q[l=r=1]=0]=1;
  33. while(l<=r)
  34. {
  35. for(int i=fl.hd[q[l]];i;i=fl.e[i].nxt)
  36. if(fl.e[i].f&&!v[fl.e[i].v])
  37. v[q[++r]=fl.e[i].v]=v[q[l]]+1;
  38. ++l;
  39. }
  40. return v[T];
  41. }
  42. int dfs(int x,int f)
  43. {
  44. if(x==T)
  45. return f;
  46. for(int&i=fl.hd[x];i;i=fl.e[i].nxt)
  47. {
  48. int k;
  49. if(fl.e[i].f&&v[fl.e[i].v]==v[x]+1&&(k=dfs(fl.e[i].v,min(f,fl.e[i].f))))
  50. {
  51. fl.e[i].f-=k;
  52. fl.e[i^1].f+=k;
  53. return k;
  54. }
  55. }
  56. return 0;
  57. }
  58. void tarjan(int x)
  59. {
  60. dfn[x]=low[x]=++idx,st[++tp]=x;
  61. for(int i=g.hd[x];i;i=g.e[i].nxt)
  62. {
  63. if(g.e[i].f&&!dfn[g.e[i].v])
  64. {
  65. tarjan(g.e[i].v);
  66. low[x]=min(low[x],low[g.e[i].v]);
  67. }
  68. else if(g.e[i].f&&!id[g.e[i].f])
  69. low[x]=min(low[x],dfn[g.e[i].v]);
  70. }
  71. if(low[x]==dfn[x])
  72. {
  73. ++tme;
  74. while(st[tp]^x)
  75. id[st[tp--]]=tme;
  76. id[st[tp--]]=tme;
  77. }
  78. }
  79. int main()
  80. {
  81. scanf("%d",&t);
  82. while(t--)
  83. {
  84. g.clear(),fl.clear();
  85. for(int i=0;i<=n*d;i++)
  86. dfn[i]=id[i]=0;
  87. idx=tme=mxf=tp=0;
  88. scanf("%d%d",&n,&d);
  89. for(int i=1,u,v;i<=n*d;i++)
  90. {
  91. scanf("%d%d",&u,&v);
  92. g.add_edge(u,v,0);
  93. fl.add_edge(0,i,1);
  94. fl.add_edge(i,n*d+u,1);
  95. to[i]=fl.e_num;
  96. fl.add_edge(i,n*d+v,1);
  97. }
  98. for(int i=1;i<=n;i++)
  99. fl.add_edge(i+n*d,T,d);
  100. for(int i=0;i<=n*d+n;i++)
  101. hd[i]=fl.hd[i];
  102. hd[T]=fl.hd[T];
  103. int k;
  104. while(bfs())
  105. while(k=dfs(0,2000000000))
  106. mxf+=k;
  107. if(mxf!=n*d)
  108. {
  109. puts("No");
  110. continue;
  111. }
  112. for(int i=1;i<=n*d;i++)
  113. {
  114. if(fl.e[to[i]].f)
  115. g.e[i<<1].f=1;
  116. else
  117. g.e[i<<1|1].f=1;
  118. }
  119. for(int i=1;i<=n;i++)
  120. if(!dfn[i])
  121. tarjan(i);
  122. puts(tme^1? "No":"Yes");
  123. }
  124. }

[ARC161F] Everywhere is Sparser than Whole (Judge)的更多相关文章

  1. Gym 101102C---Bored Judge(区间最大值)

    题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...

  2. NOJ 1074 Hey Judge(DFS回溯)

    Problem 1074: Hey Judge Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format: ...

  3. 【教程】如何正确的写一个Lemon/Cena的SPJ(special judge)

    转自:http://www.cnblogs.com/chouti/p/5752819.html Special Judge:当正确的输出结果不唯一的时候需要的自定义校验器 首先有个框架 #includ ...

  4. 九度 Online Judge 之《剑指 Offer》一书相关题目解答

    前段时间准备华为机试,正好之前看了一遍<剑指 Offer>,就在九度 Online Judge 上刷了书中的题目,使用的语言为 C++:只有3题没做,其他的都做了. 正如 Linus To ...

  5. UVa Online Judge 工具網站

    UVa Online Judge 工具網站   UVa中译题uHuntAlgorithmist Lucky貓的ACM園地,Lucky貓的 ACM 中譯題目 Mirror UVa Online Judg ...

  6. [swustoj 1021] Submissions of online judge

    Submissions of online judge(1021) 问题描述 An online judge is a system to test programs in programming c ...

  7. HDOJ/HDU 1073 Online Judge(字符串处理~)

    Problem Description Ignatius is building an Online Judge, now he has worked out all the problems exc ...

  8. write a macro to judge big endian or little endian

    Big endian means the most significant byte stores first in memory. int a=0x01020304, if the cpu is b ...

  9. UVA 489-- Hangman Judge(暴力串处理)

     Hangman Judge  In ``Hangman Judge,'' you are to write a program that judges a series of Hangman gam ...

  10. 杭州电子科技大学Online Judge 之 “确定比赛名次(ID1285)”解题报告

    杭州电子科技大学Online Judge 之 "确定比赛名次(ID1285)"解题报告 巧若拙(欢迎转载,但请注明出处:http://blog.csdn.net/qiaoruozh ...

随机推荐

  1. Job System 初探

    作者:i_dovelemon 日期:2023-08-24 主题:Fiber, Atomic Operation, MPMC Queue, Multiple thread, Job system 引言 ...

  2. 选择合适的方法进行API接口调试

    随着互联网的快速发展,API(Application Programming Interface)接口在软件开发中扮演着重要的角色.调试API接口是确保系统正常运行的关键步骤之一.本文将介绍如何选择适 ...

  3. 算法笔记_python

    目录 算法 概念 时间复杂度 空间复杂度 递归原理 顺序查找 二分查找 列表排序 LowB 三人组 冒泡排序 选择排序 插入排序 NB三人组 快速排序 堆排序 归并排序 NB三人组小结 总结 其他排序 ...

  4. 地表最帅缓存Caffeine

    简介 缓存是程序员们绕不开的话题,像是常用的本地缓存Guava,分布式缓存Redis等,是提供高性能服务的基础.今天敬姐带大家一起认识一个更高效的本地缓存--Caffeine. Caffeine Ca ...

  5. 不关闭Tamper Protection(篡改保护)下强制卸载Windows Defender和安全中心所有组件

    个人博客: xzajyjs.cn 背景介绍 由于微软不再更新arm版本的win10系统,因此只能通过安装insider preview的镜像来使用.而能找到的win10 on arm最新版镜像在安装之 ...

  6. Xshell远程连接、MBR/BOOT和GRUB三者关系总结(系统启动过程)

    远程连接 远程连接Linux服务器的常见工具有Xshell.SecureCRT.Putty等,这些客户端连接工具在Linux服务器对应着相同SSH服务进程sshd,即远程连接都是使用SSH协议,当然它 ...

  7. a-2b

    a-2b   描述 输入两个高精度数a和b,求a-2b的值. 输入 输入两行,第一行是被减数a,第二行是减数b(a>2b并且a,2b的位数不同且不存在借位,且b+b不存在进位). 输出 一行,即 ...

  8. DDMS查看Threads情况

    有时候程序运行出现死锁或者信号量卡死是很纠结的问题,单看代码很难分析定位问题,这时候可以借助DDMS来查看threads的运行情况,一目了然. 手机连接上USB,确保adb连通,然后启动Eclipse ...

  9. Sentinel源码改造,实现Nacos双向通信!

    Sentinel Dashboard(控制台)默认情况下,只能将配置规则保存到内存中,这样就会导致 Sentinel Dashboard 重启后配置规则丢失的情况,因此我们需要将规则保存到某种数据源中 ...

  10. 一元多项式求和(c++源码)

    LinkList.h #ifndef LINKLIST_H_ #define LINKLIST_H_ #include<stdio.h> template<class T> s ...