#K66852. Valid Topological Order for Any DAG
Valid Topological Order for Any DAG
Valid Topological Order for Any DAG
You are given an integer n representing the number of tasks numbered from 1 to n and a permutation of these tasks, which represents a candidate topological order. Your task is to determine whether this order can be the topological order of some directed acyclic graph (DAG).
Recall that a topological order for a DAG is an ordering of its vertices such that for every directed edge \(u \rightarrow v\), vertex u comes before v in the ordering. One can always construct a DAG with the given permutation as a valid topological order. For instance, consider the permutation \(a_1, a_2, \dots, a_n\). One can create a DAG with edges \(a_i \rightarrow a_j\) for all \(i < j\) to satisfy the topological order property.
Thus, every permutation of tasks is a valid topological order for some DAG. Your solution should reflect this fact.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)), representing the number of tasks.
- The second line contains \(n\) space-separated integers representing a permutation of the tasks, which are the task identifiers.
outputFormat
Output to stdout a single line containing Yes
(without quotes), since any permutation can form a valid topological order for some DAG.
4
1 2 3 4
Yes
</p>