Cut point (template) Luogu 3388

topic background

cut point

Topic description

Given an undirected graph with n points and m edges, find the cut point of the graph.

Input and output format

Input format:
input n,m in the first line

The following m lines each input x, y indicates that there is an edge from x to y

Output format:
the first line outputs the number of cut points

The second line outputs the nodes according to the node number from small to large, separated by spaces

Input and output example

Input Sample #1: Copy
6 7
1 2
1 3
1 4
2 5
3 5
4 5
5 6
Output Sample #1: Copy
1
5
Description

n, m are both 100000

The tarjan map is not necessarily connected! ! !

#include<iostream>
#include<cstdio>

using namespace std;
const int MAXN = 100010;

struct Edge {
    int nxt,to;
} edge[MAXN*2];

int n,m,dfn[MAXN],low[MAXN],tot,cnt;
int head[MAXN],num;
bool cut[MAXN];

inline void add(int bg,int ed) {
    edge[++cnt].to=ed;
    edge[cnt].nxt=head[bg];
    head[bg]=cnt;
}

inline void tarjan(int u,int fa) {
    dfn[u]=low[u]=++num;
    int child=0;
    for(int i=head[u]; i; i=edge[i].nxt) {
        int v=edge[i].to;
        if(!dfn[v]) {
            tarjan(v,fa);
            low[u]=min(low[v],low[u]);
            if(low[v]>=dfn[u] && u!=fa)
                cut[u]=1;
            if(u==fa)
                child++;
        }
        low[u]=min(low[u],dfn[v]);
        if(child>=2 && u==fa)
            cut[u]=1;
    }
}

inline void debug() {
    for(int i=1; i<=n; i++)
        printf("%d %d %d\n",i,dfn[i],low[i]);
}

int main() {
    scanf("%d%d",&n,&m);
    for(int i=1; i<=m; i++) {
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    for(int i=1; i<=n; i++)
        if(!dfn[i]) tarjan(i,i);
    //debug();
    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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324830368&siteId=291194637