一种抠环的办法
int fa[maxn]; //dfs时的父亲 int dfn[maxn], idx; //访问的时间 int loop[maxn], cnt; //环 void get_loop(int u) { dfn[u] = ++ idx; for (int i = head[u];i;i=ed[i].nxt) { int v = ed[i].to; if(v == fa[u]) continue ; if(dfn[v]) { if(dfn[v] < dfn[u]) continue ; loop[++ cnt] = v; for ( ; v != u; v = fa[v]) loop[++ cnt] = fa[v]; } else fa[v] = u, get_loop(v); } }