Find the lexicographically smallest kernel among 2n knights where each knight challenges exactly one knight of the other house.
Hard8GraphGreedyNo attempts yetTime limit2sMemory limit512 MBJousting is a medieval contest in which two people ride horses at high speed and try to strike each other with wooden lances. A total of 2n knights entered a tournament, n from each of two rival houses. On arrival, every knight challenged exactly one knight of the other house to a duel.
A subset S of the knights is called a kernel when both of these hold.
Given all of the challenges, find a kernel. A kernel always exists.
The first line contains the number of knights in each house, n (1≤n≤100000). The knights of the first house are numbered 1 through n, and the knights of the second house are numbered n+1 through 2n.
The second line contains integers f1,f2,…,fn, where fk is the index of the knight challenged by knight k (n+1≤fk≤2n).
The third line contains integers s1,s2,…,sn, where sk is the index of the knight challenged by knight n+k (1≤sk≤n).
Print the indices of the knights in the kernel in increasing order on a single line, separated by single spaces.
If several kernels exist, print the lexicographically smallest one. That is, write each kernel as its increasing sequence of indices and print the sequence that comes first lexicographically.