#include <bits/stdc++.h> using namespace std; typedef pair<int,int> II; #define INFTY 2000000000 #define MOD 1000000007 #define DEBUG(s,x) cerr << s << ": " << x << "\n"; #define VI vector<int> #define LEFT 0 #define RIGHT 1 struct edge { VI vert; int begin, end; int child; }; vector<VI> a_child, b_child; VI a_parent, b_parent; VI a_child_side, b_child_side; int a_root, b_root; int n; vector<edge*> edge_to_parent; vector<edge*> long_edge; VI offset; void print_vec(VI & v) { for (auto &e : v) cerr << e << " "; cerr << "\n"; } void read_tree(vector<VI> &child, VI &parent, VI &child_side, int &root) { for (int i=0; i<n; i++) child.push_back(VI(2,-1)); parent.resize(n); child_side.resize(n); for (int i=0; i<n; i++) { int p; cin >> p; if (p != -1) p--; else root = i; parent[i] = p; if (p != -1) if (i < p) { child[p][LEFT] = i; child_side[i] = LEFT; } else { child[p][RIGHT] = i; child_side[i] = RIGHT; } } } void compress_a (int v) { int p = a_parent[v]; for (int side : {LEFT,RIGHT}) if (a_child[v][side] != -1) { compress_a(a_child[v][side]); if (p != -1 && a_child[v][1-side] == -1 && a_child_side[v] == side) { int ch = a_child[v][side]; edge_to_parent[ch]->vert.push_back(v); offset[v] = edge_to_parent[ch]->vert.size()-1; long_edge[v] = edge_to_parent[ch]; a_child [p][side] = ch; a_parent[ch] = p; } } } /* void shallow_compress (int v) { if a_child[v][LEFT] == -1 side = LEFT; else if a_child[v][RIGHT] == -1 side = RIGHT; else return; VI to_add; int x = v; while (a_child[x][side] == -1 && a_child[x][1-side] != -1) { to_add.push_back(x); x = a_child[x][1-side]; } int x = a_child[x][1-side]; sort(to_add.begin(), to_add.end()); for (auto & e : to_add) { edge_to_parent[x]->vert } } */ // dekompresuje ścieżkę (długą krawędź) od v do ojca. // jeśli w==-1 to oddziela najwyższy wierzchołek na ścieżce // wpp oddziela w od ojca w. void decompress_a (int v, int w = -1) { if (v == -1) return; // no node //cerr << "decompress(" << v << (w == -1 ? "" : ", " + to_string(w)) << ")\n"; int p = a_parent[v]; if (p == -1) return; // korzeń: nie ma krawędzi wzwyż /* cerr << "parent(v) = " << p << "\n"; cerr << "edge_to_parent[v]->begin = " << edge_to_parent[v]->begin << "\n"; cerr << "edge_to_parent[v]->end = " << edge_to_parent[v]->end << "\n"; */ if (edge_to_parent[v]->begin == edge_to_parent[v]->end) return; // edge v->parent is not compressed int side = a_child_side[v]; if (w == -1) if (side == LEFT) w = edge_to_parent[v]->vert[edge_to_parent[v]->end-1]; else w = edge_to_parent[v]->vert[edge_to_parent[v]->begin]; // cerr << "w = " << w << "\n"; if (side == LEFT) // większe od w idą do edge_to_parent[w] if (offset[w] - edge_to_parent[v]->begin >= edge_to_parent[v]->end - offset[w] - 1) // przenosimy końcówkę do w bo krótsza { // cerr << "Case A\n"; for (int i=offset[w]+1; i<edge_to_parent[v]->end; ++i) { edge_to_parent[w]->vert.push_back(edge_to_parent[v]->vert[i]); long_edge[edge_to_parent[v]->vert[i]] = edge_to_parent[w]; offset[edge_to_parent[v]->vert[i]] = edge_to_parent[w]->vert.size()-1; } edge_to_parent[w]->begin=0; edge_to_parent[w]->end=edge_to_parent[w]->vert.size(); edge_to_parent[v]->end = offset[w]; } else { // przenosimy prefiks do v, końcówka wrzucona do w. // cerr << "Case B\n"; swap(edge_to_parent[w], edge_to_parent[v]); for (int i=edge_to_parent[w]->begin; i<offset[w]; ++i) { edge_to_parent[v]->vert.push_back(edge_to_parent[w]->vert[i]); long_edge[edge_to_parent[w]->vert[i]] = edge_to_parent[v]; offset[edge_to_parent[w]->vert[i]] = edge_to_parent[v]->vert.size()-1; } edge_to_parent[v]->begin=0; edge_to_parent[v]->end=edge_to_parent[v]->vert.size(); edge_to_parent[w]->begin = offset[w]+1; } else // side == RIGHT; mniejsze od w idą do edge_to_parent[w] if (offset[w] - edge_to_parent[v]->end - 1 >= offset[w] - edge_to_parent[v]->begin) // przenosimy prefix do w bo krótszy { // cerr << "Case C\n"; for (int i=edge_to_parent[v]->begin; i<offset[w]; ++i) { edge_to_parent[w]->vert.push_back(edge_to_parent[v]->vert[i]); long_edge[edge_to_parent[v]->vert[i]] = edge_to_parent[w]; offset[edge_to_parent[v]->vert[i]] = edge_to_parent[w]->vert.size()-1; } edge_to_parent[w]->begin=0; edge_to_parent[w]->end=edge_to_parent[w]->vert.size(); edge_to_parent[v]->begin = offset[w]+1; } else { // przenosimy końcówkę do v, prefix wrzucony do w. // cerr << "Case D\n"; swap(edge_to_parent[w], edge_to_parent[v]); for (int i=offset[w]+1; i<edge_to_parent[w]->end; ++i) { edge_to_parent[v]->vert.push_back(edge_to_parent[w]->vert[i]); long_edge[edge_to_parent[w]->vert[i]] = edge_to_parent[v]; offset[edge_to_parent[w]->vert[i]] = edge_to_parent[v]->vert.size()-1; } // cerr << "after loop\n"; edge_to_parent[v]->begin=0; edge_to_parent[v]->end=edge_to_parent[v]->vert.size(); edge_to_parent[w]->end = offset[w]; } edge_to_parent[v]->child = v; edge_to_parent[w]->child = w; long_edge[v]=edge_to_parent[v]; long_edge[w]=edge_to_parent[w]; a_child[p][side] = w; a_parent[w]=p; a_child[w][side] = v; a_child[w][1-side] = -1; a_parent[v]=w; a_child_side[w] = side; /* DEBUG("edge_to_parent[v]->begin",edge_to_parent[v]->begin) DEBUG("edge_to_parent[v]->end",edge_to_parent[v]->end) DEBUG("edge_to_parent[w]->begin",edge_to_parent[w]->begin) DEBUG("edge_to_parent[w]->end",edge_to_parent[w]->end) */ } void rotate (int v) { //DEBUG("rotate (",v) if (a_child[v][1-a_child_side[v]] != -1) { cout << -1 << "\n"; exit(0); } int w = a_parent[v]; assert(w != -1); int p = a_parent[w]; if (p != -1) a_child[p][a_child_side[w]] = v; a_parent[v] = p; a_parent[w] = v; a_child[v][1-a_child_side[v]] = w; a_child[w][a_child_side[v]] = -1; int side = a_child_side[v]; a_child_side[v] = a_child_side[w]; a_child_side[w] = 1-side; swap(edge_to_parent[v], edge_to_parent[w]); edge_to_parent[v]->child = v; edge_to_parent[w]->child = w; } int transform (int v_a, int v_b) { if (v_a == -1) return 0; // cerr << "transform(" << v_a << ", " << v_b << ")\n"; int p = a_parent[v_a]; if (long_edge[v_b]->child != v_b) decompress_a (long_edge[v_b]->child, v_b); // else // cerr << "no decompress\n"; int num_rot = 0; while (a_parent[v_b] != p) { // cerr << "a_parent[v_b] == " << a_parent[v_b] << " != " << p << "\n"; num_rot += edge_to_parent[v_b]->vert.size()+1; rotate (v_b); } // cerr << "left child after rotations: " << a_child[v_b][LEFT] << "\n"; // cerr << "right child after rotations: " << a_child[v_b][RIGHT] << "\n"; decompress_a (a_child[v_b][LEFT]); decompress_a (a_child[v_b][RIGHT]); // cerr << "left child after rotations and decompress: " << a_child[v_b][LEFT] << "\n"; // cerr << "right child after rotations and decompress: " << a_child[v_b][RIGHT] << "\n"; return (num_rot + (transform (a_child[v_b][LEFT], b_child[v_b][LEFT]) + transform (a_child[v_b][RIGHT], b_child[v_b][RIGHT])) % MOD) % MOD; } int main() { std::ios::sync_with_stdio(0); int i, j, k; cin >> n; read_tree(a_child, a_parent, a_child_side, a_root); read_tree(b_child, b_parent, b_child_side, b_root); for (i=0; i<n; i++) { edge * e = new edge(); e->child = i; edge_to_parent.push_back(e); long_edge.push_back(e); offset.push_back(-1); } compress_a (a_root); for (auto e : edge_to_parent) { e->begin = 0; e->end = e->vert.size(); sort(e->vert.begin(), e->vert.end()); } /* cerr << a_child[a_root][LEFT] << "\n"; int cc = a_child[a_root][LEFT]; print_vec( edge_to_parent[cc]->vert); cerr << edge_to_parent[cc]->begin << " " << edge_to_parent[cc]->end << "\n"; */ cout << transform (a_root, b_root) << "\n"; return 0; }
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | #include <bits/stdc++.h> using namespace std; typedef pair<int,int> II; #define INFTY 2000000000 #define MOD 1000000007 #define DEBUG(s,x) cerr << s << ": " << x << "\n"; #define VI vector<int> #define LEFT 0 #define RIGHT 1 struct edge { VI vert; int begin, end; int child; }; vector<VI> a_child, b_child; VI a_parent, b_parent; VI a_child_side, b_child_side; int a_root, b_root; int n; vector<edge*> edge_to_parent; vector<edge*> long_edge; VI offset; void print_vec(VI & v) { for (auto &e : v) cerr << e << " "; cerr << "\n"; } void read_tree(vector<VI> &child, VI &parent, VI &child_side, int &root) { for (int i=0; i<n; i++) child.push_back(VI(2,-1)); parent.resize(n); child_side.resize(n); for (int i=0; i<n; i++) { int p; cin >> p; if (p != -1) p--; else root = i; parent[i] = p; if (p != -1) if (i < p) { child[p][LEFT] = i; child_side[i] = LEFT; } else { child[p][RIGHT] = i; child_side[i] = RIGHT; } } } void compress_a (int v) { int p = a_parent[v]; for (int side : {LEFT,RIGHT}) if (a_child[v][side] != -1) { compress_a(a_child[v][side]); if (p != -1 && a_child[v][1-side] == -1 && a_child_side[v] == side) { int ch = a_child[v][side]; edge_to_parent[ch]->vert.push_back(v); offset[v] = edge_to_parent[ch]->vert.size()-1; long_edge[v] = edge_to_parent[ch]; a_child [p][side] = ch; a_parent[ch] = p; } } } /* void shallow_compress (int v) { if a_child[v][LEFT] == -1 side = LEFT; else if a_child[v][RIGHT] == -1 side = RIGHT; else return; VI to_add; int x = v; while (a_child[x][side] == -1 && a_child[x][1-side] != -1) { to_add.push_back(x); x = a_child[x][1-side]; } int x = a_child[x][1-side]; sort(to_add.begin(), to_add.end()); for (auto & e : to_add) { edge_to_parent[x]->vert } } */ // dekompresuje ścieżkę (długą krawędź) od v do ojca. // jeśli w==-1 to oddziela najwyższy wierzchołek na ścieżce // wpp oddziela w od ojca w. void decompress_a (int v, int w = -1) { if (v == -1) return; // no node //cerr << "decompress(" << v << (w == -1 ? "" : ", " + to_string(w)) << ")\n"; int p = a_parent[v]; if (p == -1) return; // korzeń: nie ma krawędzi wzwyż /* cerr << "parent(v) = " << p << "\n"; cerr << "edge_to_parent[v]->begin = " << edge_to_parent[v]->begin << "\n"; cerr << "edge_to_parent[v]->end = " << edge_to_parent[v]->end << "\n"; */ if (edge_to_parent[v]->begin == edge_to_parent[v]->end) return; // edge v->parent is not compressed int side = a_child_side[v]; if (w == -1) if (side == LEFT) w = edge_to_parent[v]->vert[edge_to_parent[v]->end-1]; else w = edge_to_parent[v]->vert[edge_to_parent[v]->begin]; // cerr << "w = " << w << "\n"; if (side == LEFT) // większe od w idą do edge_to_parent[w] if (offset[w] - edge_to_parent[v]->begin >= edge_to_parent[v]->end - offset[w] - 1) // przenosimy końcówkę do w bo krótsza { // cerr << "Case A\n"; for (int i=offset[w]+1; i<edge_to_parent[v]->end; ++i) { edge_to_parent[w]->vert.push_back(edge_to_parent[v]->vert[i]); long_edge[edge_to_parent[v]->vert[i]] = edge_to_parent[w]; offset[edge_to_parent[v]->vert[i]] = edge_to_parent[w]->vert.size()-1; } edge_to_parent[w]->begin=0; edge_to_parent[w]->end=edge_to_parent[w]->vert.size(); edge_to_parent[v]->end = offset[w]; } else { // przenosimy prefiks do v, końcówka wrzucona do w. // cerr << "Case B\n"; swap(edge_to_parent[w], edge_to_parent[v]); for (int i=edge_to_parent[w]->begin; i<offset[w]; ++i) { edge_to_parent[v]->vert.push_back(edge_to_parent[w]->vert[i]); long_edge[edge_to_parent[w]->vert[i]] = edge_to_parent[v]; offset[edge_to_parent[w]->vert[i]] = edge_to_parent[v]->vert.size()-1; } edge_to_parent[v]->begin=0; edge_to_parent[v]->end=edge_to_parent[v]->vert.size(); edge_to_parent[w]->begin = offset[w]+1; } else // side == RIGHT; mniejsze od w idą do edge_to_parent[w] if (offset[w] - edge_to_parent[v]->end - 1 >= offset[w] - edge_to_parent[v]->begin) // przenosimy prefix do w bo krótszy { // cerr << "Case C\n"; for (int i=edge_to_parent[v]->begin; i<offset[w]; ++i) { edge_to_parent[w]->vert.push_back(edge_to_parent[v]->vert[i]); long_edge[edge_to_parent[v]->vert[i]] = edge_to_parent[w]; offset[edge_to_parent[v]->vert[i]] = edge_to_parent[w]->vert.size()-1; } edge_to_parent[w]->begin=0; edge_to_parent[w]->end=edge_to_parent[w]->vert.size(); edge_to_parent[v]->begin = offset[w]+1; } else { // przenosimy końcówkę do v, prefix wrzucony do w. // cerr << "Case D\n"; swap(edge_to_parent[w], edge_to_parent[v]); for (int i=offset[w]+1; i<edge_to_parent[w]->end; ++i) { edge_to_parent[v]->vert.push_back(edge_to_parent[w]->vert[i]); long_edge[edge_to_parent[w]->vert[i]] = edge_to_parent[v]; offset[edge_to_parent[w]->vert[i]] = edge_to_parent[v]->vert.size()-1; } // cerr << "after loop\n"; edge_to_parent[v]->begin=0; edge_to_parent[v]->end=edge_to_parent[v]->vert.size(); edge_to_parent[w]->end = offset[w]; } edge_to_parent[v]->child = v; edge_to_parent[w]->child = w; long_edge[v]=edge_to_parent[v]; long_edge[w]=edge_to_parent[w]; a_child[p][side] = w; a_parent[w]=p; a_child[w][side] = v; a_child[w][1-side] = -1; a_parent[v]=w; a_child_side[w] = side; /* DEBUG("edge_to_parent[v]->begin",edge_to_parent[v]->begin) DEBUG("edge_to_parent[v]->end",edge_to_parent[v]->end) DEBUG("edge_to_parent[w]->begin",edge_to_parent[w]->begin) DEBUG("edge_to_parent[w]->end",edge_to_parent[w]->end) */ } void rotate (int v) { //DEBUG("rotate (",v) if (a_child[v][1-a_child_side[v]] != -1) { cout << -1 << "\n"; exit(0); } int w = a_parent[v]; assert(w != -1); int p = a_parent[w]; if (p != -1) a_child[p][a_child_side[w]] = v; a_parent[v] = p; a_parent[w] = v; a_child[v][1-a_child_side[v]] = w; a_child[w][a_child_side[v]] = -1; int side = a_child_side[v]; a_child_side[v] = a_child_side[w]; a_child_side[w] = 1-side; swap(edge_to_parent[v], edge_to_parent[w]); edge_to_parent[v]->child = v; edge_to_parent[w]->child = w; } int transform (int v_a, int v_b) { if (v_a == -1) return 0; // cerr << "transform(" << v_a << ", " << v_b << ")\n"; int p = a_parent[v_a]; if (long_edge[v_b]->child != v_b) decompress_a (long_edge[v_b]->child, v_b); // else // cerr << "no decompress\n"; int num_rot = 0; while (a_parent[v_b] != p) { // cerr << "a_parent[v_b] == " << a_parent[v_b] << " != " << p << "\n"; num_rot += edge_to_parent[v_b]->vert.size()+1; rotate (v_b); } // cerr << "left child after rotations: " << a_child[v_b][LEFT] << "\n"; // cerr << "right child after rotations: " << a_child[v_b][RIGHT] << "\n"; decompress_a (a_child[v_b][LEFT]); decompress_a (a_child[v_b][RIGHT]); // cerr << "left child after rotations and decompress: " << a_child[v_b][LEFT] << "\n"; // cerr << "right child after rotations and decompress: " << a_child[v_b][RIGHT] << "\n"; return (num_rot + (transform (a_child[v_b][LEFT], b_child[v_b][LEFT]) + transform (a_child[v_b][RIGHT], b_child[v_b][RIGHT])) % MOD) % MOD; } int main() { std::ios::sync_with_stdio(0); int i, j, k; cin >> n; read_tree(a_child, a_parent, a_child_side, a_root); read_tree(b_child, b_parent, b_child_side, b_root); for (i=0; i<n; i++) { edge * e = new edge(); e->child = i; edge_to_parent.push_back(e); long_edge.push_back(e); offset.push_back(-1); } compress_a (a_root); for (auto e : edge_to_parent) { e->begin = 0; e->end = e->vert.size(); sort(e->vert.begin(), e->vert.end()); } /* cerr << a_child[a_root][LEFT] << "\n"; int cc = a_child[a_root][LEFT]; print_vec( edge_to_parent[cc]->vert); cerr << edge_to_parent[cc]->begin << " " << edge_to_parent[cc]->end << "\n"; */ cout << transform (a_root, b_root) << "\n"; return 0; } |