-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentroid.cpp
59 lines (58 loc) · 1.14 KB
/
centroid.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
class Centroid{
int n;
vector<set<int> > adj;
vi par;
vi has;
public:
Centroid(vector<vi> ad)
{
n = ad.Z;
adj.resize(n);
par.resize(n);
has.resize(n);
REP(i,0,n)
for (int z : ad[i])
{
adj[i].insert(z);
adj[z].insert(i);
}
build(0,-1);
}
int dfs(int x, int p)
{
has[x] = 1;
for (int z : adj[x])
if (z!=p)
has[x] += dfs(z,x);
return has[x];
}
int centroid(int x, int p, int sz)
{
for (int z: adj[x])
if (z != p)
if (has[z] > sz/2)
return centroid(z,x,sz);
return x;
}
void build(int x,int p)
{
int n = dfs(x,p);
int c = centroid(x,p,n);
if (p == -1)
p = c;
par[c] = p;
vi tmp(adj[c].B,adj[c].E);
for (int z : tmp)
{
adj[z].erase(c);
adj[c].erase(z);
}
for (int z : tmp)
build(z,c);
}
public:
vi decompose()
{
return par;
}
};