Description

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\)

\(\left(2 \le n \le 500, 1 \le m \le \min\left(n \cdot\left(n - 1\right), 100000\right)\right)\) — 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 \(\left(1 \le u, v \le n, u \neq v\right)​\). Each ordered pair \(\left(u, v\right)​\) 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 \(2 \rightarrow 3\) and the graph becomes acyclic.

In the second example you have to remove at least two edges (for example, \(2 \rightarrow 1\) and \(2 \rightarrow 3\)) in order to make the graph acyclic.

题解

有向图无环当且仅当存在拓扑序,而删掉边\(\left(u, v\right)\)的作用是使点\(v\)的入度减一,尽管边的数量是\(100000\),但是对于同一个顶点,删掉不同入边的效果是等价的,所以我们只需要枚举每个顶点,将其入度减一,检查是否存在拓扑序即可。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 511;
vector<int> w[maxn];
int d1[maxn], d2[maxn];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
w[u].push_back(v);
++d1[v];
}
bool fg = false;
for (int i = 1; i <= n; ++i) {
if (d1[i] == 0) continue;
for (int j = 1; j <= n; ++j)
d2[j] = d1[j];
--d2[i];
queue<int> que;
int ct = 0;
for (int j = 1; j <= n; ++j) {
if (!d2[j]) {
que.push(j);
++ct;
}
}
while (!que.empty()) {
int u = que.front();
que.pop();
for (int v : w[u]) {
if (--d2[v] == 0) {
que.push(v);
++ct;
}
}
}
if (ct == n) {
fg = true;
break;
}
}
puts(fg ? "YES" : "NO");
return 0;
}

CodeForces 915D Almost Acyclic Graph的更多相关文章

  1. codeforces 915D Almost Acyclic Graph 拓扑排序

    大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环 解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就YES,都不行就NO 复杂度O(N*M)看起来不超时 但是实现了以后发现 ...

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

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

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

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

  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. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

  6. D. Almost Acyclic Graph 判断减一条边能不能得到DAG

    D. Almost Acyclic Graph time limit per test 1 second memory limit per test 256 megabytes input stand ...

  7. ACM - 最短路 - CodeForces 295B Greg and Graph

    CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...

  8. Almost Acyclic Graph CodeForces - 915D (思维,图论)

    大意: 给定无向图, 求是否能删除一条边后使图无环 直接枚举边判环复杂度过大, 实际上删除一条边可以看做将该边从一个顶点上拿开, 直接枚举顶点即可 复杂度$O(n(n+m))$ #include &l ...

  9. Almost Acyclic Graph Codeforces - 915D

    以前做过的题都不会了.... 此题做法:优化的暴力 有一个显然的暴力:枚举每一条边试着删掉 注意到题目要求使得图无环,那么找出图上任意一个环,都应当要在其某一处断开(当然没有环是YES) 因此找出图中 ...

随机推荐

  1. 用Model来计算cell的高度

    用Model来计算cell的高度 效果: 将计算cell高度的方法直接移植到Model当中,初始化的瞬间就计算好了高度,非常好用! 源码: Model // // Model.h // // Copy ...

  2. [翻译] UCZProgressView

    UCZProgressView UCZProgressView is a circular progress indicator with cool animations for image load ...

  3. Stored Properties 与 Computed Properties

    Stored Properties 与 Computed Properties About Swift Stored Properties In its simplest form, a stored ...

  4. cxfreeze打包python程序的方法说明(生成安装包,实现桌面快捷方式、删除快捷方式)

    一.cxfreeze基础 1.cxfreeze功能 python代码文件转exe方法有三种,分别是cx_freeze,py2exe,PyInstaller,这三种方式各有千秋,本人只用过py2exe和 ...

  5. 基于scrapyd爬虫发布总结

    一.版本情况 python以丰富的三方类库取得了众多程序员的认可,但也因此带来了众多的类库版本问题,本文总结的内容是基于最新的类库版本. 1.scrapy版本:1.1.0 D:\python\Spid ...

  6. php实现session入库

    为什么要把session存入数据库?有什么用? 可以:统计在线人数,现实多站点session共享(通行证),控制同个账号登入人数等. 要实现session的入库,有关键的几个基本知识: session ...

  7. October 08th 2017 Week 41st Sunday

    Talent wins games, but teamwork and intelligence wins championships. 才华让你赢得比赛,团队及智慧让你赢得冠军. But the m ...

  8. APUE 4.8 umask函数

  9. CentOS7 防火墙(firewall)的操作命令

    CentOS7 防火墙(firewall)的操作命令 安装:yum install firewalld 1.firewalld的基本使用 启动: systemctl start firewalld 查 ...

  10. 用Python爬虫爬取炉石原画卡牌图片

    前段时间看了点Python的语法以及制作爬虫常用的类库,于是动手制作了一个爬虫尝试爬取一些炉石原画图片.本文仅记录对特定目标网站的分析过程和爬虫代码的编写过程.代码功能很局限,无通用性,仅作为一个一般 ...