| 113 | |
| 114 | // dfs1的迭代版 |
| 115 | public static void dfs3() { |
| 116 | stacksize = 0; |
| 117 | push(1, 0, -1); |
| 118 | while (stacksize > 0) { |
| 119 | pop(); |
| 120 | if (edge == -1) { |
| 121 | fa[first] = second; |
| 122 | dep[first] = dep[second] + 1; |
| 123 | siz[first] = 1; |
| 124 | edge = head[first]; |
| 125 | } else { |
| 126 | edge = next[edge]; |
| 127 | } |
| 128 | if (edge != 0) { |
| 129 | push(first, second, edge); |
| 130 | if (to[edge] != second) { |
| 131 | push(to[edge], first, -1); |
| 132 | } |
| 133 | } else { |
| 134 | for (int e = head[first], v; e > 0; e = next[e]) { |
| 135 | v = to[e]; |
| 136 | if (v != second) { |
| 137 | siz[first] += siz[v]; |
| 138 | if (son[first] == 0 || siz[son[first]] < siz[v]) { |
| 139 | son[first] = v; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // dfs2的迭代版 |
| 148 | public static void dfs4() { |