CodeForces 915D Almost Acyclic Graph
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的更多相关文章
- codeforces 915D Almost Acyclic Graph 拓扑排序
大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环 解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就YES,都不行就NO 复杂度O(N*M)看起来不超时 但是实现了以后发现 ...
- Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...
- 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环
[题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...
- 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 ...
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- D. Almost Acyclic Graph 判断减一条边能不能得到DAG
D. Almost Acyclic Graph time limit per test 1 second memory limit per test 256 megabytes input stand ...
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
- Almost Acyclic Graph CodeForces - 915D (思维,图论)
大意: 给定无向图, 求是否能删除一条边后使图无环 直接枚举边判环复杂度过大, 实际上删除一条边可以看做将该边从一个顶点上拿开, 直接枚举顶点即可 复杂度$O(n(n+m))$ #include &l ...
- Almost Acyclic Graph Codeforces - 915D
以前做过的题都不会了.... 此题做法:优化的暴力 有一个显然的暴力:枚举每一条边试着删掉 注意到题目要求使得图无环,那么找出图上任意一个环,都应当要在其某一处断开(当然没有环是YES) 因此找出图中 ...
随机推荐
- django 实用工具dj-database-url 快速配置数据库
dj-database-url Github>>> django快速配置多种数据库 $ pip install dj-database-url Configure your data ...
- Python学习---IO的异步[gevent+Grequests模块]
安装gevent模块 pip3 install gevent Gevent实例 import gevent import requests from gevent import monkey # so ...
- Linux setfacl/getfacl命令详解
setfacl,命令名,设置文件访问控制列表,即ACL规则.而Acl(Access Control List)就是访问控制列表 setfacl常见命令参数 setfacl 2.2.51 -- 设定文件 ...
- Spring Boot+MyBabits静态连接多个数据库
1.修改.properties first.datasource.jdbc-url=jdbc:mysql://localhost/forwind first.datasource.username=r ...
- 铁乐学python_day05-作业
1,有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2": [&q ...
- C++浅拷贝和深拷贝的区别
C++浅拷贝和深拷贝的区别 2012-04-24 21:22 11454人阅读 评论(6) 收藏 举报 c++deleteclass编译器c c++默认的拷贝构造函数是浅拷贝 浅拷贝就是对象的数据成员 ...
- Java 实现对文件系统的监控
在开发中经常会用到监控文件或是目录的状态,如果你还在手写轮巡扫描文件的话,那你久out了. 1. Commons io为我们提供了一套可靠.高性能的一套文件系统监控API 1.1. 需要的jar包如下 ...
- 判断浏览器是否为ie的最快方法
var ie = !-[1,]; alert(ie); 只要6 bytes!它利用了IE与标准浏览器在处理数组的toString方法的差异做成的.对于标准游览器,如果数组里面最后一个字符为逗号,JS引 ...
- BZOJ4517:[SDOI2016]排列计数(组合数学,错排公式)
Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是 ...
- CF613D:Kingdom and its Cities(树形DP,虚树)
Description 一个王国有n座城市,城市之间由n-1条道路相连,形成一个树结构,国王决定将一些城市设为重要城市. 这个国家有的时候会遭受外敌入侵,重要城市由于加强了防护,一定不会被占领.而非重 ...