#P5191. HOLMES: Guaranteed Events Propagation

    ID: 18427 Type: Default 1000ms 256MiB

HOLMES: Guaranteed Events Propagation

HOLMES: Guaranteed Events Propagation

In this problem, there are \(D\) events numbered from \(1\) to \(D\). Holmes has \(M\) inference rules of the form \(A \rightarrow B\), meaning that if event \(A\) occurs then event \(B\) will definitely occur. Note that this does not mean that if \(A\) does not occur then \(B\) will not occur.

The \(M\) inference rules may form chains (e.g. \(A \rightarrow B \rightarrow C\)), but no cycles will ever be formed. It is also possible that an event \(X\) may have several incoming rules, say \(Y_1\rightarrow X, Y_2\rightarrow X, \dots\). In that case, event \(X\) is guaranteed to occur if and only if at least one of the events \(Y_1, Y_2, \dots\) is guaranteed to occur.

You are also given a list of \(N\) events \(S_1, S_2, \dots, S_N\) that are known to occur. Your task is to determine which events are guaranteed to occur based on the inference rules and these initial events.

inputFormat

The first line of the input contains three integers \(D\), \(M\) and \(N\) representing the total number of events, the number of inference rules, and the number of initially occurring events, respectively.

Then \(M\) lines follow, each containing two space-separated integers \(A\) and \(B\) (denoted \(A \rightarrow B\)) indicating that if event \(A\) occurs, then event \(B\) will definitely occur.

The last line contains \(N\) space-separated integers \(S_1, S_2, \dots, S_N\) representing the events that are known to occur.

outputFormat

Output in a single line all the events that are guaranteed to occur, in increasing order, separated by a single space.

sample

6 4 2
1 2
2 3
2 4
5 6
1 5
1 2 3 4 5 6