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. Phaser都不懂,还学什么多线程

    前面的文章中我们讲到了CyclicBarrier.CountDownLatch的使用,这里再回顾一下CountDownLatch主要用在一个线程等待多个线程执行完毕的情况,而CyclicBarrier ...

  2. 美版健康码要来了!苹果Google被网友质疑:这是变相的监视系统吗?

    4 月 30 日消息,据外媒报道,由苹果和谷歌合作开发一个冠状病毒追踪系统将在 5 月 1 日正式上架,今天已经进入测试阶段. 图自:techcrunch 这款应用可以让 iOS 和 Android ...

  3. POJ2389 Bull Math【大数】

    Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15040   Accepted: 7737 Descri ...

  4. k8s namespace限制调研

    1.创建namespace gpu 2.增加限制 [root@tensorflow1 gpu-namespace]# cat compute-resources.yaml apiVersion: v1 ...

  5. 搭建Springboot+mybatis+redis+druid

    2019独角兽企业重金招聘Python工程师标准>>> 准备工作 JDK:1.8 使用技术:SpringBoot.Dubbo.Mybatis.Druid 开发工具:Intelj ID ...

  6. 腾讯技术工程 | 基于Prophet的时间序列预测

    预测未来永远是一件让人兴奋而又神奇的事.为此,人们研究了许多时间序列预测模型.然而,大部分的时间序列模型都因为预测的问题过于复杂而效果不理想.这是因为时间序列预测不光需要大量的统计知识,更重要的是它需 ...

  7. Xapian实战(四):搜索

    参考资料: 学习Xapian(1)-基础的建索引和搜索 1. Xapian中用于搜索的类 Enquire - 提供了检索的接口:(Enquire API) QueryParser(QueryParse ...

  8. Chrome最新版如何安装Proxy SwitchyOmega

    由于Chrome的代理设置与windows10的1703及以后的版本不兼容,导致无法使用代理功能,给工作带来了很大的不便.最近发现一款不错的Chrome代理插件Proxy SwitchyOmega,由 ...

  9. P1522 牛的旅行 Cow Tours(floyd)

    题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...

  10. CF1324D Pair of Topics

    好像题解里都是树状数组(起码我翻到的是 说一种cdq分治的(这应该算是cdq分治了 用cdq比较简单,所以可以作为一个练手题 cdq分治其实是一种模糊的思想,处理\([l,r]\)区间内,有多少\(( ...