Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
2020-12-13 16:38
标签:stream void algorithm origin put tps down any pop CodeForces - 915D time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 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 (2?≤?n?≤?500, 1?≤?m?≤?min(n(n?-?1),?100000)) — 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 (1?≤?u,?v?≤?n, u?≠?v). Each ordered pair (u,?v) 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 Copy output Copy input Copy output Copy Note In the first example you can remove edge , and the graph becomes acyclic. In the second example you have to remove at least two edges (for example, and ) in order to make the graph acyclic. 给你有一个n个点,m个边的有向图。 问是否可以只删除一个边,使整个图无环。 枚举每一个节点,将该节点的入度减去1,先不用管删除的是哪个边,删除一个终点是i节点的边的影响就是i的入度减去1. 然后通过拓扑排序在\(O(n+m)\) 的时间复杂度里可以判断出一个有向图是否有环。 所以整体的时间复杂度是\(O(n*(n+m))\) Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环) 标签:stream void algorithm origin put tps down any pop 原文地址:https://www.cnblogs.com/qieqiemin/p/11620866.htmlAlmost Acyclic Graph
3 41 22 33 23 1
YES
5 61 22 33 23 12 14 5
NO
题意:
思路:
代码:
#include
文章标题:Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
文章链接:http://soscw.com/essay/36353.html