• 无向图求割(找桥)tarjan


    本博客参考了李煜东的《算法竞赛进阶指南》,大家要是觉得这篇文章写的不错请大家支持正版。豆瓣图书

    我在之前的博客中讲解了搜索序时间戳,这次我们讲讲追溯值的概念。

    追溯值:

        设subtree(x)表示搜索树中,以X为根的子树。low[x]定义为一下节点的时间戳最小值:

        1.subtree(x)中的节点。

         2.通过1条不在搜素树上的边,能够到达subtree(x)的节点。

          

    以上图为例。为了叙述简便,我们用时间戳代替节点编号。subtree(2)={2,3,4,5}。零位,节点1通过搜索树边的(1,5)能够到达subtree(2)。所以low[2]=1。根据定义拉算的话,首先应该让low[x]=dfn[x],然后考虑从x出发的每条边(x,y);

    若在搜素树上x是y 的父节点,则令low[x]=min(low[x],low[y]).

    若无向边(x,y)不是搜索树边,则令low[x]=min(low[x],dfn[y]).

    该图中写出了追溯值的 图。

    割边的判定法则:

    无向边x---y如果是桥,当且仅当搜索树上存在x的存在y满足 dfn[x]<low[y],说明从y出发不可能通过非搜索树边回到x。也即是x--y是桥。

    //模板
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <map>
    #include <stack>
    using namespace std;
    const int N=100010;
    int head[N],ver[N*2],Next[N*2];
    int dfn[N],low[N],n,m,tot,num;
    bool brige[N*2];
    void add(int x,int y){
        ver[++tot]=y,Next[tot]=head[x],head[x]=tot;
    }
    void tarjan(int x,int inedge)
    {
    
        dfn[x]=low[x]=++num;
        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])
                {
                    brige[i]=brige[i^1]=1;
                }
            }
            else if(i!=(inedge^1))
                low[x]=min(low[x],dfn[y]);
        }
    }
    int main()
    {
        cin>>n>>m;
        tot=1;
        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);
        int ans=0;
        for(int i=2;i<tot;i+=2)
        {
            if(brige[i])
            {
               printf("%d %d
    ",ver[i^1],ver[i]);
            }
        }
    }
    
  • 相关阅读:
    monkey之monkey日志分析
    ros 阅读记录 1
    Occupancy Grid Maps 资料记录
    双目稠密点云的资料记录
    ros 在callback中publish (不用类的方法)
    ros中同时订阅两个topic(2张图像)合并成一个topic(1张图像)
    编译 链接和加载 (转)
    制作自己的livecd
    源码安装ROS Melodic Python3 指南 (转) + 安装记录
    Depth from Videos in the Wild 解读
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798672.html
Copyright © 2020-2023  润新知