Problem:
Score : 1700 points

Problem Statement

There are two rooted trees, each with N vertices.
The vertices of each tree are numbered 1 through N.
In the first tree, the parent of Vertex i is Vertex A_i.
Here, A_i=-1 if Vertex i is the root of the first tree.
In the second tree, the parent of Vertex i is Vertex B_i.
Here, B_i=-1 if Vertex i is the root of the second tree.

Snuke would like to construct an integer sequence of length N, X_1 , X_2 , ... , X_N, that satisfies the following condition:

For each vertex on each tree, let the indices of its descendants including itself be a_1 , a_2 , ..., a_k. Then, abs(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

1 \leq N \leq 10^5

1 \leq A_i \leq N, if Vertex i is not the root in the first tree.

A_i = -1, if Vertex i is the root in the first tree.

1 \leq B_i \leq N, if Vertex i is not the root in the second tree.

B_i = -1, if Vertex i is the root in the second tree.

Input corresponds to valid rooted trees.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 .. A_N
B_1 B_2 .. B_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 X_1 , X_2 , ... , X_N, an integer sequence that satisfies the condition.

Sample Input 1

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

Sample Output 1

POSSIBLE
1 -1 -1 3 -1

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

Sample Input 2

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

Sample Output 2

IMPOSSIBLE

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

Sample Input 3

8
2 7 1 2 2 1 -1 4
4 -1 4 7 4 4 2 4

Sample Output 3

POSSIBLE
1 2 -1 0 -1 1 0 -1

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::io;
use std::io::{Read, BufReader};
use std::cmp;

fn next_token<R: Read>(reader: &mut R) -> Vec<u8> {
    let mut buf: [u8; 1] = [0];
    let mut read_chars = false;
    let mut ret: Vec<u8> = vec![];

    loop {
        if reader.read(&mut buf).unwrap() == 0 {
            break;
        } else {
            if buf[0] == '\r' as u8 || buf[0] == '\n' as u8 || buf[0] == ' ' as u8 {
                if read_chars {
                    break;
                }
            } else {
                read_chars = true;
                ret.push(buf[0]);
            }
        }
    };
    ret
}
fn next_i64<R: Read>(reader: &mut R) -> i64 {
    let token = next_token(reader);
    let mut ret: i64 = 0;
    let mut sgn = false;

    for c in token {
        if '0' as u8 <= c && c <= '9' as u8 {
            ret = ret * 10 + (c - ('0' as u8)) as i64;
        } else if '-' as u8 == c {
            sgn = true;
        }
    };
    ret * if sgn { -1 } else { 1 }
}
fn next_i32<R: Read>(reader: &mut R) -> i32 {
    next_i64(reader) as i32
}

fn assign_sign(v: usize, rt: usize, tree: &Vec<Vec<usize>>, ans: &mut Vec<i32>) {
    let mut x = 1;
    for &w in &tree[v] {
        if w == rt {
            continue;
        }
        assign_sign(w, v, tree, ans);
        x ^= 1;
    }
    ans[v] = x;
}
fn make_aux_graph(v: usize, rt: usize, tree: &Vec<Vec<usize>>, aux: &mut Vec<Vec<usize>>) -> usize {
    let mut last_ch: Option<usize> = None;
    for &w in &tree[v] {
        if w == rt {
            continue;
        }
        let free = make_aux_graph(w, v, tree, aux);
        match last_ch {
            Some(l) => {
                aux[l].push(free);
                aux[free].push(l);
                last_ch = None;
            },
            None => {
                last_ch = Some(free);
            }
        }
    }
    match last_ch {
        Some(l) => l,
        None => v
    }
}
fn decide_ans(v: usize, sto: i32, aux: &Vec<Vec<usize>>, ans: &mut Vec<i32>) {
    if ans[v] != -2 {
        return;
    }
    ans[v] = sto;
    for &w in &aux[v] {
        decide_ans(w, -sto, aux, ans);
    }
}
fn main() {
    let buf = &mut BufReader::new(io::stdin());

    let n = next_i32(buf) as usize;
    let mut a = vec![vec![]; n];
    let mut b = vec![vec![]; n];
    let mut ra = 0;
    let mut rb = 0;

    for i in 0..n {
        let par = next_i32(buf);
        if par != -1 {
            let par = (par - 1) as usize;
            a[i].push(par);
            a[par].push(i);
        } else {
            ra = i;
        }
    }
    for i in 0..n {
        let par = next_i32(buf);
        if par != -1 {
            let par = (par - 1) as usize;
            b[i].push(par);
            b[par].push(i);
        } else {
            rb = i;
        }
    }

    let mut sgn_a = vec![0; n];
    let mut sgn_b = vec![0; n];

    assign_sign(ra, n, &a, &mut sgn_a);
    assign_sign(rb, n, &b, &mut sgn_b);
    
    if sgn_a != sgn_b {
        println!("IMPOSSIBLE");
        return;
    }

    println!("POSSIBLE");
    let mut aux = vec![vec![]; n];
    make_aux_graph(ra, n, &a, &mut aux);
    make_aux_graph(rb, n, &b, &mut aux);

    let mut ans = vec![-2; n];
    for i in 0..n {
        if sgn_a[i] == 0 {
            ans[i] = 0;
        } else {
            decide_ans(i, 1, &aux, &mut ans);
        }
    }

    for i in 0..n {
        if i == n - 1 {
            println!("{}", ans[i]);
        } else {
            print!("{} ", ans[i]);
        }
    }
}