-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlink_cut.cpp
137 lines (118 loc) · 2.62 KB
/
link_cut.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
/*
In the name of ALLAH
Author : Raashid Anwar
*/
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int M1 = 998244353;
const int M2 = 1000000007;
mt19937 rng((uint64_t)chrono::steady_clock::now().time_since_epoch().count());
class link_cut {
struct node {
int p = 0, pp = 0, val = 0, dp = 0, flip = 0;
vector <int> c = {0, 0};
};
vector <node> T;
public:
link_cut(int n) {
T.resize(n + 1);
}
int dir(int x, int y) {
return T[x].c[1] == y;
}
void set(int x, int d, int y) {
if (x) {
T[x].c[d] = y;
pull(x);
}
if (y) {
T[y].p = x;
}
}
void pull(int x) {
if (x) {
T[x].dp = max({T[x].val, T[T[x].c[0]].dp, T[T[x].c[1]].dp});
}
}
void push(int x) {
if (x && T[x].flip) {
int &l = T[x].c[0], &r = T[x].c[1];
swap(l, r);
T[l].flip ^= 1;
T[r].flip ^= 1;
T[x].flip = 0;
}
}
void rotate(int x, int d) {
int y = T[x].p, z = T[y].p, w = T[x].c[d];
swap(T[x].pp, T[y].pp);
set(y, !d, w);
set(x, d, y);
set(z, dir(z, y), x);
}
void splay(int x) {
for (push(x); T[x].p;) {
int y = T[x].p, z = T[y].p;
push(z); push(y); push(x);
int dx = dir(y, x), dy = dir(z, y);
if (!z) {
rotate(x, !dx);
} else if (dx == dy) {
rotate(y, !dx), rotate(x, !dx);
} else {
rotate(x, dy), rotate(x, dx);
}
}
}
void make_root(int u) {
access(u);
int l = T[u].c[0];
T[l].flip ^= 1;
swap(T[l].p, T[l].pp);
set(u, 0, 0);
}
void access(int _u) {
for (int v = 0, u = _u; u; u = T[v = u].pp) {
splay(u); splay(v);
int r = T[u].c[1];
T[v].pp = 0;
swap(T[r].p, T[r].pp);
set(u, 1, v);
}
splay(_u);
}
void link(int u, int v) {
assert(!connected(u, v));
make_root(v);
T[v].pp = u;
}
void cut(int u, int v) {
make_root(u); access(u); splay(v);
assert(T[v].pp == u);
T[v].pp = 0;
}
bool connected(int u, int v) {
if (u == v) return true;
make_root(u); access(v); splay(u);
return T[v].p == u || T[T[v].p].p == u;
}
int get_path(int u, int v) {
make_root(u);
access(v);
return v;
}
int find_root(int u) {
access(u);
for (; T[u].c[0]; u = T[u].c[0]);
access(u);
return u;
}
};
void solve() {
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}