#P17459. [GESP202609 七级] 必经之路
[GESP202609 七级] 必经之路
Problem Description
Given a directed graph with nodes and edges, the nodes in are numbered in order. The -th edge () goes from node to node .
Any node in with in-degree can be a valid starting node, and any node with out-degree can be a valid ending node.
If all possible paths in from any valid starting node to any valid ending node must pass through node , then is called a mandatory node. Note that a mandatory node can be a valid starting node or a valid ending node.
Please find the indices of all mandatory nodes in .
For example, in the figure below, the valid starting nodes are node and node , and the valid ending nodes are node and node .
(1) (5)---->(7)
\ ^ \ ^
v / v /
(3) / (6)
^ \ / \
/ v / v
(2)---->(4) (8)
All paths from valid starting nodes to valid ending nodes are:
Therefore, there are two mandatory nodes, with indices .
Input Format
The first line contains two positive integers , representing the number of nodes and the number of edges in the directed graph .
The next lines each contain two positive integers , representing a directed edge from node to node .
It is guaranteed that has at least one valid starting node and at least one valid ending node, and that there is at least one path from a valid starting node to a valid ending node. Also, there are no isolated nodes (i.e., nodes whose out-degree and in-degree are both ).
Output Format
The first line contains an integer, representing the number of mandatory nodes .
If mandatory nodes exist, then the second line outputs the indices of all mandatory nodes in in increasing order.
8 9
1 3
2 3
3 4
4 5
5 6
6 7
6 8
2 4
5 7
2
4 5
8 9
1 3
2 3
3 4
4 5
5 6
6 7
6 8
2 5
4 7
0
Hint
For of the test points, it is guaranteed that and .
For all test points, it is guaranteed that and . It is guaranteed that has at least one valid starting node and at least one valid ending node, and that there is at least one path from a valid starting node to a valid ending node. Also, there are no isolated nodes (i.e., nodes whose out-degree and in-degree are both ).
Translated by ChatGPT 5