1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include<bits/stdc++.h> 
using namespace std;

//g++ -O3 -static 5B_bst/5B_bst.cpp -std=c++11 -o 5B_bst/5b_bst
//./5B_bst/5b_bst
/*
4
3 1 -1 3
2 -1 4 2
*/

class Node{
    public:
        int id;
        Node *father, *left, *right;
};

const int sz = 5e5 + 5;
Node nodes[sz];
int target[sz];
int n;
int visited[2010][2010];
int curr[2010];
int past[2010];
int idx = 0;
int bfs[2010][2010];
int bp = 0, ba = 0;

bool same(int ar1[], int ar2[]){
    for(int i = 1; i <= n; i++){
        if(ar1[i] != ar2[i]){
            return false;
        }
    }
    return true;
}

void copy(){
    for(int i = 1; i <= n; i++){
        past[i] = curr[i];
    }
}

void paste(){
    for(int i = 1; i <= n; i++){
        curr[i] = past[i];
    }
}

void convert(){
    for(int i = 1; i <= n; i++){
        if(nodes[i].father == NULL){
            curr[i] = -1;
        }else{
            curr[i] = nodes[i].father->id;
        }
    }
}

void deconvert(){
    for(int i = 1; i <= n; i++){
        nodes[i].father = NULL;
        nodes[i].left = NULL;
        nodes[i].right = NULL;
    }
    for(int i = 1; i <= n; i++){
        if(curr[i] != -1){
            nodes[i].father = &nodes[curr[i]];
            if(i < curr[i]){
                nodes[curr[i]].left = &nodes[i];
            }else{
                nodes[curr[i]].right = &nodes[i];
            }
        }
    }
}

void add(){
    for(int i = 1; i <= n; i++){
        visited[idx][i] = curr[i];
    }
    idx ++;
}

bool is_visited(){
    for(int i = 0; i < idx; i++){
        if(same(curr, visited[i])){
            return true;
        }
    }
    return false;
}

void bfs_add(int score){
    bfs[ba][2000] = score;
    for(int i = 1; i <= n; i++){
        bfs[ba][i] = curr[i];
    }
    ba ++;
}

int bfs_pop(){
    for(int i = 1; i <= n; i++){
        curr[i] = bfs[bp][i];
    }
    bp ++;
    return bfs[bp - 1][2000];
}

void rotate(Node* w, Node* v){
    if(v->father != NULL){
        if(v->father->right == v){
            v->father->right = w;
        }else{
            v->father->left = v;
        }
    }
    w->father = v->father;
    v->father = w;
}

void left(Node* w){  
    Node* v = w->father;
    rotate(w, v);
    w->left = v;
    v->right = NULL;
}

void right(Node* w){
    Node* v = w->father;
    rotate(w, v);
    w->right = v;
    v->left = NULL;
}

// inline void print(){
//     cout<<endl<<"tree "<<bp<<endl;
//     for(int i = 1; i <= n; i++){
//         cout << bfs[bp][i] << " ";
//     }
//     cout << "score "<<bfs[bp][2000];
// }
// inline void print_curr(){
//     cout<<endl<<"tree "<<endl;
//     for(int i = 1; i <= n; i++){
//         cout << curr[i] << " ";
//     }
// }
// inline void print_node(int i){
//     cout<<endl<<"node "<<i<<" "<<&nodes[i]<<endl;
//     cout<<(nodes[i].left == NULL)<<" "<<(nodes[i].right == NULL)<<endl;
//     if(nodes[i].father){
//         cout<<(nodes[i].father->left)<<" "<<(nodes[i].father->right)<<endl;
//     }
// }

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    for(int i = 1; i <= n; i++){
        Node n;
        n.id = i;
        nodes[i] = n;
    }
    for(int i = 1; i <= n; i++){
        cin >> curr[i];
    }
    for(int i = 1; i <= n; i++){
        cin >> target[i];
    }
    bfs_add(0);

    while(bp < ba){
        //print();
        int s = bfs_pop();
        copy();
        deconvert();
        for(int i = 1; i <= n; i++){
            //print_node(i);
            if(nodes[i].left == NULL && nodes[i].father != NULL && nodes[i].father->right == &nodes[i]){
                left(&nodes[i]);
                convert();
                //cout<<"left"<<endl;
               //print_curr();
                if(same(target, curr)){
                    cout << s+1;
                    return 0;
                }
                if(!is_visited()){
                    add();
                    bfs_add(s + 1);
                }
                paste();
                deconvert();
            }
            if(nodes[i].right == NULL && nodes[i].father != NULL && nodes[i].father->left == &nodes[i]){
                right(&nodes[i]);
                convert();
                //cout<<"right"<<endl;
                //print_curr();
                if(same(target, curr)){
                    cout << s+1;
                    return 0;
                }
                if(!is_visited()){
                    add();
                    bfs_add(s + 1);
                }
                paste();
                deconvert();
            }
        }
    }


    return 0;
}