Sinks of a Graph
Time limit1sMemory limit128 MB
In each directed graph, list every node v such that every node reachable from v can reach v back.
- Level
Medium7 of 10
- Topics
- Graph, DFS, Implementation, Sorting
- Solved
- No attempts yet
Problem
You are given a directed graph .
For any two nodes , if there is a path from to using only edges in , we write .
A node is called a sink if every node reachable from has a path back to ; that is, if it satisfies the following condition:
The set of all sinks of is denoted .
Given the graph , compute .
Input
The input consists of several test cases.
The first line of each test case contains the number of nodes () and a non-negative integer (). This means and the number of edges is .
Then follow integer pairs separated by whitespace, where each pair denotes an edge . These integers may span multiple lines.
The input ends with a value of equal to ; such a test case must not be processed, and the program should terminate.
Output
For each test case, print all nodes in on one line, sorted in ascending order and separated by spaces. If is empty, print an empty line.