CF1019C Sergey's problem

    • 30通过
    • 78提交
  • 题目来源 CodeForces 1019C
  • 评测方式 RemoteJudge
  • 标签
  • 难度 省选/NOI-
  • 时空限制 2000ms / 256MB


  • 提示:收藏到任务计划后,可在首页查看。
  • 体验新版界面

    最新讨论 显示

    推荐的相关题目 显示


    $$Sergey's problem$$

    $\space \space \space \space Sergey$五岁了!当他一岁的时候,他的父母给了他一个数;当他两岁的时候,他的父母给了他一个整数数组;三岁时他收到了一个字符串;当他四岁时,他的妈妈轻轻地叫醒他,让他做一个好孩子并给了他一棵有根树。今天是他的五岁生日!他收到了一份来自父母的有向图(没有自环)。


    第一行输入 $n$ , $m$; 表示有 $n$ 个点 $m$ 条边。 $(1<=n,m<=100,000)$
    接下来的 $m$ 行每行给出 $a$,$b$;表示 $a$ 到 $b$ 有一条有向边,可能出现重边。

    第一行输出 $k$ ,表示集合中有 $k$ 个点。
    第二行输出 $k$ 个整数表示集合中的点。保证有解。


    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 $ Q $ of vertices in this graph, such that no two vertices $ x, y \in Q $ are connected by an edge, and it is possible to reach any vertex $ z \notin Q $ from some vertex of $ Q $ in no more than two moves.

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

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



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

    Each of the following $ m $ lines describes a corresponding edge. Each one contains two integers $ a_i $ and $ b_i $ ( $ 1 \le a_i, b_i \le n $ , $ a_i \ne b_i $ ) — the beginning and the end of the $ i $ -th edge. The graph may contain multiple edges between the same pair of vertices.


    First print the number $ k $ — the number of selected vertices. Then print $ k $ 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.


    输入样例#1: 复制
    5 4
    1 2
    2 3
    2 4
    2 5
    输出样例#1: 复制
    1 3 4 5 
    输入样例#2: 复制
    3 3
    1 2
    2 3
    3 1
    输出样例#2: 复制


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

    In the second sample, it is possible to reach the vertex $ 1 $ in one move and the vertex $ 2 $ in two moves.

    The following pictures illustrate sample tests and their answers.