-
Notifications
You must be signed in to change notification settings - Fork 9
/
centroid.java
58 lines (48 loc) · 1.11 KB
/
centroid.java
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
ArrayList<ArrayList<pair>> g=new ArrayList<>();
LinkedList<Integer> q=new LinkedList<>();
boolean blocked[];
int[] subTreeSum,parent;
long solveTree(int root,int componentSize){
return 0l;
}
void dfs(int s,int par){
ArrayList<pair> adj=g.get(s);
subTreeSum[s]=1;
parent[s]=par;
for(pair p:adj){
if(p.x==par || blocked[p.x]) continue;
dfs(p.x,s);
subTreeSum[s]+=subTreeSum[p.x];
}
}
long go(int entryP){
dfs(entryP,entryP);
int centroid=entryP;
int bestSize = subTreeSum[entryP];
q.add(entryP);
int componentSize=0;
while(!q.isEmpty()){
int i=q.poll();
componentSize++;
int size=subTreeSum[entryP]-subTreeSum[i];
ArrayList<pair> adj=g.get(i);
for(pair p:adj){
if( p.x == parent[i] || blocked[p.x] ) continue;
q.add(p.x);
size=Math.max(size,subTreeSum[p.x]);
}
if( bestSize > size ){
bestSize=size;
centroid=i;
}
}
// pn("centroid "+centroid);
// long ways = solveTree(centroid,componentSize);
// blocked[centroid] =true;
// ArrayList<pair> adj=g.get(centroid);
// for(pair p:adj){
// if(blocked[p.x]) continue;
// ways += go(p.x);
// }
// return ways;
}