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) 因此找出图中 ...
随机推荐
- TreeView控件概述、属性与方法
1.作用:用于显示Node结点的分层列表.2.添加到控件箱菜单命令:工程 | 部件,在部件对话框中选择:Microsoft Windows Common Controls 6.03.TreeView控 ...
- LocationCoder 地图经纬度解析
LocationCoder 地图经纬度解析 其实,在地图里面将地图解析成有意义的地址,或者把地址转换成有意义的经纬度都是很容易的事情,只是我将其封装了支持KVO,通知中心,block取结果,代理取结果 ...
- jQuery复制table header到表格的最下面
为了让table具有更好的可读性,我们可以将表格的header信息克隆一份到表格的底部,这种特效通过JQuery就很容易实现: 1 2 3 4 5 var $tfoot = $(''); $($('t ...
- 1854. [SCOI2010]游戏【二分图】
Description lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性 ...
- Sequelize-nodejs-9-Scopes
Scopes作用域 Scoping allows you to define commonly used queries that you can easily use later. Scopes c ...
- 为什么 token可以防止 csrf?
Token被用户端放在Cookie中(不设置HttpOnly),同源页面每次发请求都在请求头或者参数中加入Cookie中读取的Token来完成验证.CSRF只能通过浏览器自己带上Cookie,不能操作 ...
- CUDA和OpenGL互操作经典博文赏析和学习
1.使用cuda+opengl图形互操作性实现MPR.原学位论文学习:实时交互的医学图像可视化.在该论文的第5.1.1节. 2.cuda与opengl互操作之PBO 3.cuda与opengl互操作之 ...
- P1474 货币系统 Money Systems
题目描述 母牛们不但创建了它们自己的政府而且选择了建立了自己的货币系统.由于它们特殊的思考方式,它们对货币的数值感到好奇. 传统地,一个货币系统是由1,5,10,20 或 25,50, 和 100的单 ...
- no persistent volumes available for this claim and no storage class is set FailedBinding -- nfs --存储
添加PV标签oc label pv registrypv disktype=registry oc get pv --show-labels NAME CAPACITY ACCESSMODES REC ...
- Altium Ddesigner 栅格 含义
栅格分为可视栅格(Visible Grid).捕获栅格(snap grid).元件放置捕获栅格(Component Grid).电气栅格(Electrical Grid). 可视栅格:就是编辑过程中看 ...