CF1036G.Sources and Sinks

省选/NOI-

通过率:0%

时间限制:7.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an acyclic directed graph, consisting of nn vertices and mm edges. The graph contains no multiple edges and no self-loops.

The vertex is called a source if it has no incoming edges. The vertex is called a sink if it has no outgoing edges. These definitions imply that some vertices can be both source and sink.

The number of sources in the given graph is equal to the number of sinks in it, and each of these numbers doesn't exceed 2020.

The following algorithm is applied to the graph:

  1. if the graph has no sources and sinks then quit;
  2. choose arbitrary source ss, arbitrary sink tt, add an edge from tt to ss to the graph and go to step 11 (that operation pops ss out of sources and tt out of sinks). Note that ss and tt may be the same vertex, then a self-loop is added.

At the end you check if the graph becomes strongly connected (that is, any vertex is reachable from any other vertex).

Your task is to check that the graph becomes strongly connected no matter the choice of sources and sinks on the second step of the algorithm.

给你一个有向无环图,包含 nn 个顶点和 mm 条边。该图不含重边和自环。

若一个顶点没有入边,则称其为源点(source);若一个顶点没有出边,则称其为汇点(sink)。根据上述定义,某些顶点可能既是源点又是汇点。

给定图中源点的数目等于汇点的数目,且该数目不超过 2020。

对图执行如下算法:

  1. 若图中既无源点也无汇点,则退出;
  2. 任选一个源点 ss 和一个汇点 tt,向图中添加一条从 tt 指向 ss 的边,然后返回步骤 1(该操作将使 ss 不再是源点、tt 不再是汇点)。注意:ss 和 tt 可能是同一个顶点,此时添加的是一个自环。

最终,你需检查图是否变为强连通图(即任意顶点均可从其余任意顶点到达)。

你的任务是:判断无论在算法第 2 步中如何选择源点与汇点,最终得到的图是否一定强连通。

输入格式

The first line contains two integers nn and mm (1≤n,m≤1061 \le n, m \le 10^6) — the number of vertices and the number of edges in the graph, respectively.

Each of the next mm lines contains two integers vi,uiv_i, u_i (1≤vi,ui≤n1 \le v_i, u_i \le n, vi≠uiv_i \ne u_i) — the description of the ii-th edge of the original graph.

It is guaranteed that the number of sources and the number of sinks in the graph are the same and they don't exceed 2020. It is guaranteed that the given graph contains no multiple edges. It is guaranteed that the graph contains no cycles.

第一行包含两个整数 nn 和 mm(1≤n,m≤1061 \le n, m \le 10^6),分别表示图中顶点的数量和边的数量。

接下来的 mm 行,每行包含两个整数 vi,uiv_i, u_i(1≤vi,ui≤n1 \le v_i, u_i \le n,且 vi≠uiv_i \ne u_i),表示原图中第 ii 条边的描述。

保证图中源点(source)的数量与汇点(sink)的数量相等,且均不超过 2020。保证给定图中不存在重边。保证图中不含环。

输出格式

Print "YES" if the graph becomes strongly connected no matter the choice of sources and sinks on the second step of the algorithm. Otherwise print "NO".

如果无论在算法的第二步中如何选择源点和汇点,该图均变为强连通图,则输出 "YES";否则输出 "NO"。

输入输出样例

  • 输入#1

    3 1
    1 2

    输出#1

    NO
  • 输入#2

    3 3
    1 2
    1 3
    2 3

    输出#2

    YES
  • 输入#3

    4 4
    1 2
    1 3
    4 2
    4 3

    输出#3

    YES

输入解题思路,AI测评打分。不知道怎么写?

首页