#B3644. Family Descendant Ordering
Family Descendant Ordering
Family Descendant Ordering
You are given a messy family tree in which the descendant relationships are provided as a directed acyclic graph. Each edge (u,v) indicates that v is a descendant of u. Your task is to output a sequence of all individuals such that every person appears before all of their descendants. To ensure a unique answer, you are required to output the lexicographically smallest valid ordering (i.e. always choose the smallest numbered individual available at each step).
The descendant information is provided as pairs of integers. It is guaranteed that the graph does not contain cycles.
inputFormat
The first line contains two integers n
and m
where n
is the number of individuals (numbered from 1 to n) and m
is the number of descendant relationships.
Each of the following m
lines contains two space-separated integers u
and v
, indicating that v
is a descendant of u
.
outputFormat
Output a single line containing n
space-separated integers representing the lexicographically smallest ordering such that for every descendant relationship (u, v), u
appears before v
in the sequence.
sample
4 3
1 2
1 3
3 4
1 2 3 4