#P15194. [SWERC 2019] Bird Watching

[SWERC 2019] Bird Watching

题目描述

Kiara studies an odd species of birds which travel in a very peculiar way. Their movements are best explained using the language of graphs: there exists a directed graph GG where the nodes are trees, and a bird can only fly from a tree TaT_a to TbT_b when (Ta,Tb)(T_a, T_b) is an edge of GG.

Kiara does not know the real graph GG governing the flight of these birds but, in her previous field study, Kiara has collected data from the journey of many birds. Using this, she has devised a graph PP explaining how they move. Kiara has spent so much time watching them that she is confident that if a bird can fly directly from aa to bb, then she has witnessed at least one such occurrence. However, it is possible that a bird flew from aa to bb to cc but she only witnessed the stops aa and cc and then added (a,c)(a,c) to PP. It is also possible that a bird flew from aa to bb to cc to dd and she only witnessed aa and dd, and added (a,d)(a,d) to PP, etc. To sum up, she knows that PP contains all the edges of GG and that PP might contain some other edges (a,b)(a,b) for which there is a path from aa to bb in GG (note that PP might not contain all such edges).

For her next field study, Kiara has decided to install her base next to a given tree TT. To be warned of the arrival of birds on TT, she would also like to install detectors on the trees where the birds can come from (i.e. the trees TT' such that there is an edge (T,T)(T', T) in GG). As detectors are not cheap, she only wants to install detectors on the trees TT' for which she is sure that (T,T)(T', T) belongs to GG.

Kiara is sure that an edge (a,b)(a,b) belongs to GG when (a,b)(a,b) is an edge of PP and all the paths in PP starting from aa and ending in bb use the edge (a,b)(a,b). Kiara asks you to compute the set S(T)S(T) of trees TT' for which she is sure that (T,T)(T', T) is an edge of GG.

输入格式

The input describes the graph PP. The first line contains three space-separated integers NN, MM, and TT: NN is the number of nodes of PP, MM is the number of edges of PP and TT is the node corresponding to the tree on which Kiara will install her base.

The next MM lines describe the edges of the graph PP. Each contains two space-separated integers aa and bb (0a,b<N0 \leq a, b < N and aba \neq b) stating that (a,b)P(a,b) \in P. It is guaranteed that the same pair (a,b)(a,b) will not appear twice.

输出格式

Your output should describe the set S(T)S(T). The first line should contain an integer LL, which is the number of nodes in S(T)S(T), followed by LL lines, each containing a different element of S(T)S(T). The elements of S(T)S(T) should be printed in increasing order, with one element per line.

3 3 2
0 1
0 2
1 2
1
1
6 8 2
0 1
0 2
1 2
2 0
2 3
3 4
4 2
2 5
2
1
4

提示

Sample Explanation 1

The graph corresponding to this example is depicted on the right. The node 11 belongs to S(2)S(2) because the (only) path from 11 to 22 uses (1,2)(1,2). The node 00 does not belong to S(2)S(2) because the path 0120 \to 1 \to 2 does not use the edge (0,2)(0,2).

:::align{center} :::

Sample Explanation 2

The graph corresponding to this example is depicted on the right. For the same reason as in Sample 1, the node 00 does not belong to S(2)S(2) while 11 does. The nodes 33 and 55 do not belong to S(2)S(2) because we do not have edges (3,2)(3,2) or (5,2)(5,2). Finally 44 belongs to S(2)S(2) because all paths from 44 to 22 use the edge (4,2)(4,2).

:::align{center} :::

Limits

  • 1N,M1000001 \leq N, M \leq 100\,000;
  • 0T<N0 \leq T < N.