MCPcopy Create free account
hub / github.com/algorithmzuo/algorithm-journey / getCentroid

Method getCentroid

src/class183/Code03_Race1.java:118–136  ·  view source on GitHub ↗
(int u, int fa)

Source from the content-addressed store, hash-verified

116 }
117
118 public static int getCentroid(int u, int fa) {
119 // getSize1(u, fa);
120 getSize2(u, fa);
121 int half = siz[u] >> 1;
122 boolean find = false;
123 while (!find) {
124 find = true;
125 for (int e = head[u]; e > 0; e = nxt[e]) {
126 int v = to[e];
127 if (v != fa && !vis[v] && siz[v] > half) {
128 fa = u;
129 u = v;
130 find = false;
131 break;
132 }
133 }
134 }
135 return u;
136 }
137
138 // 收集信息递归版,java会爆栈,C++可以通过
139 public static void dfs1(int u, int fa, int dis, int edge) {

Callers 2

solveMethod · 0.95
mainMethod · 0.95

Calls 1

getSize2Method · 0.95

Tested by

no test coverage detected