CF1019C.Sergey's problem

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Sergey just turned five years old! When he was one year old, his parents gave him a number; when he was two years old, his parents gave him an array of integers. On his third birthday he received a string. When he was four, his mother woke him up in a quiet voice, wished him to be a good boy and gave him a rooted tree. Today he celebrates his birthday again! He found a directed graph without loops as a present from his parents.

Since Sergey is a very curious boy, he immediately came up with a thing to do. He decided to find a set QQ of vertices in this graph, such that no two vertices x,y∈Qx, y \in Q are connected by an edge, and it is possible to reach any vertex z∉Qz \notin Q from some vertex of QQ in no more than two moves.

After a little thought, Sergey was able to solve this task. Can you solve it too?

A vertex yy is reachable from a vertex xx in at most two moves if either there is a directed edge (x,y)(x,y), or there exist two directed edges (x,z)(x,z) and (z,y)(z, y) for some vertex zz.

谢尔盖刚刚满五岁!他一岁时,父母送给他一个数字;两岁时,父母送给他一个整数数组;三岁生日时,他收到了一个字符串;四岁时,妈妈用轻柔的声音将他唤醒,祝他做个好孩子,并送给他一棵有根树。今天他又迎来了生日!父母送给他一份特别的礼物——一个无环有向图。

由于谢尔盖是个非常好奇的男孩,他立刻想到了一件有趣的事情:他决定在该图中找出一个顶点集合 QQ,使得其中任意两个顶点 x,y∈Qx, y \in Q 之间均不存在有向边,并且对任意不在 QQ 中的顶点 zz(即 z∉Qz \notin Q),都存在某个顶点 q∈Qq \in Q,使得从 qq 出发至多经过两步即可到达 zz。

稍作思考后,谢尔盖成功解决了这个问题。你也能解决它吗?

顶点 yy 可由顶点 xx 在至多两步内到达,当且仅当满足以下条件之一:存在一条从 xx 到 yy 的有向边 (x,y)(x,y);或存在某个顶点 zz,使得同时存在两条有向边 (x,z)(x,z) 和 (z,y)(z, y)。

输入格式

The first line of input contains two positive integers nn and mm (1≤n≤1 000 0001 \le n \le 1\,000\,000, 1≤m≤1 000 0001 \le m \le 1\,000\,000) — the number of vertices and the number of edges in the directed graph.

Each of the following mm lines describes a corresponding edge. Each one contains two integers aia_i and bib_i (1≤ai,bi≤n1 \le a_i, b_i \le n, ai≠bia_i \ne b_i) — the beginning and the end of the ii-th edge. The graph may contain multiple edges between the same pair of vertices.

输入的第一行包含两个正整数 nn 和 mm(1≤n≤1 000 0001 \le n \le 1\,000\,000,1≤m≤1 000 0001 \le m \le 1\,000\,000)—— 分别表示有向图的顶点数和边数。

接下来的 mm 行每行描述一条对应的边。每行包含两个整数 aia_i 和 bib_i(1≤ai,bi≤n1 \le a_i, b_i \le n,ai≠bia_i \ne b_i)—— 分别表示第 ii 条边的起点和终点。该图可能在相同一对顶点之间包含多条边。

输出格式

First print the number kk — the number of selected vertices. Then print kk distinct integers — the indices of the selected vertices.

If multiple answers exist you can output any of them. In particular, you don't have to minimize the number of vertices in the set. It is guaranteed, that there is always at least one valid set.

首先输出一个整数 kk —— 所选顶点的数量。然后输出 kk 个互不相同的整数 —— 所选顶点的下标。

如果存在多个合法答案,你可以输出其中任意一个。特别地,你无需最小化所选顶点集合的大小。题目保证至少存在一个合法的顶点集合。

输入输出样例

  • 输入#1

    5 4
    1 2
    2 3
    2 4
    2 5

    输出#1

    4
    1 3 4 5
  • 输入#2

    3 3
    1 2
    2 3
    3 1

    输出#2

    1
    3

说明/提示

In the first sample, the vertices 1,3,4,51, 3, 4, 5 are not connected. The vertex 22 is reachable from vertex 11 by one edge.

In the second sample, it is possible to reach the vertex 11 in one move and the vertex 22 in two moves.

The following pictures illustrate sample tests and their answers.

在第一个样例中,顶点 1,3,4,51, 3, 4, 5 互不连通。顶点 22 可通过一条边从顶点 11 到达。

在第二个样例中,顶点 11 可在一步内到达,顶点 22 可在两步内到达。

以下图片展示了样例测试及其答案。

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

首页