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 }
  • 相关阅读:
    进阶篇:3.2.5)DFM钣金-常见装配和成形结构
    基础篇:3.4)3d模型绘制的好坏会影响产品合格率(注意点)
    进阶篇:2.1)DFMA实施障碍和关键
    [洛谷P2224][题解][HNOI2001]产品加工
    [洛谷P1262][题解]间谍网络
    [洛谷P3919][题解]可持久化数组&&主席树讲解
    [洛谷P5677][题解][GZOI2017]配对统计
    [洛谷P1040][题解]加分二叉树
    [校内赛3-1][题解]folder
    [校内赛3-3][题解]block
  • 原文地址:https://www.cnblogs.com/maple-kingdom/p/maple-kingdom_star.html
Copyright © 2011-2022 走看看