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) 因此找出图中 ...
随机推荐
- js过滤HTML标签以及
function removeHTMLTag(str) { str = str.replace(/<\/?[^>]*>/g,''); //去除HTML tag str = str.r ...
- 1.windows下Redis安装
参考文档:https://www.cnblogs.com/Leo_wl/p/6392196.html?utm_source=itdadao&utm_medium=referral Redis数 ...
- 用以替换系统NSLog的YouXianMingLog
用以替换系统NSLog的YouXianMingLog 这是本人自己使用并改良的用以替换系统NSLog的类,非常好用,以下是使用示例,现在开源出来并提供源码,好用的话顶一下吧^_^ 效果: YouXia ...
- MFC连接postgre数据库
配置环境: include:libpq-fe.h文件 lib:libpq.lib文件 dll:libeay32.dll,libiconv-2.dll,libintl-8.dll,libpq.dll,s ...
- (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
1.Map集合(重点)1.1 常用的方法 Set<Map.Entry<K,V>> entrySet() - 用于将Map集合转换为Set集合. 其中Map.Entry<K ...
- October 05th 2017 Week 40th Thursday
Happiness is to find someone who can give you warm and share your life together. 幸福,就是找一个温暖的人过一辈子. Y ...
- Sql Server 流程控制语句
T-SQL中用来编写流程控制模块的语句有:BEGIN...AND语句.IF...ELSE语句.CASE语句.WHILE语句.GOTO语句.BREAK语句.WAITFOR语句和RETURN语句. 批处理 ...
- 第二次作业:找Bug
引子 我真的想了一个小时,上哪里去找bug.我昨天还留意到一个bug,今天就不见了.灵光不断,我想起来了.我就要找大公司的产品的bug... 第一部分 调研, 评测 体验. <腾讯桌球>是 ...
- 学习日期时间控件 daterangepicker
aterangepicker 是一款日期时间控件,可选择“年,月,日,时,分,秒”,可选择单面板,也可选择双面板(起止时间). 单面板示例:daterangepicker 单面板 codepen 在线 ...
- 基于easyui开发Web版Activiti流程定制器详解(六)——Draw2d详解(二)
回顾: 上一篇我们介绍了Draw2d整体结构,展示了组件类关系图,其中比较重要的类有Node.Canvas.Command.Port.Connection等,这篇将进一步介绍Draw2d如何使用以及如 ...