割边判定(模板)

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

无向边(x,y)是桥,当且仅当搜索树上存在x的一个子节点y,满足:dfn【x】<low【y】。

low【x】数组称作回溯值:指x的子树中的节点中或者这些子节点+x父节点(非树边连接)中时间戳值最小的点,

#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];//该点两个low值来源中最小的时间戳值
int n,m,tot,num;//边数、时间戳
bool bridge[2*SIZE];
void add(int x,int y)
{
    ver[++tot]=y;
    Next[tot]=head[x];
    head[x]=tot;
}
void tarjan(int x,int in_edge)
{
    dfn[x]=low[x]=++num;//从上往下搜索时预处理x点的时间戳和回溯值
    for(int i=head[x];i;i=Next[i])
    {
        int y=ver[i];
        if(!dfn[y])
        {
            tarjan(y,i);
            low[x]=min(low[x],low[y]);//

            if(low[y]>dfn[x])bridge[i]=bridge[i^1]=true;//满足定理条件:割边
        }
        else if(i!=(in_edge^1))low[x]=min(low[x],dfn[y]);//low【x】值的两种来源之后一种,y通过一条非树边到达x
    }
}
int main()
{
    cin>>n>>m;
    tot=0;
    num=0;
    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,0);//搜一片森林
    for(int i=1;i<tot;i+=2)
        if(bridge[i])printf("%d %d",ver[i^1],ver[i]);
    return 0;
}

The end;

猜你喜欢

转载自blog.csdn.net/qq_41661919/article/details/85409198
今日推荐