Splay:
1 #include<cstdio> 2 #include<cstring> 3 #define MAXN 100010 4 int first[MAXN], next[MAXN], v[MAXN], e; 5 int seq[MAXN], cnt; 6 int n; 7 bool vis[MAXN]; 8 struct SplayTree { 9 int root; 10 int next[MAXN][2], pre[MAXN]; 11 void Init() { 12 pre[0] = next[0][0] = next[0][1] = 0; 13 } 14 inline void NewNode(int &x, int size, int father) { 15 x = size; 16 next[x][0] = next[x][1] = 0; 17 pre[x] = father; 18 } 19 void Build(int &x, int L, int R, int father) { 20 if (L <= R) { 21 int mid = (L + R) >> 1; 22 NewNode(x, seq[mid], father); 23 Build(next[x][0], L, mid - 1, x); 24 Build(next[x][1], mid + 1, R, x); 25 } 26 } 27 inline void Rotate(int x, int kind) { 28 int y, z; 29 y = pre[x]; 30 z = pre[y]; 31 next[y][!kind] = next[x][kind]; 32 pre[next[x][kind]] = y; 33 next[z][next[z][1] == y] = x; 34 pre[x] = z; 35 next[x][kind] = y; 36 pre[y] = x; 37 } 38 void Splay(int x, int goal) { 39 if (x != goal) { 40 while (pre[x] != goal) { 41 if (next[pre[x]][0] == x) 42 Rotate(x, 1); 43 else 44 Rotate(x, 0); 45 } 46 } 47 } 48 void Move(int x, int y) { 49 if (x == y) 50 return; 51 int tmp; 52 Splay(x, 0); 53 Splay(x + n, x); 54 for (tmp = y; tmp; tmp = pre[tmp]) { 55 if (next[x + n][0] == tmp) 56 return; 57 } 58 int a, b; 59 a = next[x][0]; 60 b = next[x + n][1]; 61 next[x][0] = next[x + n][1] = pre[a] = pre[b] = 0; 62 if (a && b) { 63 while (next[b][0]) 64 b = next[b][0]; 65 next[b][0] = a; 66 pre[a] = b; 67 } 68 if (y == 0) 69 return; 70 Splay(y, 0); 71 for (tmp = next[y][1]; next[tmp][0]; tmp = next[tmp][0]) 72 ; 73 Splay(tmp, y); 74 next[tmp][0] = x; 75 pre[x] = tmp; 76 } 77 int Query(int x) { 78 Splay(x, 0); 79 for (; next[x][0]; x = next[x][0]) 80 ; 81 return x; 82 } 83 } spt; 84 inline void AddEdge(int x, int y) { 85 v[e] = y; 86 next[e] = first[x]; 87 first[x] = e++; 88 } 89 void DFS(int x) { 90 int i; 91 seq[cnt++] = x; 92 for (i = first[x]; i != -1; i = next[i]) 93 DFS(v[i]); 94 seq[cnt++] = x + n; 95 } 96 int main() { 97 char cmd[10]; 98 int x, y, i, q; 99 bool flag = true; 100 while (~scanf("%d", &n)) { 101 spt.Init(); 102 if (flag) 103 flag = false; 104 else 105 putchar('\n'); 106 memset(first, -1, sizeof(first)); 107 memset(vis, false, sizeof(vis)); 108 for (e = 0, i = 1; i <= n; i++) { 109 scanf("%d", &x); 110 if (x) 111 AddEdge(x, i); 112 else 113 vis[i] = true; 114 } 115 for (i = 1; i <= n; i++) { 116 if (vis[i]) { 117 cnt = 0; 118 DFS(i); 119 spt.Build(spt.root, 0, cnt - 1, 0); 120 } 121 } 122 scanf("%d", &q); 123 while (q--) { 124 scanf(" %s%d", cmd, &x); 125 if (strcmp(cmd, "MOVE") == 0) { 126 scanf("%d", &y); 127 spt.Move(x, y); 128 } else 129 printf("%d\n", spt.Query(x)); 130 } 131 } 132 return 0; 133 }
Link Cut Tree
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #define MAXN 50010 5 using namespace std; 6 int n; 7 struct LCT { 8 int bef[MAXN], pre[MAXN], next[MAXN][2]; 9 void Init() { 10 memset(pre, 0, sizeof(pre)); 11 memset(next, 0, sizeof(next)); 12 } 13 inline void Rotate(int x, int kind) { 14 int y, z; 15 y = pre[x]; 16 z = pre[y]; 17 next[y][!kind] = next[x][kind]; 18 pre[next[x][kind]] = y; 19 next[z][next[z][1] == y] = x; 20 pre[x] = z; 21 next[x][kind] = y; 22 pre[y] = x; 23 } 24 void Splay(int x) { 25 int rt; 26 for (rt = x; pre[rt]; rt = pre[rt]) 27 ; 28 if (x != rt) { 29 bef[x] = bef[rt]; 30 bef[rt] = 0; 31 while (pre[x]) { 32 if (next[pre[x]][0] == x) 33 Rotate(x, 1); 34 else 35 Rotate(x, 0); 36 } 37 } 38 } 39 void Access(int x) { 40 int father; 41 for (father = 0; x; x = bef[x]) { 42 Splay(x); 43 pre[next[x][1]] = 0; 44 bef[next[x][1]] = x; 45 next[x][1] = father; 46 pre[father] = x; 47 bef[father] = 0; 48 father = x; 49 } 50 } 51 int Query(int x) { 52 Access(x); 53 Splay(x); 54 while (next[x][0]) 55 x = next[x][0]; 56 return x; 57 } 58 void Cut(int x) { 59 Access(x); 60 Splay(x); 61 bef[next[x][0]] = bef[x]; 62 bef[x] = 0; 63 pre[next[x][0]] = 0; 64 next[x][0] = 0; 65 } 66 void Join(int x, int y) { 67 if (y == 0) 68 Cut(x); 69 else { 70 int tmp; 71 Access(y); 72 Splay(y); 73 for (tmp = x; pre[tmp]; tmp = pre[tmp]) 74 ; 75 if (tmp != y) { 76 Cut(x); 77 bef[x] = y; 78 } 79 } 80 } 81 } lct; 82 int INT() { 83 char ch; 84 int res; 85 while (ch = getchar(), !isdigit(ch)) 86 ; 87 for (res = ch - '0'; ch = getchar(), isdigit(ch);) 88 res = res * 10 + ch - '0'; 89 return res; 90 } 91 char CHAR() { 92 char ch, res; 93 while (res = getchar(), !isalpha(res)) 94 ; 95 while (ch = getchar(), isalpha(ch)) 96 ; 97 return res; 98 } 99 int main() { 100 bool flag = true; 101 char ch; 102 int i, x, y, q; 103 while (~scanf("%d", &n)) { 104 if (flag) 105 flag = false; 106 else 107 putchar('\n'); 108 lct.Init(); 109 for (i = 1; i <= n; i++) 110 lct.bef[i] = INT(); 111 q = INT(); 112 while (q--) { 113 ch = CHAR(); 114 if (ch == 'Q') { 115 x = INT(); 116 printf("%d\n", lct.Query(x)); 117 } else { 118 x = INT(), y = INT(); 119 lct.Join(x, y); 120 } 121 } 122 } 123 return 0; 124 }