luogu_P3388 【模板】割点(割顶)

 https://www.luogu.org/problem/P3388

题目背景

割点

题目描述

给出一个nnn个点,mmm条边的无向图,求图的割点。

输入格式

第一行输入n,mn,mn,m

下面mmm行每行输入x,yx,yx,y表示xxx到yyy有一条边

输出格式

第一行输出割点个数

第二行按照节点编号从小到大输出节点,用空格隔开


 一个不是搜索树根节点的点x是割点,当且仅当存在至少一个搜索树上x的儿子y的low[y]>=dfn[x]

#include<iostream>
#include<cstdio>

#define ri register int
#define u int

namespace opt {

    inline u in() {
        u x(0),f(1);
        char s(getchar());
        while(s<'0'||s>'9') {
            if(s=='-') f=-1;
            s=getchar();
        }
        while(s>='0'&&s<='9') {
            x=(x<<1)+(x<<3)+s-'0';
            s=getchar();
        }
        return x*f;
    }

}

using opt::in;

#define NN 20005
#define MM 100005

#include<algorithm>

namespace mainstay {

    u N,M,h[NN],cnt(1);

    struct node {
        u to,next;
    } a[MM<<1];

    inline void add(const u &x,const u &y) {
        a[++cnt].to=y,a[cnt].next=h[x],h[x]=cnt;
    }

    u dfn[NN],low[NN],poi[NN],n,num;

    void tar(const u &x,const u &edg,const u &st) {
        dfn[x]=low[x]=++num;
        u _t(0);
        for(ri i(h[x]); i; i=a[i].next) {
            u _y(a[i].to);
            if((i^1)^edg) {
                if(!dfn[_y]) {
                    tar(_y,i,st),low[x]=std::min(low[x],low[_y]);
                    if(low[_y]>=dfn[x]) ++_t;
                } else low[x]=std::min(low[x],dfn[_y]);
            }
        }
        if(x^st) {
            if(_t) poi[++n]=x;
        } else if(_t>=2) poi[++n]=x;
    }

    inline void solve() {
        N=in(),M=in();
        for(ri i(1); i<=M; ++i) {
            u _a(in()),_b(in());
            add(_a,_b),add(_b,_a);
        }
        for(ri i(1); i<=N; ++i) {
            if(!dfn[i]) {
                tar(i,0,i);
            }
        }
        std::sort(poi+1,poi+n+1);
        printf("%d
",n);
        for(ri i(1); i<=n; ++i) printf("%d ",poi[i]);
    }

}

int main() {

    //freopen("x.txt","r",stdin);
    mainstay::solve();

}
原文地址:https://www.cnblogs.com/ling-zhi/p/11762522.html