洛谷-3388 【模板】割点(割顶)

版权声明:试试这是啥 https://blog.csdn.net/mkopvec/article/details/91488341

题目描述
给出一个n个点,m条边的无向图,求图的割点。
输入格式
第一行输入n,m
下面m行每行输入x,y表示x到y有一条边
输出格式
第一行输出割点个数
第二行按照节点编号从小到大输出节点,用空格隔开

输入输出样例
输入 #1
6 7
1 2
1 3
1 4
2 5
3 5
4 5
5 6

输出 #1
1
5

说明/提示
对于全部数据,n≤20000,m≤100000
点的编号均大于0小于等于n。
tarjan图不一定联通。

#include<bits/stdc++.h>
using namespace std;
struct edge{
    int nxt,mark;
}pre[200010];
int n,m,idx,cnt,tot;
int head[100010],DFN[100010],LOW[100010];
bool cut[100010];
void add (int x,int y){
    pre[++cnt].nxt=y;
    pre[cnt].mark=head[x];
    head[x]=cnt;
}
void tarjan (int u,int fa){
    DFN[u]=LOW[u]=++idx;
    int child=0;
    for (int i=head[u];i!=0;i=pre[i].mark){
        int nx=pre[i].nxt;
        if (!DFN[nx]){
            tarjan (nx,fa);
            LOW[u]=min (LOW[u],LOW[nx]);
            if (LOW[nx]>=DFN[u]&&u!=fa)
                cut[u]=1;
            if(u==fa)
                child++;
        }
        LOW[u]=min (LOW[u],DFN[nx]);
    }
    if (child>=2&&u==fa)
        cut[u]=1;
}
int main(){
    memset (DFN,0,sizeof (DFN));
    memset (head,0,sizeof (head));
    scanf ("%d%d",&n,&m);
    for (int i=1;i<=m;i++){
        int a,b;
        scanf ("%d%d",&a,&b);
        add(a,b);
        add(b,a);
    }
    for(int i=1;i<=n;i++) if(DFN[i]==0) tarjan(i,i);
    for(int i=1;i<=n;i++) if(cut[i]) tot++;
    printf("%d\n",tot);
    for(int i=1;i<=n;i++) if(cut[i]) printf ("%d ",i);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/mkopvec/article/details/91488341
今日推荐