仙人掌找环

点仙人掌和边仙人掌都能用

int n, cnt, dep[N], fa[N];
struct _ {int y, id;};
vector<_> g[N];
vector<int> v[N];
void get(int x, int y){
    if (dep[x] < dep[y]) return;
    ++cnt;
    v[cnt].pb(y);
    while(x ^ y) v[cnt].pb(x), x = fa[x];
}
void dfs(int x, int f){
    fa[x] = f,dep[x] = dep[f]+1;
    for (_ e : g[x]) if (e.y ^ f){
        int y = e.y;
        if (dep[y]) get(x,y);
        else dfs(y,x);
    }  
}
原文地址:https://www.cnblogs.com/XiaoVsun/p/13054053.html