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
| #include <bits/stdc++.h> using namespace std;
int n; vector <int> e[1001]; bool have[1001];
struct node { int leftMark, rightMark; node* sonLeft, *sonRight; void updateMark() { leftMark = sonLeft->leftMark; rightMark = sonRight->rightMark; } }*start, *want;
struct step { int fromA, fromB; int toA, toB; };
node* addNode(int L, int R, int from) { node *t = new node(); t->leftMark = L; t->rightMark = R; if(L == R+1) return t; for(int i = 0; i < e[L].size(); i++) if(e[L][i] != from) have[e[L][i]] = true; int M = -1; for(int j = 0; j < e[R].size(); j++) if(e[R][j] != from) if(have[e[R][j]]) M = e[R][j]; for(int i = 0; i < e[L].size(); i++) have[e[L][i]] = false; t->sonLeft = addNode(L, M, R); t->sonRight = addNode(M, R, L); return t; }
node* input() { for(int i = 1; i <= n; i++) e[i].clear(); for(int i = 1; i <= n-3; i++) { int a, b; cin >> a >> b; e[a].push_back(b); e[b].push_back(a); } for(int i = 1; i <= n; i++) { int j = i+1; if(i == n) j = 1; e[i].push_back(j); e[j].push_back(i); } return addNode(n, 1, -1); }
void print(node *p, string append) { cout << append << p->leftMark << " " << p->rightMark << endl; if(p->sonLeft != NULL) print(p->sonLeft, append + "\t"); if(p->sonRight != NULL) print(p->sonRight, append + "\t"); }
vector <step> record;
void rotateR(node *r) { step t; node *s = r->sonLeft; node *a = s->sonLeft; node *b = s->sonRight; node *c = r->sonRight; t.fromA = s->leftMark; t.fromB = s->rightMark; r->sonLeft = a; r->sonRight = s; s->sonLeft = b; s->sonRight = c; s->updateMark(); r->updateMark(); t.toA = s->leftMark; t.toB = s->rightMark; record.push_back(t); }
void rotateL(node *r) { step t; node *s = r->sonRight; node *a = r->sonLeft; node *b = s->sonLeft; node *c = s->sonRight; t.fromA = s->leftMark; t.fromB = s->rightMark; r->sonLeft = s; r->sonRight = c; s->sonLeft = a; s->sonRight = b; s->updateMark(); r->updateMark(); t.toA = s->leftMark; t.toB = s->rightMark; record.push_back(t); }
void rotateRT(node *p, int middle) { int myMiddle = p->sonLeft->rightMark; if(myMiddle == middle) return; if(myMiddle < middle) { rotateRT(p->sonLeft, middle); rotateR(p); } else if(myMiddle > middle) { rotateRT(p->sonRight, middle); rotateL(p); } }
void norm(node *p) { if(p->leftMark - p->rightMark <= 2) return; int middle = (p->leftMark + p->rightMark) / 2; rotateRT(p, middle); norm(p->sonLeft); norm(p->sonRight); }
int main() { ios :: sync_with_stdio(false); memset(have, 0, sizeof(have)); cin >> n; start = input(); norm(start); vector <step> record1 = record; record.clear(); want = input(); norm(want); cout << record.size() + record1.size() << endl; for(int i = 0; i < record1.size(); i++) cout << record1[i].fromA << " " << record1[i].fromB << endl; for(int i = record.size()-1; i >= 0; i--) cout << record[i].toA << " " << record[i].toB << endl; return 0; }
|