D17 Tarjan 割边

视频链接:https://www.bilibili.com/video/BV14g411Q7ze

// Luogu P1656 炸铁路(可处理重边)
// 链式邻接表 
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

const int N=210,M=10010;
int n,m,a,b,cnt;
struct edge{int u,v;};
vector<edge>e;
vector<int>h[N];
int dfn[N],low[N],tot;
struct bridge{
  int x,y;
  bool operator<(const bridge &t)const{
    if(x==t.x)return y<t.y;
    return x<t.x;
  }
}bri[M];

void add(int a,int b){
  e.push_back({a,b});
  h[a].push_back(e.size()-1);
}
void tarjan(int x,int in_edge){
  dfn[x]=low[x]=++tot;
  for(int i=0;i<h[x].size();i++){
    int j=h[x][i], y=e[j].v;
    if(!dfn[y]){//若y尚未访问
      tarjan(y,j);
      low[x]=min(low[x],low[y]);
      if(low[y]>dfn[x]){
        bri[cnt++]={x,y};
      }
    }
    else if(j!=(in_edge^1))//不是反边
      low[x]=min(low[x],dfn[y]);
  }
}
int main(){
  cin>>n>>m;
  while(m --){
    cin>>a>>b;
    add(a,b),add(b,a);
  }
  for(int i=1; i<=n; i++)
    if(!dfn[i])tarjan(i,0);

  sort(bri,bri+cnt);  
  for(int i=0;i<cnt;i++)
    printf("%d %d\n",bri[i].x,bri[i].y);
  return 0;
}

// Luogu P1656 炸铁路(可处理重边)
// 链式前向星
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N=210,M=10010;
int n,m,a,b,cnt;
struct edge{int v,ne;}e[M];
int h[N],idx=1;//从2,3开始配对
int dfn[N],low[N],tot;
struct bridge{
  int x,y;
  bool operator<(const bridge &t)const{
    if(x==t.x)return y<t.y;
    return x<t.x;
  }
}bri[M];

void add(int a,int b){
  e[++idx]={b,h[a]};
  h[a]=idx;
}
void tarjan(int x,int in_edge){
  dfn[x]=low[x]=++tot;
  for(int i=h[x];i;i=e[i].ne){
    int y=e[i].v;
    if(!dfn[y]){//若y尚未访问
      tarjan(y,i);
      low[x]=min(low[x],low[y]);
      if(low[y]>dfn[x]){
        bri[cnt++]={x,y};
      }
    }
    else if(i!=(in_edge^1))//不是反边
      low[x]=min(low[x],dfn[y]);
  }
}
int main(){
  cin>>n>>m;
  while(m --){
    cin>>a>>b;
    add(a,b),add(b,a);
  }
  for(int i=1; i<=n; i++)
    if(!dfn[i])tarjan(i,0);

  sort(bri,bri+cnt);  
  for(int i=0;i<cnt;i++)
    printf("%d %d\n",bri[i].x,bri[i].y);
  return 0;
}

 

posted @ 2022-05-28 13:31  董晓  阅读(701)  评论(0编辑  收藏  举报