CF915D.Almost Acyclic Graph
提高+/省选-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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).
你被给定一个由 n 个顶点和 m 条边组成的有向图(每条边是有方向的,因此只能沿一个方向遍历)。你最多可以删除其中一条边。
你能否通过最多删除一条边,使该图变为无环图?当且仅当一个有向图不包含任何环(即一条非空路径,其起点与终点为同一顶点)时,该图被称为无环图。
输入格式
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).
第一行包含两个整数 n 和 m(2≤n≤500,1≤m≤min(n(n−1),100000)),分别表示顶点数和边数。
接下来是 m 行。每行包含两个整数 u 和 v,表示一条从顶点 u 指向顶点 v 的有向边(1≤u,v≤n,且 u=v)。每个有序对 (u,v) 至多出现一次(即从 u 到 v 的有向边至多有一条)。
输出格式
If it is possible to make this graph acyclic by removing at most one edge, print YES. Otherwise, print NO.
如果可以通过最多删除一条边使该图变为无环图,则输出 YES;否则输出 NO。
输入输出样例
输入#1
3 4 1 2 2 3 3 2 3 1
输出#1
YES
输入#2
5 6 1 2 2 3 3 2 3 1 2 1 4 5
输出#2
NO
说明/提示
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.
在第一个例子中,你可以删除边
,从而使图变为无环图。
在第二个例子中,你至少需要删除两条边(例如
和
),才能使图变为无环图。
输入解题思路,AI测评打分。不知道怎么写?