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

Method getSize2

src/class185/Code01_Wave1.java:202–228  ·  view source on GitHub ↗
(int cur, int fa)

Source from the content-addressed store, hash-verified

200
201 // getSize1的迭代版
202 public static void getSize2(int cur, int fa) {
203 stacksize = 0;
204 push(cur, fa, 0, -1);
205 while (stacksize > 0) {
206 pop();
207 if (e == -1) {
208 siz[u] = 1;
209 e = head[u];
210 } else {
211 e = nxt[e];
212 }
213 if (e != 0) {
214 push(u, f, 0, e);
215 int v = to[e];
216 if (v != f && !vis[v]) {
217 push(v, u, 0, -1);
218 }
219 } else {
220 for (int ei = head[u]; ei > 0; ei = nxt[ei]) {
221 int v = to[ei];
222 if (v != f && !vis[v]) {
223 siz[u] += siz[v];
224 }
225 }
226 }
227 }
228 }
229
230 public static int getCentroid(int u, int fa) {
231 // getSize1(u, fa);

Callers 1

getCentroidMethod · 0.95

Calls 2

pushMethod · 0.95
popMethod · 0.95

Tested by

no test coverage detected