Viking Village
Time limit2sMemory limit1024 MB
Given a directed graph, decide whether a rooted tree has the same reachable set for every node, and print its parent array if one exists.
- Level
Medium7 of 10
- Topics
- Graph, Topological sort, DFS
- Solved
- No attempts yet
Problem
Hiccup noticed that the chain of command in his village is very tangled. The village has Vikings, numbered from to . Hiccup wrote down pairs , , meaning that the -th Viking can pass a task to the -th Viking. A Viking who receives a task may pass it on to any Viking that the first one can pass tasks to.
We say that Viking is under Viking if a task from Viking can reach Viking .
Hiccup finds this scheme too complicated and inefficient, so he wants to introduce a new one. The new scheme has exactly one leader, to whom nobody can pass a task. Every Viking other than the leader receives tasks from exactly one other Viking. Tasks from the leader must be able to reach every Viking in the village. In other words, the new scheme is a rooted tree with edges directed away from the root. Hiccup also wants the set of Vikings under each Viking to be the same in the new scheme and in the old one, for every .
Hiccup has not been able to find such a scheme. Determine whether one exists, and if it does, find any one of them.
Input
The first line contains two integers and , the number of Vikings and the number of pairs in the old scheme ().
Each of the next lines contains a pair of integers , , meaning that in the old scheme the -th Viking can pass a task to the -th Viking (, ). The same pair may appear more than once.
Output
If the required scheme does not exist, print No.
Otherwise, print Yes on the first line. On the second line, print integers . If the -th Viking is the leader of the new scheme, then . Otherwise, is the number of the Viking who passes tasks to the -th Viking in the new scheme (, ).