//2.【非递归版】 void solve(int root) { static Nod stk[maxn]; memset(vis, 0, sizeof(vis)); stk[0].init(root, E[root]); int len = 1; int lev = 0; cnt = 0; route[root] = 0; //update route! while(1) { Nod & cur = stk[len-1]; //here, b means current Nod, nxt means nxt's pointer(as buf.nxt)! if(false == vis[cur.b]) { vis[cur.b] = 1; F[++ cnt] = cur.b; rmq[cnt] = lev; P[cur.b] = cnt; } else if(cur.nxt == -1) { if(--len == 0) break; F[++ cnt] = stk[len-1].b; rmq[cnt] = -- lev; } else { int & i = cur.nxt; if(!vis[buf[i].b]) { route[buf[i].b] = route[cur.b] + V[i]; //update route! lev ++; stk[len ++].init(buf[i].b, E[buf[i].b]); } i = buf[i].nxt; } } st.init(2 * n - 1); }
void solve(int root) { memset(vis, 0, sizeof(vis)); route[root] = cnt = 0; dfs(root, 0); st.init(2*n-1); }