Luogu P1656 Railway Explosion [Tarjan Bridge Formwork Question]

Keywords: shell


Portal: P1656

Deep fried Railway

Problem Description
Because a country is ruled by the brutal high-pressure violence of a red regime. The U.S. sent General uim to take strategic measures against the country to save charred life.
There are n cities in the country. These cities are connected by railways. Any two cities can arrive directly or indirectly through the railway.
uim found that after some railways were destroyed, two cities could not reach each other by rail. Such railways are called key road s.
uim in order to paralyze the country's logistics system as soon as possible, we hope to blow up the railway so as to achieve the effect that two cities can not reach each other by rail.
However, there was only one shell (Congress did not pay for it). So which railway can he bomb?

Input
The first row n, m (1<=n<=150, 1<=m<=5000), respectively, shows that there are n cities, a total of M railways.
The following m lines, each row of two integers a, b, indicate that there is a direct connection between a and city b.

Output
The output has several lines.
Each line contains two digits A and B (a < b), indicating that < a, b > is the key road.
Note: When output, all pairs of numbers < a, b > must be sorted from small to large in order of a; if a is the same, then from small to large in order of B.

Sample Input
6 6
1 2
2 3
2 4
3 5
4 5
5 6

Sample Output
1 2
5 6


The naked tarjan seeks the bridge, later uses as a template.


AC Code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<stack>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=110000;
int n,m,idx,ans;
int dfn[N],low[N];
vector<int> g[2*N];
struct edge
{
  int from,to;
}e[2*N];

void init()
{
  memset(dfn,0,sizeof(dfn));
  memset(low,0,sizeof(low));
  for(int i=0;i<2*N;i++)
  {
    g[i].clear();
  }
  idx=0,ans=0;
}
bool cmp(edge x,edge y)
{
  if(x.from==y.from) return x.to<y.to;
  return x.from<y.from;
}
void add_edge(int x,int y)
{
  e[ans].from=min(x,y);
  e[ans].to=max(x,y);
  ans++;
}
void tarjan(int cur,int fa)
{
  int child;
  dfn[cur]=low[cur]=++idx;
  int len=g[cur].size();
  for(int i=0;i<len;i++)
  {
    child=g[cur][i];
    if(!dfn[child])
    {
      tarjan(child,cur);
      low[cur]=min(low[cur],low[child]);
      if(low[child]>dfn[cur])//Condition of bridge
        add_edge(child,cur);
    }
    else if(dfn[child]<dfn[cur]&&child!=fa)
    {
      low[cur]=min(low[cur],dfn[child]);
    }
  }
}
int main()
{
  int a,b;
  while(scanf("%d%d",&n,&m)!=EOF)
  {
    init();
    for(int i=0;i<m;i++)
    {
      scanf("%d%d",&a,&b);
      g[a].push_back(b);
      g[b].push_back(a);
    }
    for(int i=1;i<=n;i++)
    {
      if(!dfn[i])
      {
        tarjan(i,i);
      }
    }
    sort(e,e+ans,cmp);
    for(int i=0;i<ans;i++)
    {
      printf("%d %d\n",e[i].from,e[i].to);
    }
  }
  return 0;
}

Posted by pratheshshah on Sat, 05 Oct 2019 00:41:07 -0700