#P15508. [CEOI 2012] Network

[CEOI 2012] Network

Description

Our engineers have designed a communication network that consists of nodes and unidirectional direct communication channels (links) between some pairs of nodes. We say that a node qq is reachable from node pp on a path, if there is a sequence of different nodes p1,p2,,pkp_1, p_2, \ldots, p_k with p=p1p = p_1 and q=pkq = p_k, such that there is a link that transmits data from pip_i to pi+1p_{i+1} for every i=1,,k1i = 1, \ldots, k-1. This network has a central node rr, such that any other node pp can be reached from rr via a path, and for any pair of nodes pp and qq there is at most one path on which qq can be reached from pp. The maintainers plan to improve on the network, but have not yet decided how. One idea they are considering is to reassign the central node, therefore they want to know for each node how many nodes are reachable from it on a path. Another idea is to just decentralize the network, so they also want to know how they could introduce new links so that for any pair of nodes pp and qq, there is exactly one path on which the node qq can be reached from pp, and vice versa.

You are to write a program that computes the number of reachable nodes for every node (Subtask A), and also computes the minimum number of new links needed to make every node reachable in a unique way from every other node. Your program must give the list of new links, too (Subtask B).

Input Format

The first line of the input contains three integers, NN (1N1000001 \leq N \leq 100\,000) the number of nodes, MM (1M5000001 \leq M \leq 500\,000) the number of links, and rr (1rN1 \leq r \leq N) the central node. Nodes are numbered from 11 to NN. The next MM lines contain the description of the links. Each line contains two integers pp and qq separated by space, that corresponds to a link, which can transmit data from pp to qq.

Output Format

The first line of the output contains the solution to Subtask A: NN integers separated by space, where the iith number is the number of reachable nodes from node ii (including ii itself). The remaining lines contain the solution for Subtask B: The second line of the output contains one integer KK, the minimum number of new links needed to achieve the above property of the network. The next KK lines list the new links: each of lines contains two integers uu and vv separated by space, that corresponds to a new link transmitting data from node uu to node vv. If there are multiple solutions, your program should output only one; it does not matter which one.

11 12 3
3 2
2 1
2 4
4 5
4 6
6 2
6 7
3 8
8 9
9 10
9 11
10 8
1 6 11 6 1 6 1 4 4 4 1
5
1 3
5 4
7 6
11 9
8 3

Hint

Grading

In 50%50\% of the test cases NN is at most 1000010\,000.

Subtask A is worth 40%40\% of the points, Subtask B is worth the other 60%60\% of the points.

If you only solve Subtask B, you must write NN integers in the first line.