-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCentroidDecomposition.cpp
More file actions
176 lines (161 loc) · 4.16 KB
/
Copy pathCentroidDecomposition.cpp
File metadata and controls
176 lines (161 loc) · 4.16 KB
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
/**
* Author: Kevin Li
* Lang: C++
* Description: Centroid decomposition on a tree
*/
#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <unordered_map>
using namespace std;
#define MAXN 1000000
#define MAXH 30
struct centroid_decomposition {
typedef long long ll;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define f first
#define s second
int n,h;
vector<int> *e;
vector<int> *ctree;
int *cp;
int *layer;
int maxl;
int *stsize;
int croot;
int ss;
int **st;
bool **viss;
bool **vist;
bool *cvis;
bool *cpvis;
int **anc;
centroid_decomposition (int _n) : n(_n) {
e = new vector<int>[n];
ctree = new vector<int>[n];
ss = 0;
h = ceil((double)(log(double(n))/log(2.0)));
st = new int*[h];
viss = new bool*[h];
vist = new bool*[h];
cvis = new bool[n];
cpvis = new bool[n];
cp = new int[n];
layer = new int[n];
stsize = new int[n];
maxl = 0;
for (int i = 0; i < n; i++) {
cvis[i] = false;
cpvis[i] = false;
cp[i] = 0;
layer[i] = 0;
stsize[i] = 0;
}
croot = 0;
for (int i = 0; i < h; i++) {
st[i] = new int[n];
viss[i] = new bool[n];
vist[i] = new bool[n];
for (int j = 0; j < n; j++) {
st[i][j] = 0;
viss[i][j] = false;
vist[i][j] = false;
}
}
}
void add(int u, int v) {
e[u].pb(v);
e[v].pb(u);
}
void sdfs(int node, int depth) {
viss[depth][node] = true;
ss++;
st[depth][node] = 1;
for (int i = 0; i < e[node].size(); i++) {
if (!viss[depth][e[node][i]] && !cvis[e[node][i]]) {
sdfs(e[node][i],depth);
st[depth][node]+=st[depth][e[node][i]];
}
}
}
int centroid(int root, int depth) {
bool c = true;
vist[depth][root] = true;
int heavy = -1;
for (int i = 0; i < e[root].size(); i++) {
if (!vist[depth][e[root][i]] && !cvis[e[root][i]]) {
if (st[depth][e[root][i]] > ss/2) {
c = false;
}
if (heavy == -1 || st[depth][e[root][i]]>st[depth][heavy]) {
heavy = e[root][i];
}
}
}
if (c && ss-st[depth][root]<=ss/2) {
return root;
}
return centroid(heavy,depth);
}
int decompose(int root, int depth) {
ss = 0;
sdfs(root,depth);
int c = centroid(root,depth);
cvis[c] = true;
stsize[c] = ss;
return c;
}
int centroiddecomposition(int root, int depth) {
int c = decompose(root,depth);
for (int i = 0; i < e[c].size(); i++) {
if (!cvis[e[c][i]]) {
int cs = centroiddecomposition(e[c][i],depth+1);
ctree[c].pb(cs);
ctree[cs].pb(c);
}
}
return c;
}
void cparentdfs(int node, int depth) {
cpvis[node] = true;
layer[node] = depth;
maxl = max(maxl,depth);
for (int i = 0; i < ctree[node].size(); i++) {
if (!cpvis[ctree[node][i]]) {
cp[ctree[node][i]] = node;
cparentdfs(ctree[node][i],depth+1);
}
}
}
void cparent() {
cp[croot] = -1;
cparentdfs(croot,0);
}
void driver() {
croot = centroiddecomposition(0,0);
cparent();
}
void ancd() {
anc = new int*[h];
for (int i = 0; i < h; i++) {
anc[i] = new int[n];
for (int j = 0; j < n; j++) {
anc[i][j] = -1;
}
}
for (int i = 0; i < n; i++) {
int cur = layer[i];
int ver = i;
while (cur >= 0) {
anc[cur][i] = ver;
ver = cp[ver];
cur--;
}
}
}
};
int main() {
}