This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dynamic_Link_Cut_Tree.cpp
137 lines (137 loc) · 3.65 KB
/
Dynamic_Link_Cut_Tree.cpp
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
#include <iostream>
#include <vector>
class LinkCutNode {
public:
int key;
LinkCutNode* parent;
LinkCutNode* leftChild;
LinkCutNode* rightChild;
bool isRoot;
bool reversed;
LinkCutNode(int k) : key(k), parent(nullptr), leftChild(nullptr), rightChild(nullptr), isRoot(true), reversed(false) {}
};
class LinkCutTree {
private:
std::vector<LinkCutNode*> nodes;
void makeRoot(LinkCutNode* x) {
access(x);
splay(x);
x->reversed = !x->reversed;
}
void link(LinkCutNode* x, LinkCutNode* y) {
makeRoot(x);
x->parent = y;
}
void cut(LinkCutNode* x) {
access(x);
splay(x);
x->leftChild->parent = nullptr;
x->leftChild = nullptr;
}
LinkCutNode* access(LinkCutNode* x) {
LinkCutNode* last = nullptr;
for (LinkCutNode* y = x; y; y = y->parent) {
splay(y);
y->rightChild = last;
last = y;
}
splay(x);
return last;
}
void splay(LinkCutNode* x) {
while (!x->isRoot) {
LinkCutNode* p = x->parent;
LinkCutNode* g = p->parent;
if (g) {
pushDown(g);
}
pushDown(p);
pushDown(x);
if (!p->isRoot) {
if ((g->leftChild == p) == (p->leftChild == x)) {
rotate(p);
} else {
rotate(x);
}
}
rotate(x);
}
}
void rotate(LinkCutNode* x) {
LinkCutNode* p = x->parent;
LinkCutNode* g = p->parent;
if (g) {
if (g->leftChild == p) {
g->leftChild = x;
} else {
g->rightChild = x;
}
}
if (p->leftChild == x) {
p->leftChild = x->rightChild;
if (x->rightChild) {
x->rightChild->parent = p;
}
x->rightChild = p;
} else {
p->rightChild = x->leftChild;
if (x->leftChild) {
x->leftChild->parent = p;
}
x->leftChild = p;
}
x->parent = g;
p->parent = x;
}
void pushDown(LinkCutNode* x) {
if (x->reversed) {
std::swap(x->leftChild, x->rightChild);
if (x->leftChild) {
x->leftChild->reversed = !x->leftChild->reversed;
}
if (x->rightChild) {
x->rightChild->reversed = !x->rightChild->reversed;
}
x->reversed = false;
}
}
public:
LinkCutTree(int n) {
nodes.reserve(n);
for (int i = 0; i < n; ++i) {
nodes.push_back(new LinkCutNode(i));
}
}
~LinkCutTree() {
for (LinkCutNode* node : nodes) {
delete node;
}
}
void link(int x, int y) {
link(nodes[x], nodes[y]);
}
void cut(int x) {
cut(nodes[x]);
}
void makeRoot(int x) {
makeRoot(nodes[x]);
}
bool connected(int x, int y) {
return access(nodes[x]) == access(nodes[y]);
}
void printRoot(int x) {
std::cout << "Root of node " << x << ": " << access(nodes[x])->key << std::endl;
}
};
int main() {
LinkCutTree linkCutTree(5);
linkCutTree.link(0, 1);
linkCutTree.link(1, 2);
linkCutTree.link(2, 3);
std::cout << "Is 0 connected to 3? " << (linkCutTree.connected(0, 3) ? "Yes" : "No") << std::endl;
linkCutTree.makeRoot(0);
linkCutTree.cut(2);
std::cout << "Is 0 connected to 3 after cut? " << (linkCutTree.connected(0, 3) ? "Yes" : "No") << std::endl;
linkCutTree.printRoot(2);
return 0;
}