#P5954. [JSOI2013] 侦探 JYY

[JSOI2013] 侦探 JYY

Problem Description

In the world of JSOI, there are NN different events (numbered from 11 to NN), and MM clues. Each clue corresponds to an ordered pair (x,y)(x,y), meaning that if event xx happens, it will cause event yy to happen.

Note: Clues are one-way. That is, if yy happens, it does not mean that xx must have happened.

Clues are transitive. That is, if there are clues (x,y)(x,y) and (y,z)(y,z), then if xx happens, it will cause zz to happen.

At the same time, since the world is reasonable, any event xx will never, through some clues, cause event xx itself to happen.

Also, the entire world contains only these MM clues. We do not assume that some events happen out of nowhere (just as Holmes would never believe a strange murder case is due to divine punishment). Specifically, for an event xx, if xx happens and there exists some event that could cause xx to happen, then at least one event that could cause xx to happen must have happened.

Now, given the MM clues in the world and DD events that are known to have happened, determine which events must have happened for sure.

Input Format

The first line contains three integers separated by spaces: NN, MM, and DD.

The next MM lines each contain two integers xx and yy, representing the clue (x,y)(x,y), satisfying 1x,yN1\leq x,y\leq N.

The next DD lines each contain a distinct integer between 11 and NN, representing the events that are known to have happened.

Output Format

Output one line containing at least DD strictly increasing positive integers separated by spaces, representing the events that JYY can deduce must have happened based on the MM clues and the DD known events.

3 2 1
1 3
2 3
3
3
4 4 1
1 2
1 3
2 4
3 4
4
1 2 3 4

Hint

Sample Explanation

In the first sample, since either event 11 or event 22 happening would cause event 33 to happen, we cannot determine which of the two events actually happened.

In the second sample, since event 44 happened, at least one of event 22 and event 33 happened. No matter which one happened, we can conclude that event 11 happened.

Finally, since event 11 happened, we can deduce that all 44 events must have happened.

Constraints

For 100%100\% of the data, 1DN103,1M1051\leq D\leq N\leq 10^3,1\leq M\leq 10^5

Translated by ChatGPT 5