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) 因此找出图中 ...
随机推荐
- 关于解决sql2012编辑器对象名无效问题
出现以下情况: 解决办法: 选择“编辑”——“Intellisense”——“刷新本地缓存” 或者按Ctrl+Shift+R组合键
- iOS设计模式 - 生成器
iOS设计模式 - 生成器 原理图 说明 1. 将构建复杂对象的过程拆分成一个一个的模块,通过统一的指导者来指导对象的构建过程称之为生成器模式 2. 生成器模式适合用于构建组合的对象 源码 https ...
- 第八章 计时器(DIGCLOCK)
/*-------------------------------------- DIGCLOCK.C -- Digital Clock (c) Charles Petzold, 1998 ----- ...
- November 26th 2016 Week 48th Saturday
All growth is a leap in the dark. 所有的成长都是黑暗中的一跃. But it is a dark and long night, I can't see any st ...
- Weblogic 设置优先引用web项目的jar包
在WEB-INF/weblogic.xml中进行如下配置: <container-descriptor> <prefer-web-inf-classes>true</pr ...
- 【ansible】Windows开启远程控制错误解决方案:无法检查防火墙状态
这个在老版本的Windows系统才有这种bug.例如Windows 2008 R2和Windows 7,如果你的系统的阿里云的Windows server 2008 R2,无需装这个,好像阿里云的系统 ...
- CSS控制图片和文字在同一行显示且对齐的3种方法
CSS控制图片和文字在同一行显示且对齐的3种方法 在 HTML 代码中,有时会需要在文字旁边加上一个图标. 默认情况,是图片置顶对齐,文字置底对齐,所以通常图片高,文字低,不能水平居中对齐. 常见方法 ...
- c# datetime用法总结
备忘:YYYY-mm-dd HH:MM:SS部分解释 d 月中的某一天.一位数的日期没有前导零. dd 月中的某一天.一位数的日期有一个前导零. ddd 周中某天的缩写名称,在 Abbreviated ...
- class , field , method
Class类 由于Class类没有公共构造方法,所以创建Class的对象的方法有以下几种: 1.通过Class.forName静态方法返回Class类的一个实例 2.通过类名.class来获取一个Cl ...
- Codeforces 1118 F2. Tree Cutting (Hard Version) 优先队列+树形dp
题目要求将树分为k个部分,并且每种颜色恰好在同一个部分内,问有多少种方案. 第一步显然我们需要知道哪些点一定是要在一个部分内的,也就是说要求每一个最小的将所有颜色i的点连通的子树. 这一步我们可以将所 ...