zoukankan      html  css  js  c++  java
  • <学习笔记> tarjan 求割边(桥)

    简单定义:去掉一条边后,图的联通分量增加,那么我们把删去的这条边称为割边

    红色的边即为桥。

    桥和割点一样,也可以用tarjan求,代码相差不大。

     1 void Tarjan(int n,int fa)
     2 {
     3      dfn[n]=low[n]=++Index;
     4      for(int i=first[n];i;i=Next[i])
     5      {
     6           int j=Rode[i].t;
     7           if(!dfn[j])
     8           {
     9                Tarjan(j,n);
    10                low[n]=min(low[n],low[j]);
    11              if(low[j]>dfn[n]) // 这里没有等号 如果可以追溯到n,那这条边删去无影响
    12              {
    13                  Del[++cut]=(maple){n,j};
    14              }
    15           }
    16           else if(j!=fa) low[n]=min(low[n],dfn[j]); // 注意是无向图
    17      }
    18 }

    求桥与割点的例题

    hiho Coder 1183 连通性一:割边与割点

    代码

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 int N,M,cnt,cut,u,v,Index,F=0;
     9 int first[200010],Next[200010];
    10 int dfn[200010],low[200010],del[200010];
    11 
    12 struct maple{
    13     int f,t;
    14 }Rode[200010],Del[200010];
    15 
    16 void Build(int f,int t)
    17 {
    18     Rode[++cnt]=(maple){f,t};
    19     Next[cnt]=first[f];
    20     first[f]=cnt;
    21 }
    22 
    23 bool cmp(maple a,maple b)
    24 {
    25     if(a.f==b.f) return a.t<b.t;
    26     return a.f<b.f;
    27 } 
    28 void Tarjan(int n,int fa)
    29 {
    30      int cd=0;
    31      dfn[n]=low[n]=++Index;
    32      for(int i=first[n];i;i=Next[i])
    33      {
    34           int j=Rode[i].t;
    35           if(!dfn[j])
    36           {
    37                Tarjan(j,n);
    38                low[n]=min(low[n],low[j]);
    39                if(low[j]>=dfn[n]&&fa!=-1)  del[n]=1; 
    40              if(low[j]>dfn[n])
    41              {
    42                  Del[++cut]=(maple){n,j};
    43                  if(n>j) swap(Del[cut].f,Del[cut].t);
    44              }
    45              if(fa==-1) ++cd;
    46           }
    47           else if(j!=fa) low[n]=min(low[n],dfn[j]);
    48      }
    49      if(fa==-1&&cd>=2) del[n]=1;
    50 }
    51 int main()
    52 {
    53     scanf("%d%d",&N,&M);
    54     for(int i=1;i<=M;++i)
    55     {
    56         scanf("%d%d",&u,&v);
    57         Build(u,v);
    58         Build(v,u);
    59     }
    60     Tarjan(1,-1);
    61     for(int i=1;i<=N;++i)
    62         if(del[i]) printf("%d ",i),F=1;
    63     if(!F) printf("Null");
    64     printf("
    ");
    65     sort(Del+1,Del+cut+1,cmp);
    66     for(int i=1;i<=cut;++i)
    67         printf("%d %d
    ",Del[i].f,Del[i].t);
    68     return 0;
    69 }
  • 相关阅读:
    php http_build_query 将布尔值类型转为整型的问题
    一天一个 Linux 命令(34):free 命令
    Laravel 如何使用 PHP 内置的服务器启动服务
    一天一个 Linux 命令(33):top 命令
    Java基础(5)-Java数据类型
    Java int和Integer有什么区别
    Java异常处理常见问题
    PHP重载,不一样的重载
    nginx 反向代理 proxy_pass详解
    composer repositories仓库配置,命令行修改仓库地址
  • 原文地址:https://www.cnblogs.com/maple-kingdom/p/maple-kingdom_star.html
Copyright © 2011-2022 走看看