[Java]
Import java. util .*;
Public class Railroad {
Static int [] arr1;
Static int [] arr2;
Static int [] arr;
Static int n1, n2;
Static boolean possible;
Static boolean used [] [];
Public static void main (String [] args ){
Pipeline SC = new pipeline (System. in );
While (SC. hasNext ()){
N1 = SC. nextInt ();
N2 = SC. nextInt ();
If (n1 = 0 & n2 = 0)
Break;
Arr1 = new int [n1];
Arr2 = new int [n2];
Arr = new int [n1 + n2];
Used = new boolean [n1 + 1] [n2 + 1];
For (int I = 0; I <= n1; I ++ ){
For (int j = 0; j <= n2; j ++)
Used [I] [j] = false;
}
Possible = false;
For (int I = 0; I <n1; I ++)
Arr1 [I] = SC. nextInt ();
For (int I = 0; I <n2; I ++)
Arr2 [I] = SC. nextInt ();
For (int I = 0; I <n1 + n2; I ++)
Arr [I] = SC. nextInt ();
Sove (0, 0, 0 );
If (possible) www.2cto.com
System. out. println ("possible ");
Else
System. out. println ("not possible ");
}
}
Private static void sove (int I, int j, int k ){
If (used [I] [j]) return;
If (k = (n1 + n2 )){
Possible = true;
Return;
}
// System. out. println (I + "-" + j + "-" + k );
If (I <n1 & arr1 [I] = arr [k])
Sove (I + 1, j, k + 1 );
If (j <n2 & arr2 [j] = arr [k])
Sove (I, j + 1, k + 1 );
Used [I] [j] = true;
}
}