| 95 | |
| 96 | // dfs1的迭代版 |
| 97 | public static void dfs3() { |
| 98 | stacksize = 0; |
| 99 | push(root, 0, -1); |
| 100 | while (stacksize > 0) { |
| 101 | pop(); |
| 102 | if (edge == -1) { |
| 103 | fa[first] = second; |
| 104 | dep[first] = dep[second] + 1; |
| 105 | siz[first] = 1; |
| 106 | edge = head[first]; |
| 107 | } else { |
| 108 | edge = next[edge]; |
| 109 | } |
| 110 | if (edge != 0) { |
| 111 | push(first, second, edge); |
| 112 | if (to[edge] != second) { |
| 113 | push(to[edge], first, -1); |
| 114 | } |
| 115 | } else { |
| 116 | for (int e = head[first], v; e > 0; e = next[e]) { |
| 117 | v = to[e]; |
| 118 | if (v != second) { |
| 119 | siz[first] += siz[v]; |
| 120 | if (son[first] == 0 || siz[son[first]] < siz[v]) { |
| 121 | son[first] = v; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // dfs2的迭代版 |
| 130 | public static void dfs4() { |