D. Almost Acyclic Graph
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can be traversed in only one direction). You are allowed to remove at most one edge from it.

Can you make this graph acyclic by removing at most one edge from it? A directed graph is called acyclic iff it doesn't contain any cycle (a non-empty path that starts and ends in the same vertex).

Input

The first line contains two integers n and m (2 ≤ n ≤ 500, 1 ≤ m ≤ min(n(n - 1), 100000)) — the number of vertices and the number of edges, respectively.

Then m lines follow. Each line contains two integers u and v denoting a directed edge going from vertex u to vertex v (1 ≤ u, v ≤ n, u ≠ v). Each ordered pair (u, v) is listed at most once (there is at most one directed edge from u to v).

Output

If it is possible to make this graph acyclic by removing at most one edge, print YES. Otherwise, print NO.

Examples
Input
3 4
1 2
2 3
3 2
3 1
Output
YES
Input
5 6
1 2
2 3
3 2
3 1
2 1
4 5
Output
NO
Note

In the first example you can remove edge , and the graph becomes acyclic.

In the second example you have to remove at least two edges (for example, and ) in order to make the graph acyclic

https://www.cnblogs.com/Blogggggg/p/8290354.html  //这篇博客给了两个解法。

判断是否存在环用的拓扑排序,我想到的一个问题是度数是由连接这个点的很多条边决定的,为什么度数减一能够契合那条关键边边并得到正确答案呢?

我臆想的答案是:  每个点的价值就是: 使 所通向的点的度数 -1,那么先实现这个价值肯定是好的,所以说只要度数变为0了,剩下的那条边就一定是关键边了。

顺便复习Tarjan

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=;
const int M=1e5+;
int n,m,tot,x,y,head[N],to[M],nxt[M],ru[N];
bool vis[N],f,B[N];
void add(int u,int v){
to[++tot]=v;nxt[tot]=head[u];head[u]=tot;
}
int dfn[N],q[N],low[N],top,sz,ry[N];
void Tajan(int u){
if(f) return;
low[u]=dfn[u]=++sz;
vis[u]=;
q[++top]=u;
for(int i=head[u];!f&&i;i=nxt[i]){
int v=to[i];
if(!dfn[v]) Tajan(v),low[u]=min(low[u],low[v]);
else if(vis[v]&&low[u]>dfn[v]) low[u]=dfn[v];
}
if(low[u]==dfn[u]) {
int x,p=;
do{
x=q[top--];
vis[x]=;
B[x]=;
ry[p++]=x;
}while(x!=u);
if(p>) f=;
else B[x]=;
}
}
pair<int,int>re[N];
void dfs(int u,int pos){
if(!f) return;
for(int i=head[u];i&&f;i=nxt[i]) {
int v=to[i];
if(v==ry[]) {re[pos].first=u,re[pos].second=v; sz=pos;f=;return;}
if(!B[v]||vis[v]) continue;
else {vis[v]=;re[pos].first=u,re[pos].second=v;dfs(v,pos+);}
}
}
int ru1[N];
bool Topsort(){
int l=,r=,own=;
for(int i=;i<=n;++i) if(!ru1[i]) q[r++]=i;
while(l<r) {
int u=q[l++];
for(int i=head[u];i;i=nxt[i]) {
--ru1[to[i]];
if(!ru1[to[i]]) q[r++]=to[i];
}
}
return r==n;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=m;++i) {
scanf("%d%d",&x,&y);
add(x,y);
++ru[y];
}
for(int i=;!f&&i<=n;++i) if(!dfn[i]) Tajan(i);
if(!f) {puts("YES");return ;}
memset(vis,,sizeof(vis));
dfs(ry[],);
for(int i=;i<=sz;++i) {
--ru[re[i].second];
for(int j=;j<=n;++j) ru1[j]=ru[j];
if(Topsort()) {puts("YES");return ;}
++ru[re[i].second];
}
puts("NO");
}

D. Almost Acyclic Graph 判断减一条边能不能得到DAG的更多相关文章

  1. 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环

    [题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...

  2. Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

  3. 凸包稳定性判断:每条边上是否至少有三点 POJ 1228

    //凸包稳定性判断:每条边上是否至少有三点 // POJ 1228 #include <iostream> #include <cstdio> #include <cst ...

  4. algorithm@ Shortest Path in Directed Acyclic Graph (O(|V|+|E|) time)

    Given a Weighted Directed Acyclic Graph and a source vertex in the graph, find the shortest paths fr ...

  5. JFinal Model判断数据库某条记录的属性字段是否包含空值

    如果做报表,一条记录中有空值,使用FreeMarker渲染word会报错,并把错误日志输出到Word中.所以需要之前判断下当前记录中属性值是否有空值. package com.huijiasoft.u ...

  6. CodeForces 915D Almost Acyclic Graph

    Description You are given a directed graph consisting of \(n\) vertices and \(m\) edges (each edge i ...

  7. CF915D Almost Acyclic Graph

    题目链接:http://codeforces.com/contest/915/problem/D 题目大意: 给出一个\(n\)个结点\(m\)条边的有向图(无自环.无重边,2 ≤ n ≤ 500, ...

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

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

  9. [内容分享]粗略判断Shader每条代码的成本

    https://mp.weixin.qq.com/s/Vyn1bKaBMHommxbnFPPQeg Unity对Shader文件进行编译的时候,DX9和DX11的版本会直接生成汇编码. ?   len ...

随机推荐

  1. 【JAVA基础】08 面向对象3

    1. 多态 多态polymorhic概述 事物存在的多种形态. 多态前提 要有继承关系 要有方法重写 要有父类引用指向子类对象 案例演示 代码体现多态 class Demo1_Polymorphic{ ...

  2. Radware:上周五美国大规模DDoS攻击是如何发生的

    10月21日上午,Dyn遭受到拒绝服务(DoS)攻击,造成了托管DNS网络的中断.成千上万的网站因此变得不可访问,其中包括Amazon EC2.当天晚些时候,当攻击者发起第二轮针对Dyn DNS系统的 ...

  3. Java中的集合Queue

    2019独角兽企业重金招聘Python工程师标准>>> package com.zhaogang.test; import org.junit.Test; import java.u ...

  4. RHEL6 搭建 keepalived + lvs/DR 集群

    搭建 keepalived + lvs/DR  集群 使用Keepalived为LVS调度器提供高可用功能,防止调度器单点故障,为用户提供Web服务: LVS1调度器真实IP地址为192.168.4. ...

  5. 图论--拓扑排序--判断是否为DAG图

    #include<cstdio> #include<cstring> #include<vector> #include<queue> using na ...

  6. 禅道部署(基于 Linux)

    1. 查看 Linux 服务器是 32位 还是 64位 的 getconf LONG_BIT 2. 禅道开源版安装包下载 下载站点1:# wget http://sourceforge.net/pro ...

  7. 最新Idea超实用告别996插件,都是免费

    Idea告别996插件 在IntelliJ IDEA中,秉着IDEA自带能实现的快捷方式就不用插件的原则,少用些插件,运行性能也提升一些,虽然很少,哈哈.分享下我个人常用的插件,希望对大家有些帮助.插 ...

  8. Android JetPack组件-CameraX初探

    CameraX 又是一个 Google 推出的 JetPack 组件 ,是一个新鲜玩意儿,故给大家分享下我在项目中的使用过程心得.. CameraX 是什么? Google 开发者文档 对 Camer ...

  9. c/c++获取文件夹下所有文件名

    如何获取某一文件夹下所有文件名,是一个很有意思的问题.网上代码很多,找了个简单的,特此收录. #include <iostream> #include <io.h> #incl ...

  10. 【MIT6.828】centos7下使用Qemu搭建xv6运行环境

    title:[MIT6.828]centos7下使用Qemu搭建xv6运行环境 date: "2020-05-05" [MIT6.828]centos7下搭建xv6运行环境 1. ...