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. 这个linux命令能让时光倒流!你不知道的date隐藏用法

    文章每周持续更新,各位的「三连」是对我最大的肯定.可以微信搜索公众号「 后端技术学堂 」第一时间阅读(一般比博客早更新一到两篇) 今天给项目写了个脚本需要获取前一天的时间,本来先获取今天的然后减一下, ...

  2. 【Linux常见命令】echo命令

    echo - display a line of text 打印输出内容的常用命令,可以结合重定向>和追加>>对文件进行覆盖或追加内容. 语法: echo [SHORT-OPTION ...

  3. Get史上最优雅的加密方式!没有之一!

    你的配置文件是不是还在使用下面这种落后的配置暴露一些密码: jdbc.url=jdbc:mysql://127.0.0.1:3305/afei jdbc.username=afei 如果是,那么继续往 ...

  4. RHCS图形界面建立GFS共享下

    我们上面通过图形界面实现了GFS,我们这里使用字符界面实现 1.1.       系统基础配置 5台节点均采用相同配置. 配置/etc/hosts文件 # vi /etc/hosts 127.0.0. ...

  5. CC视频CTO栗伟:CDN系统架构及CC视频应用实践

    2017 年 11 月9日,CC视频获2.08 亿元C轮融资. EGO 北京分会会员.CC视频CTO栗伟获邀作为 EGO 线上分享第三季嘉宾,与大家交流了CDN系统架构及CC 视频的应用实践. \\ ...

  6. WebStorm 2019 3.3 安装及破解教程附汉化教程

    WebStorm2019 3.3 安装及破解教程附加汉化教程 安装包及破解补丁 链接: https://pan.baidu.com/s/19ATTAW3Tsm0huIJSqYChTw 提取码:1ei7 ...

  7. ACM学习心得

    今天打比赛,调整好了心态,不管rank榜,所以做的比上次好,今天A了四个题,都很水,memset的清零时间,需要好长,因为memset 跟cin超时了,它的数据量1e6,所以超时了还是多用scanf, ...

  8. 图论--差分约束--POJ 3169 Layout(超级源汇建图)

    Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < ...

  9. Element upload组件上传图片与回显图片

    场景:新增商品时需要添加商品主图,新增成功之后可编辑 上传图片: <el-form-item label="专区logo:" style="height:160px ...

  10. 去 HBase,Kylin on Parquet 性能表现如何?

    Kylin on HBase 方案经过长时间的发展已经比较成熟,但也存在着局限性,因此,Kyligence 推出了 Kylin on Parquet 方案(了解详情戳此处).通过标准数据集测试,与仍采 ...