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:

$T$
$\mathrm{case}_1$
$\mathrm{case}_2$
$\vdots$
$\mathrm{case}_T$

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

$N$ $D$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$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

2
3 1
1 2
1 3
2 3
4 1
1 2
1 3
2 3
3 4

Sample Output 1

Yes
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\),那么他没有出度,只能是整个强连通分量。

#include<bits/stdc++.h>
using namespace std;
const int N=5e4+5,T=(N<<1)-1;
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];
template<int N,int M>struct graph{
int hd[N],e_num;
struct edge{
int u,v,nxt,f;
}e[M<<1];
void add_edge(int u,int v,int f)
{
e[++e_num]=(edge){u,v,hd[u],f};
hd[u]=e_num;
e[++e_num]=(edge){v,u,hd[v],0};
hd[v]=e_num;
}
void clear()
{
for(int i=2;i<=e_num;i++)
hd[e[i].u]=0;
e_num=1;
}
};
graph<N,N>g;
graph<N<<1,N<<2>fl;
int bfs()
{
int l,r;
for(int i=0;i<=n+n*d;i++)
fl.hd[i]=hd[i],v[i]=0;
fl.hd[T]=hd[T],v[T]=0;
v[q[l=r=1]=0]=1;
while(l<=r)
{
for(int i=fl.hd[q[l]];i;i=fl.e[i].nxt)
if(fl.e[i].f&&!v[fl.e[i].v])
v[q[++r]=fl.e[i].v]=v[q[l]]+1;
++l;
}
return v[T];
}
int dfs(int x,int f)
{
if(x==T)
return f;
for(int&i=fl.hd[x];i;i=fl.e[i].nxt)
{
int k;
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))))
{
fl.e[i].f-=k;
fl.e[i^1].f+=k;
return k;
}
}
return 0;
}
void tarjan(int x)
{
dfn[x]=low[x]=++idx,st[++tp]=x;
for(int i=g.hd[x];i;i=g.e[i].nxt)
{
if(g.e[i].f&&!dfn[g.e[i].v])
{
tarjan(g.e[i].v);
low[x]=min(low[x],low[g.e[i].v]);
}
else if(g.e[i].f&&!id[g.e[i].f])
low[x]=min(low[x],dfn[g.e[i].v]);
}
if(low[x]==dfn[x])
{
++tme;
while(st[tp]^x)
id[st[tp--]]=tme;
id[st[tp--]]=tme;
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
g.clear(),fl.clear();
for(int i=0;i<=n*d;i++)
dfn[i]=id[i]=0;
idx=tme=mxf=tp=0;
scanf("%d%d",&n,&d);
for(int i=1,u,v;i<=n*d;i++)
{
scanf("%d%d",&u,&v);
g.add_edge(u,v,0);
fl.add_edge(0,i,1);
fl.add_edge(i,n*d+u,1);
to[i]=fl.e_num;
fl.add_edge(i,n*d+v,1);
}
for(int i=1;i<=n;i++)
fl.add_edge(i+n*d,T,d);
for(int i=0;i<=n*d+n;i++)
hd[i]=fl.hd[i];
hd[T]=fl.hd[T];
int k;
while(bfs())
while(k=dfs(0,2000000000))
mxf+=k;
if(mxf!=n*d)
{
puts("No");
continue;
}
for(int i=1;i<=n*d;i++)
{
if(fl.e[to[i]].f)
g.e[i<<1].f=1;
else
g.e[i<<1|1].f=1;
}
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
puts(tme^1? "No":"Yes");
}
}

[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. U盘目录穿越获取车机SHELL - 分析与复现

    github上破解日系车机的文章 - https://github.com/ea/bosch_headunit_root 其中有利用 U 盘获取车机 shell 的操作 主要根据下面这篇文章进行环境搭 ...

  2. ChatGPT如何生成可视化图表-示例中国近几年出生人口

    本教程收集于:AIGC从入门到精通教程汇总 ChatGPT本身不能直接生成可视化图表,但可以配合其他可视化工具或库 方法一:前端可视化开发库 Echarts(地址:Apache ECharts ) 方 ...

  3. 带你上手基于Pytorch和Transformers的中文NLP训练框架

    本文分享自华为云社区<全套解决方案:基于pytorch.transformers的中文NLP训练框架,支持大模型训练和文本生成,快速上手,海量训练数据>,作者: 汀丶 . 1.简介 目标: ...

  4. 如何获取和分析Java堆信息

    引言 在Java应用程序的开发和维护过程中,了解和分析Java堆信息是一项重要的任务.本文将介绍如何获取Java堆信息的不同方法,并提供一些分析堆信息的实用技巧. 获取Java堆信息的方法 Java虚 ...

  5. 高德Android高性能高稳定性代码覆盖率技术实践

    ​前言 代码覆盖率(Code coverage)是软件测试中的一种度量方式,用于反映代码被测试的比例和程度. 在软件迭代过程中,除了应该关注测试过程中的代码覆盖率,用户使用过程中的代码覆盖率也是一个非 ...

  6. 【matplotlib基础】--子图

    使用Matplotlib对分析结果可视化时,比较各类分析结果是常见的场景.在这类场景之下,将多个分析结果绘制在一张图上,可以帮助用户方便地组合和分析多个数据集,提高数据可视化的效率和准确性. 本篇介绍 ...

  7. CodeForces 1343D Constant Palindrome Sum

    题意 多组样例 给一个长度为\(n\)(\(n\)一定为偶数)的数组\(a[]\),给一个正整数\(k\),保证数组内元素为小于等于\(k\)的正整数,你可以每次将数组的一个元素变为小于等于\(k\) ...

  8. 【源码】Vue.js 官方脚手架 create-vue 是怎么实现的?

    Vue.js 官方脚手架 create-vue 是怎么实现的? 摘要 本文共分为四个部分,系统解析了vue.js 官方脚手架 create-vue 的实现细节. 第一部分主要是一些准备工作,如源码下载 ...

  9. KRPANO资源分析工具下载四方环视全景图

    提示:目前分析工具中的全景图下载功能将被极速全景图下载大师替代,相比分析工具,极速全景图下载大师支持更多的网站(包括各类KRPano全景网站,和百度街景) 详细可以查看如下的链接: 极速全景图下载大师 ...

  10. 如何实现一个数据库的 UDF?图数据库 NebulaGraph UDF 功能背后的设计与思考

    大家好,我是来自 BOSS直聘的赵俊南,主要负责安全方面的图存储相关工作.作为一个从 v1.x 用到 v3.x 版本的忠实用户,在见证 NebulaGraph 发展的同时,也和它一起成长. BOSS直 ...