Bridges in Karak

3.5

22 votes
Medium-Hard
Problem

View Russian Translation

In the big town of Karak, which is quite similar to Venice, there are 2N districts. Unfortunately, there was a big earthquake recently, which destroyed a lot of bridges in the town, and separated all the districts into two areas A and B without any connection between them. In each of these areas there are exactly N districts numbered from 1 to N.

Now the King of Karak is planning to build exactly N bridges between the two areas. He is planning to do it in the following process:

Initially, each district d in any area has to prepare a list of all districts in the other area, in such a way that district di is on this list before district dj if and only if people in district d prefer a new bridge from their district to di instead of to dj.

Because people in Karak are very competitive, if for any 4 districts di,djA and dl,dkB, King decides to connect district di with dl and district dj with district dk by bridges, and if people in di prefer a new bridge to connect their district to dk than to dl and people in dk prefer a new bridge to connect their district to di than to dj, then an unaccepted revolt will begin.

If it is impossible to build new bridges and avoid the revolt, then the problem in Karak cannot be solved.

Otherwise, from all possible bridges-building plans, King will either accept the one in which the number of fully pleased districts in area A is maximized or accept the one in which the number of fully pleased districts in area B is maximized.

A district d is fully pleased if and only if a new bridge is built from d to district di and there is no other valid plan to build bridges and avoid the revolt in which a bridge is built from d to district dj and d prefers a bridge to dj than a bridge to di.

Your task is to first decide if the problem of bridges in Karak can be solved, and if it can, to output 2 plans which King will accept.

Input format:

In the first line there is a single integer N denoting the number of cities in each area. Then N lines follow. In the ith of them there are N integers di1,di2,,din denoting the list of districts in the B area in order in which people from district i in area A prefer new bridges to be built to. Next, another N lines follow. In the ith of them there are N integers di1,di2,,din denoting the list of districts in the A area in order in which people from district i in area B prefer new bridges to be built to.

Output format:

If there is no possible plan to build new bridges and avoid the revolt print NO in a single line.

Otherwise, output exactly 3 lines.

In the first line output two space separated integers denoting the maximum number of fully pleased districts from area A in an optimum plan and the maximum number of fully pleased districts from area B in the second optimum plan (possible the same one).

In the second line output N space separated integers ri1,ri2,,rin, where rij is the district in area B for which the bridge will be build from district j in area A in the way which maximize the number of fully pleased districts in area A.

In the third line, output N space separated integers ri1,ri2,,rin, where rij is the district in area B for which the bridge will be build from district j in area A in the way which maximize the number of fully pleased districts in area B.

If there are many possible such solutions, output any of them.

Constraints:

1N2000
1dijN

Sample Input
2
1 2
1 2
1 2
2 1
Sample Output
2 2
1 2 
1 2 
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

There are 4 districts in the town. 2 of them are in area A and the other two in area B. Both districts from area A prefer a bridge to district 1 in area B. People from district 1 in area B prefer a bridge to district 1 in area A, and people from district 2 in area B prefer a bridge to district 2 in area A.

If King decides to build a bridge from district 1 in area A to district 1 in area B, and from district 2 in area A to district 2 in area B, then this plan guarantees that there is no revolt, and all 4 regions are fully pleased, so this plan is the best one to fully please people from area A as well as people from area B.

Editor Image

?