atcoder#AGC018F. [AGC018F] Two Trees

[AGC018F] Two Trees

Score : 17001700 points

Problem Statement

There are two rooted trees, each with NN vertices. The vertices of each tree are numbered 11 through NN. In the first tree, the parent of Vertex ii is Vertex AiA_i. Here, Ai=1A_i=-1 if Vertex ii is the root of the first tree. In the second tree, the parent of Vertex ii is Vertex BiB_i. Here, Bi=1B_i=-1 if Vertex ii is the root of the second tree.

Snuke would like to construct an integer sequence of length NN, X1X_1 , X2X_2 , ...... , XNX_N, that satisfies the following condition:

  • For each vertex on each tree, let the indices of its descendants including itself be a1a_1 , a2a_2 , ......, aka_k. Then, abs(Xa1+Xa2+...+Xak)=1abs(X_{a_1} + X_{a_2} + ... + X_{a_k})=1 holds.

Determine whether it is possible to construct such a sequence. If the answer is possible, find one such sequence.

Constraints

  • 1N1051 \leq N \leq 10^5
  • 1AiN1 \leq A_i \leq N, if Vertex ii is not the root in the first tree.
  • Ai=1A_i = -1, if Vertex ii is the root in the first tree.
  • 1BiN1 \leq B_i \leq N, if Vertex ii is not the root in the second tree.
  • Bi=1B_i = -1, if Vertex ii is the root in the second tree.
  • Input corresponds to valid rooted trees.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 .... ANA_N

B1B_1 B2B_2 .... BNB_N

Output

If it is not possible to construct an integer sequence that satisfies the condition, print IMPOSSIBLE. If it is possible, print POSSIBLE in the first line. Then, in the second line, print X1X_1 , X2X_2 , ...... , XNX_N, an integer sequence that satisfies the condition.

5
3 3 4 -1 4
4 4 1 -1 1
POSSIBLE
1 -1 -1 3 -1

For example, the indices of the descendants of Vertex 33 of the first tree including itself, is 3,1,23,1,2. It can be seen that the sample output holds abs(X3+X1+X2)=abs((1)+(1)+(1))=abs(1)=1abs(X_3+X_1+X_2)=abs((-1)+(1)+(-1))=abs(-1)=1. Similarly, the condition is also satisfied for other vertices.

6
-1 5 1 5 1 3
6 5 5 3 -1 3
IMPOSSIBLE

In this case, constructing a sequence that satisfies the condition is IMPOSSIBLE.

8
2 7 1 2 2 1 -1 4
4 -1 4 7 4 4 2 4
POSSIBLE
1 2 -1 0 -1 1 0 -1