cswork / OAOpen practice workspace

AmazonOA #380

Location Of Data After Transfers

Estimated MediumJudge-ready

Topics (AI-labelled)HashingSimulation

The initial positions are pairwise distinct. In order, move the data at position from to position to; the original guarantees that at every step from holds data and to does not. Finally, output all positions that hold data in ascending order.

Input

The first line contains n m, the second line contains n initial positions, followed by m lines of from to. The original gives no value bounds; this site adds 0≤n,m≤200000 and each position ID is in −10^9..10^9. The input satisfies the original valid-move guarantee above, so when n=0, m can only be 0.

Output

The first line is the number of positions n, and the second line is the positions in ascending order; when n=0 the second line is empty.

Example 1

Input:

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

Output:

4
1 2 3 6

Example 2

Input:

1 2
0
0 -1
-1 2

Output:

1
2

Example 3

Input:

0 0

Output:

0

Example notes

The first example ends with 1 2 3 6. The original explanation of the second example mistakenly reused a different set of moves; this site explains it with the actual input 1→4, 4→7, 5→1, 7→3. The remaining examples end with only position 2 and with no positions, respectively.

Editorial

Loading solution…