-
Notifications
You must be signed in to change notification settings - Fork 9
/
centroid_decomp.cpp
64 lines (54 loc) · 1.06 KB
/
centroid_decomp.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
// centroid_decomp.cpp
// Eric K. Zhang; Jul. 9, 2018
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100013
int N;
vector<int> adj[MAXN];
namespace cd {
int sz[MAXN];
int cpar[MAXN];
bool vis[MAXN];
void dfs(int n, int p=-1) {
sz[n] = 1;
for (int v : adj[n]) if (v != p && !vis[v]) {
dfs(v, n);
sz[n] += sz[v];
}
}
int centroid(int n) {
dfs(n);
int num = sz[n];
int p = -1;
do {
int nxt = -1;
for (int v : adj[n]) if (v != p && !vis[v]) {
if (2 * sz[v] > num)
nxt = v;
}
p = n, n = nxt;
} while (~n);
return p;
}
void centroid_decomp(int n=0, int p=-1) {
int c = centroid(n);
vis[c] = true;
cpar[c] = p;
for (int v : adj[c]) if (!vis[v]) {
centroid_decomp(v, c);
}
}
}
using cd::centroid_decomp;
using cd::cpar;
int main() {
N = 5;
vector<pair<int, int> > edges = {{0, 1}, {1, 2}, {1, 3}, {3, 4}};
for (auto p : edges) {
adj[p.first].push_back(p.second);
adj[p.second].push_back(p.first);
}
centroid_decomp();
for (int i = 0; i < N; i++)
cout << i << " -> " << cpar[i] << endl;
}