← 返回 google 的题目列表Determine Players with Fixed Rankings
类型:online_judge
Problem: Determine Players with Fixed Rankings
There are N players, numbered from 1 to N, and M match results. Each result (winner, loser) means that winner defeated loser.
The win relation is transitive: if A defeated B and B defeated C, then A can be inferred to have defeated C.
A player's final rank is considered determinable if the available results and their transitive implications determine that player's win/loss relation with every other player. In other words, for every other player q, either p can be inferred to defeat q, or q can be inferred to defeat p.
Output all players whose ranks are determinable, in ascending order, and the number of such players.
The input is guaranteed to contain no contradictions: there are no distinct players a and b such that both a defeats b and b defeats a can be inferred.
Input Format
First line: two integers N M
Next M lines: two integers winner loser
Output Format
First line: the number K of players with determinable ranks
Second line: K player IDs in ascending order. If K = 0, output an empty line.
Constraints
1 <= N <= 500
0 <= M <= N * (N - 1) / 2
Example
Input
5 5
1 2
2 3
3 4
4 5
1 3
Output
5
1 2 3 4 5
Example
Input
5 5
1 2
2 3
3 4
4 5
1 3
Output
5
1 2 3 4 5