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. jenkins及Maven介绍

    一.环境介绍 随着软件开发需求及复杂度的不断提高,团队开发成员之间如何更好地协同工作以确保软件开发的质量已经慢慢成为开发过程中不可回避的问题.Jenkins自动化部署可以解决集成.测试.部署等重复性的 ...

  2. Java和php中的try-catch分析

    为什么80%的码农都做不了架构师?>>>   描述:对一个健壮的系统来讲,异常处理是必不可少的一部分,针对异常的管理,主要就是异常的捕获和处理操作,然而在php中使用try-catc ...

  3. docker 安装centos7并SSH远程连接

    1.安装centos7 镜像 1.搜索并拉取centos镜像(默认最新镜像) docker search centos docker pull centos 2.建立本机对应docker-centos ...

  4. xenomai内核解析之xenomai的组成结构

    @ 目录 一.xenomai 3 二.xenomai3 结构 这是第二篇笔记. 一.xenomai 3 从xenomai3开始支持两种方式构建linux实时系统,分别是cobalt 和 mercury ...

  5. Jetson AGX Xavier/Ubuntu更改pip3源

    pip3换源: 修改~/.pip/pip.conf,如果没有这个文件,就创建一个. 内容如下: [global]index-url = https://pypi.tuna.tsinghua.edu.c ...

  6. 自己封装函数,实现数组的内置方法indexOf的功能

    在学习或开发过程中,经常会有朋友需要使用到一个数组方法-indexOf,这里我们先来谈谈它的功能:返回指定数据所在的索引,如果没有则返回-1. 那么我们在使用时通常是直接使用它这个数组内置方法 今天这 ...

  7. F. Multicolored Markers 暴力+二分

    F. Multicolored Markers 题目大意: 给你 a个红块 b个蓝块 拼成一个实心矩形,并且要求红块或者蓝块自成一个矩形,问形成的这个矩形最小的周长是多少. #include < ...

  8. 关于使用ffmpeg的一些牢骚

    一.啰嗦几句 好几年不写博客了,一是工作计算机都加密了没法编辑提交:二是各种语言混用,什么都会就是什么都不会,delphi.c#.vb.python.c++要说我精通啥,啥也不精,所以不敢乱写. 最近 ...

  9. 最终父类【根类】:Object类&Objects类

    一.java.lang.Object类 1.Object类介绍 Object类是所有类的父类.一个类都会直接或间接继承自该类: ​ 该类中提供了一些非常常用的方法! 2.toString()方法 A: ...

  10. qt绘制甘特图

    重写paintEvent事件,代码如下 void xx::paintEvent(QPaintEvent *event){ QPainter painter(this); //绘制x,y轴,_maxWi ...