(模板)割点判定

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41661919/article/details/85412298

定理:

①x不是搜索树上的根节点(dfs起点)时,x是割点当且仅当搜索树上存在x的一个子节点y,满足dfn【x】<=low【y】。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include<algorithm>
#include <set>
#include <queue>
#include <stack>
#include<vector>
#include<map>
#include<ctime>
#define ll long long
using namespace std;
const int SIZE=100010;
int head[SIZE];
int ver[SIZE*2];
int Next[2*SIZE];
int dfn[SIZE];
int low[SIZE];//该点所在的联通块中最小的时间戳
int n,m,tot,num;//边数、时间戳
bool cut[SIZE];
void add(int x,int y)
{
    ver[++tot]=y;
    Next[tot]=head[x];
    head[x]=tot;
}

int root;//根节点,一般为“1”;
void tarjan(int x)
{
    dfn[x]=low[x]=++num;//从上往下搜索时预处理x点的时间戳和回溯值
    int flag=0;//判断根节点用标记
    for(int i=head[x];i;i=Next[i])
    {
        int y=ver[i];
        if(!dfn[y])
        {
            tarjan(y);
            low[x]=min(low[x],low[y]);

            if(low[y]>=dfn[x])//区别割边判定条件
            {
                flag++;
                if(x!=root||flag>1)cut[x]=true;//满足定理条件:所以是割点
            }
        }
        else low[x]=min(low[x],dfn[y]);//注意与求割边区别
    }
}
int main()
{
    cin>>n>>m;
    tot=0;
    num=0;
    for(int i=1;i<=m;++i)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        if(x==y)continue;
        add(x,y);
        add(y,x);
    }
    for(int i=1;i<=n;++i)
        if(!dfn[i])
        {
            root=i;
            tarjan(i);//搜索   森林
        }
    for(int i=1;i<=n;i++)
        if(cut[i])printf("%d\n",i);
    return 0;
}

The end;

猜你喜欢

转载自blog.csdn.net/qq_41661919/article/details/85412298