SHARE
TWEET

Untitled

a guest May 22nd, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e3;
  5. int n , ch[N] , vis[N] , O[N] , pai[N];
  6.  
  7. int main(){
  8.  
  9.   cin >> n;
  10.  
  11.   for(int i = 1 ; i <= n ; i++){ cin >> ch[i] , pai[ch[i]] = i; }
  12.   for(int i = 1 ; i <= n ; i++) cin >> O[i];
  13.  
  14.   for(int i = 1 ; i <= n ; i++){
  15.     if(O[i] != 0){
  16.       int j = i;
  17.       vis[O[i]] = 1;
  18.       while(j > 1 && O[j-1] == 0 && vis[pai[O[j]]] == 0){
  19.         vis[pai[O[j]]] = 1;
  20.         O[j-1] = pai[O[j--]];
  21.       }
  22.       j = i;
  23.       while(j < n && O[j+1] == 0 && vis[ch[O[j]]] == 0){
  24.         vis[ch[O[j]]] = 1;
  25.         O[j+1] = ch[O[j++]];
  26.       }
  27.     }
  28.   }
  29.  
  30.   for(int i = 1 ; i <= n ; i++){
  31.     if(O[i] == 0){
  32.       cout << "-1\n";
  33.       return 0;
  34.     }
  35.   }
  36.    
  37.   for(int i = 1 ; i <= n ; i++) cout << O[i] << " ";
  38.   cout << "\n";
  39.  
  40.   return 0;
  41.  
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top