#C4303. Document Migration Order Determination

    ID: 47827 Type: Default 1000ms 256MiB

Document Migration Order Determination

Document Migration Order Determination

You are given n documents labeled from 1 to n and m dependency relations. Each dependency is a directed relation from document u to document v (denoted as u → v) indicating that document u must be migrated before document v. In addition, you are given an integer k and a list of k critical document identifiers. Your task is to determine a valid migration order (i.e. a topological ordering) of all documents that respects the given dependencies and thereby includes all critical documents.

The problem can be formally defined as follows: Given integers \(n\) and \(m\), a set of \(m\) directed edges represented as pairs \((u,v)\), an integer \(k\) and a list of \(k\) critical document IDs, find a permutation \(P\) of \(\{1, 2, \ldots, n\}\) such that for every dependency \(u \to v\), \(u\) appears before \(v\) in \(P\). If such an ordering does not exist (i.e. if there is a cycle in the dependency graph), output [] (an empty list).

If multiple valid orderings exist, you may output any of them.

inputFormat

The first line of the input contains two integers \(n\) and \(m\) separated by a space.

The next \(m\) lines each contain two integers \(u\) and \(v\), representing a dependency (\(u \to v\)).

The following line contains an integer \(k\) indicating the number of critical documents.

The last line contains \(k\) integers which represent the list of critical document IDs.

outputFormat

If a valid migration order exists, output \(n\) integers separated by spaces representing the order in which the documents should be migrated. If no valid ordering exists (i.e. the dependency graph has a cycle), output [] (without quotes).

## sample
4 3
1 2
2 3
3 4
1
2
1 2 3 4

</p>