洛谷P3388 【模板】割点(割顶)

题目链接

题目背景
割点

题目描述
给出一个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,m均为100000

tarjan 图不一定联通!!!

tarjan算法详解

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
   int s=0,w=1;
   char ch=getchar();
   while(ch<='0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
const int N = 100010;
struct node
{
    int to, next;
}g[N*2];
int last[N], gl;
inline void add(int x, int y)
{
    g[++gl] = (node){y, last[x]};
    last[x] = gl;
    g[++gl] = (node){x, last[y]};
    last[y] = gl;
    return ;
}
int dfn[N], cnt, low[N], ans;
bool bj[N];
void tarjan(int x, int f)
{
    int son = 0;
    low[x] = dfn[x] = ++cnt;
    for(int i = last[x]; i; i = g[i].next)
    {
        int to = g[i].to;
        if(!dfn[to])
        {
            tarjan(to, x);
            if(!f)
                son++;
            low[x] = min(low[x], low[to]);
            if(low[to] >= dfn[x] && !bj[x] && f)
                bj[x] = 1, ans++;
        }
        else low[x] = min(low[x], dfn[to]);
    }
    if(!f && son > 2) bj[x] = 1, ans++;
    return ;
}
int main()
{
    int n = read(), m = read();
    for(register int i = 1; i <= m; i++)
        add(read(), read());
    for(register int i = 1; i <= n; i++)
        if(!dfn[i])
            tarjan(i, 0);
    printf("%d\n", ans);
    for(register int i = 2; i <= n; i++)
        if(bj[i])
            printf("%d ", i);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值