Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1e3;
- int n , ch[N] , vis[N] , O[N] , pai[N];
- int main(){
- cin >> n;
- for(int i = 1 ; i <= n ; i++){ cin >> ch[i] , pai[ch[i]] = i; }
- for(int i = 1 ; i <= n ; i++) cin >> O[i];
- for(int i = 1 ; i <= n ; i++){
- if(O[i] != 0){
- int j = i;
- vis[O[i]] = 1;
- while(j > 1 && O[j-1] == 0 && vis[pai[O[j]]] == 0){
- vis[pai[O[j]]] = 1;
- O[j-1] = pai[O[j--]];
- }
- j = i;
- while(j < n && O[j+1] == 0 && vis[ch[O[j]]] == 0){
- vis[ch[O[j]]] = 1;
- O[j+1] = ch[O[j++]];
- }
- }
- }
- for(int i = 1 ; i <= n ; i++){
- if(O[i] == 0){
- cout << "-1\n";
- return 0;
- }
- }
- for(int i = 1 ; i <= n ; i++) cout << O[i] << " ";
- cout << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement