#P2853. Picnic Pastures

    ID: 16111 Type: Default 1000ms 256MiB

Picnic Pastures

Picnic Pastures

Farmer John's cows are having a picnic! Each of his K cows is grazing in one of N pastures, numbered 1 through N. The pastures are connected by M one-way paths. Note that no path connects a pasture to itself.

The cows want to gather in the same pasture for their picnic. However, due to the one-way paths, not every pasture may be reachable by every cow. A cow can always stay in its own pasture, so the starting pasture is always reachable for that cow. Your task is to determine the number of pastures that are reachable by all cows.

The input guarantees that \(1 \leq K \leq 100\), \(1 \leq N \leq 1000\), and \(1 \leq M \leq 10000\).

Hint: Use graph traversal (BFS or DFS) from each cow's starting pasture and count the number of nodes that are visited by every cow.

inputFormat

The first line contains three integers: K (the number of cows), N (the number of pastures), and M (the number of one-way paths).

The second line contains K integers, where the \(i\)th integer denotes the starting pasture of the \(i\)th cow.

Each of the following M lines contains two integers u and v, indicating a one-way path from pasture u to pasture v.

outputFormat

Output a single integer: the number of pastures that are reachable by all cows.

sample

2 3 2
1 2
1 3
2 3
1