Skip to content
本博客自 2023 年 4 月 4 日起转为归档状态,可能不再发表新的博文。点此了解博主的竞赛生涯
This blog has been archived by the owner since April 4, 2023. It may no longer have new updates.

Link-Cut Tree 学习笔记

检测到 KaTeX 加载失败,可能会导致文中的数学公式无法正常渲染。

Link-Cut Tree 是一种用来解决动态树问题的数据结构。

它采用类似树链剖分的轻重边路径剖分,把树边分为实边和虚边,并用 Splay 来维护每一条实路径。Link-Cut Tree 的基本操作复杂度为均摊 O(logn)O(\log n),但常数因子较大,一般效率会低于树链剖分。

#主要操作

#Splay 相关

LCT 中的 Splay 与原版 Splay 之间存在一定差别。

#isRoot 函数

判断节点 uu 是否为其所属 Splay 的根。

C++
1
2
3
bool isRoot(const size_t &u) {
return tr[tr[u].f].l != u && tr[tr[u].f].r != u;
}

#旋转(rotate)操作

判断根不能以父亲节点是否存在为依据,而应该使用上文中的 isRoot() 函数。

DIFF
1
2
3
4
5
6
7
8
9
 void rotate(size_t u) {
size_t p = tr[u].f;
unsigned x = relation(u);

- if (tr[p].f) {
+ if (!isRoot(p)) {
tr[tr[p].f].child(relation(p)) = u;
}
tr[u].f = tr[p].f;
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void rotate(size_t u) {
size_t p = tr[u].f;
unsigned x = relation(u);

if (!isRoot(p)) {
tr[tr[p].f].child(relation(p)) = u;
}
tr[u].f = tr[p].f;

if (tr[u].child(x ^ 1)) {
tr[tr[u].child(x ^ 1)].f = p;
}
tr[p].child(x) = tr[u].child(x ^ 1);

tr[u].child(x ^ 1) = p;
tr[p].f = u;

pushup(p);
pushup(u);
}

#Splay 操作

要先自顶到下将路径上的所有节点 pushdown 后再进行 Splay 操作。

DIFF
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
-void splay(size_t u, size_t t = 0) {
+void splay(size_t u) {
+ std::stack<size_t> st;

+ size_t cur = u;
+ st.push(cur);
+ while (!isRoot(cur)) {
+ st.push(tr[cur].f);
+ cur = tr[cur].f;
+ }

+ while (!st.empty()) {
+ pushdown(st.top());
+ st.pop();
+ }

- while (tr[u].f != t) {
- if (tr[tr[u].f].f == t) {
+ while (!isRoot(u)) {
+ if (isRoot(tr[u].f)) {
rotate(u);
} else if (relation(u) == relation(tr[u].f)) {
rotate(tr[u].f);
rotate(u);
} else {
rotate(u);
rotate(u);
}
}
-
- if (!t) root = u;
}
C++
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
void splay(size_t u) {
std::stack<size_t> st;

size_t cur = u;
st.push(cur);
while (!isRoot(cur)) {
st.push(tr[cur].f);
cur = tr[cur].f;
}

while (!st.empty()) {
pushdown(st.top());
st.pop();
}

while (!isRoot(u)) {
if (isRoot(tr[u].f)) {
rotate(u);
} else if (relation(u) == relation(tr[u].f)) {
rotate(tr[u].f);
rotate(u);
} else {
rotate(u);
rotate(u);
}
}
}

#Access 操作

该操作意为「访问」节点 uu,被访问过的节点会有一条实路径连接到根节点,且该节点在这条路径的头部(最下端)。

C++
1
2
3
4
5
6
7
void access(size_t u) {
for (size_t f = 0; u; u = tr[f = u].f) {
splay(u);
tr[u].r = f;
pushup(u);
}
}

#makeRoot 操作

使节点 uu 成为原树的根。

C++
1
2
3
4
5
void makeRoot(const size_t &u) {
access(u);
splay(u);
tr[u].rev = !tr[u].rev;
}

#Split 操作

xyx \sim y 的路径单独抽成一棵 Splay。

C++
1
2
3
4
5
void split(const size_t &x, const size_t &y) {
makeRoot(x);
access(y);
splay(y);
}

xx 节点和 yy 节点之间连一条边,使其成为同一棵树内的两个节点。

C++
1
2
3
4
5
6
7
void link(const int &x, const int &y) {
makeRoot(x);

if (findRoot(y) != x) {
tr[x].f = y;
}
}

#Cut 操作

切断 xxyy 之间的路径。

C++
1
2
3
4
5
6
7
8
void cut(int x, int y) {
split(x, y);

if (tr[y].l == x) { // 只有路径存在时才删除
tr[y].l = 0;
tr[x].f = 0;
}
}

#代码

C++
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
#include <iostream>
#include <stack>

using std::cin;
using std::cout;
const char endl = '\n';

const int N = 1e5 + 5;

class LinkCutTree {
private:
struct node {
size_t l, r, f;
unsigned v, s;
bool rev;

node()
: l(0), r(0), f(0), s(0), v(0), rev(false) {}

node(unsigned _v, size_t _f)
: l(0), r(0), f(_f), s(_v), v(_v), rev(false) {}

size_t &child(unsigned x) {
return !x ? l : r;
}
} tr[N];

inline void pushup(size_t u) {
tr[u].s = tr[tr[u].l].s ^ tr[u].v ^ tr[tr[u].r].s;
}

inline void pushdown(const size_t &u) {
if (!tr[u].rev) return;

std::swap(tr[u].l, tr[u].r);
tr[tr[u].l].rev = !tr[tr[u].l].rev;
tr[tr[u].r].rev = !tr[tr[u].r].rev;
tr[u].rev = false;
}

unsigned relation(const size_t &u) {
return u == tr[tr[u].f].l ? 0 : 1;
}

bool isRoot(const size_t &u) {
return tr[tr[u].f].l != u && tr[tr[u].f].r != u;
}

void rotate(size_t u) {
size_t p = tr[u].f;
unsigned x = relation(u);

if (!isRoot(p)) {
tr[tr[p].f].child(relation(p)) = u;
}
tr[u].f = tr[p].f;

if (tr[u].child(x ^ 1)) {
tr[tr[u].child(x ^ 1)].f = p;
}
tr[p].child(x) = tr[u].child(x ^ 1);

tr[u].child(x ^ 1) = p;
tr[p].f = u;

pushup(p);
pushup(u);
}

void splay(size_t u) {
std::stack<size_t> st;

size_t cur = u;
st.push(cur);
while (!isRoot(cur)) {
st.push(tr[cur].f);
cur = tr[cur].f;
}

while (!st.empty()) {
pushdown(st.top());
st.pop();
}

while (!isRoot(u)) {
if (isRoot(tr[u].f)) {
rotate(u);
} else if (relation(u) == relation(tr[u].f)) {
rotate(tr[u].f);
rotate(u);
} else {
rotate(u);
rotate(u);
}
}
}

void access(size_t u) {
for (size_t f = 0; u; u = tr[f = u].f) {
splay(u);
tr[u].r = f;
pushup(u);
}
}

void makeRoot(const size_t &u) {
access(u);
splay(u);
tr[u].rev = !tr[u].rev;
}

size_t findRoot(size_t u) {
access(u);
splay(u);

while (tr[u].l) {
u = tr[u].l;
}

return u;
}

void split(const size_t &x, const size_t &y) {
makeRoot(x);
access(y);
splay(y);
}

public:
void set(int p, int v) {
tr[p].s = tr[p].v = v;
}

unsigned query(int x, int y) {
split(x, y);

return tr[y].s;
}

void link(const int &x, const int &y) {
makeRoot(x);

if (findRoot(y) != x) {
tr[x].f = y;
}
}

void cut(int x, int y) {
split(x, y);

if (tr[y].l == x) {
tr[y].l = 0;
tr[x].f = 0;
}
}

void change(int p, int v) {
access(p);
splay(p);
tr[p].v = v;
pushup(p);
}
} lct;

int n, m;

int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);

cin >> n >> m;

for (int i = 1, x; i <= n; i++) {
cin >> x;

lct.set(i, x);
}

while (m--) {
int op, x, y;

cin >> op >> x >> y;

switch (op) {
case 0: {
cout << lct.query(x, y) << endl;

break;
}
case 1: {
lct.link(x, y);

break;
}
case 2: {
lct.cut(x, y);

break;
}
case 3: {
lct.change(x, y);

break;
}
}
}

return 0;
}
C++
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
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <stack>

using std::cin;
using std::cout;
const char endl = '\n';

const int N = 1e5 + 5;

// Link-Cut Tree
class LinkCutTree {
private:
std::stack<bool> st;

struct node {
int p, // 父亲节点
l, // 左儿子
r; // 右儿子
int pre;
int val, // 节点值
sum; // 异或和
int key; // 权值
bool rev; // 翻转标记

node()
: p(0), l(0), r(0), pre(0), val(0), sum(0), key(rand()), rev(false) {}
} tr[N];

void pushup(int u) {
// 计算异或和
tr[u].sum = tr[tr[u].l].sum ^ tr[u].val ^ tr[tr[u].r].sum;

// 标记父亲节点
if (tr[u].l) tr[tr[u].l].p = u;
if (tr[u].r) tr[tr[u].r].p = u;
}

void pushdown(int u) {
if (!tr[u].rev) return;

tr[u].rev = false;
std::swap(tr[u].l, tr[u].r);
tr[tr[u].l].rev ^= 1;
tr[tr[u].r].rev ^= 1;
}

std::pair<int, int> split(int u) {
if (st.empty()) {
pushdown(u);
auto t = std::make_pair(u, tr[u].r);
tr[u].r = 0;
pushup(u);

return t;
}

bool d = st.top() ^ tr[u].rev;
st.pop();

pushdown(u);

if (d) {
auto t = split(tr[u].l);
tr[u].l = t.second;
pushup(u);

return std::make_pair(t.first, u);
}

auto t = split(tr[u].r);
tr[u].r = t.first;
pushup(u);

return std::make_pair(u, t.second);
}

// 合并
int merge(int x, int y) {
if (!x || !y) return x | y;

if (tr[x].key < tr[y].key) {
pushdown(x);
tr[x].r = merge(tr[x].r, y);
pushup(x);
return x;
}

pushdown(y);
tr[y].l = merge(x, tr[y].l);
pushup(y);
return y;
}

// 是否是根节点
bool isRoot(int u) {
return !tr[u].p || (tr[tr[u].p].l != u && tr[tr[u].p].r != u);
}

// 查找根节点
int findRoot(int u) {
while (!st.empty()) st.pop();
while (!isRoot(u)) {
// pushdown(u);
st.push(tr[tr[u].p].l == u);
u = tr[u].p;
}
return u;
}

int findLeft(int u) {
u = findRoot(u);
pushdown(u);
while (tr[u].l) {
u = tr[u].l;
pushdown(u);
}
return u;
}

int access(int u) {
int lst = 0;

while (u) {
auto t = split(findRoot(u));
tr[findLeft(lst)].pre = 0;
lst = merge(t.first, lst);
tr[findLeft(t.second)].pre = u;
u = tr[findLeft(lst)].pre;
}

return lst;
}

void makeRoot(int u) {
tr[access(u)].rev ^= 1;
}

public:
int getRoot(int u) {
return findLeft(access(u));
}

void link(int x, int y) {
makeRoot(x);
tr[x].pre = y;
}

void cut(int x, int y) {
makeRoot(x);
access(y);
access(x);
tr[y].pre = 0;
}

int query(int x, int y) {
makeRoot(x);
access(y);

auto t = split(findRoot(y));
int res = tr[t.first].sum;
merge(t.first, t.second);

return res;
}

void change(int u, int val) {
makeRoot(u);
auto t = split(findRoot(u));
tr[u].val = val;
merge(t.first, t.second);
}

void set(int u, int val) {
tr[u].sum = tr[u].val = val;
}
} lct;

int n, m;

int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);

cin >> n >> m;

for (int i = 1, x; i <= n; i++) {
cin >> x;

lct.set(i, x);
}

while (m--) {
int op, x, y;

cin >> op >> x >> y;

switch (op) {
case 0: {
cout << lct.query(x, y) << endl;

break;
}
case 1: {
if (lct.getRoot(x) != lct.getRoot(y)) {
lct.link(x, y);
}

break;
}
case 2: {
lct.cut(x, y);

break;
}
case 3: {
lct.change(x, y);

break;
}
}
}

return 0;
}

#参考资料

  1. 平衡树 & LCT,石家庄市第二中学信息学奥赛集训(线下授课),张闰清,2022 年 7 月 12 日。