| 144 | |
| 145 | // dfs2的迭代版 |
| 146 | public static void dfs4() { |
| 147 | stacksize = 0; |
| 148 | push(1, 1, -1); |
| 149 | while (stacksize > 0) { |
| 150 | pop(); |
| 151 | if (edge == -1) { // edge == -1,表示第一次来到当前节点,并且先处理重儿子 |
| 152 | top[first] = second; |
| 153 | dfn[first] = ++cntd; |
| 154 | seg[cntd] = first; |
| 155 | if (son[first] == 0) { |
| 156 | continue; |
| 157 | } |
| 158 | push(first, second, -2); |
| 159 | push(son[first], second, -1); |
| 160 | continue; |
| 161 | } else if (edge == -2) { // edge == -2,表示处理完当前节点的重儿子,回到了当前节点 |
| 162 | edge = head[first]; |
| 163 | } else { // edge >= 0, 继续处理其他的边 |
| 164 | edge = next[edge]; |
| 165 | } |
| 166 | if (edge != 0) { |
| 167 | push(first, second, edge); |
| 168 | if (to[edge] != fa[first] && to[edge] != son[first]) { |
| 169 | push(to[edge], to[edge], -1); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | public static void up(int i) { |
| 176 | min[i] = Math.min(min[i << 1], min[i << 1 | 1]); |