炸铁路

题目描述

因为某国被某红色政权残酷的高压暴力统治。美国派出将军uim,对该国进行战略性措施,以解救涂炭的生灵。

该国有n个城市,这些城市以铁路相连。任意两个城市都可以通过铁路直接或者间接到达。

uim发现有些铁路被毁坏之后,某两个城市无法互相通过铁路到达。这样的铁路就被称为key road。

uim为了尽快使该国的物流系统瘫痪,希望炸毁铁路,以达到存在某两个城市无法互相通过铁路到达的效果。

然而,只有一发炮弹(美国国会不给钱了)。所以,他能轰炸哪一条铁路呢?

输入输出格式

输入格式:

第一行n,m(1<=n<=150, 1<=m<=5000),分别表示有n个城市,总共m条铁路。

以下m行,每行两个整数a, b,表示城市a和城市b之间有铁路直接连接。

输出格式:

输出有若干行。

每行包含两个数字a,b(a<b),表示<a,b>是key road。

请注意:输出时,所有的数对<a,b>必须按照a从小到大排序输出;如果a相同,则根据b从小到大排序。

输入输出样例

输入样例#1:

6 6
1 2
2 3
2 4
3 5
4 5
5 6

输出样例#1:

1 2
5 6

solution:

本题所求的是无向图中的所有割边。

当然就是选tarjan(双联通分量)了。如果不懂自行百度吧QwQ

tarjan有个性质:如果low[x]>dfn[fa],那么x如果不经过fa就不能访问到更小的边,那么边fa->x就是割边。

代码:

#include<bits/stdc++.h>
#pragma GCC optimize(3)
namespace ZDY{
    #define res register
    #define ri res int
    #define ll long long
    #define db double
    #define sht short
    #define il inline
    #define MB template <class T>
    #define Fur(i,x,y) for(ri i=x;i<=y;i++)
    #define fur(i,x,y) for(i=x;i<=y;i++)
    #define Fdr(i,x,y) for(ri i=x;i>=y;i--)
    #define in2(x,y) in(x),in(y)
    #define in3(x,y,z) in2(x,y),in(z)
    #define in4(a,b,c,d) in2(a,b);in2(c,d)
    #define outn(x) out(x),pc('\n')
    #define clr(x,y) memset(x,y,sizeof(x))
    #define cpy(x,y) memcpy(x,y,sizeof(x))
    #define fl(i,x) for(ri i=head[x],to;to=e[i].to,i;i=e[i].nxt)
    #define inf 2147483630
    #define fin(s) freopen(s".in","r",stdin)
    #define fout(s) freopen(s".out","w",stdin)
    #define gt io.gc()
    #define l2(n) (log(n)/log(2))
    MB il T ABS(T x){return x>0?x:-x;}
    MB il T MAX(T x,T y){return x>y?x:y;}
    MB il T MIN(T x,T y){return x<y?x:y;}
    MB il T GCD(T x,T y){return y?GCD(y,x%y):x;}
    MB il void SWAP(T x,T y){T t=x;y=t;x=y;}
}using namespace ZDY;using namespace std;

class IO{
   #define fok (ch!=EOF)
   #define sep (ch==' '||ch=='\n'||ch=='\t')
   #define dsep !isdigit(ch)
   #define neq(a,b) ((a)-(b)>1e-6)
   char rbuf[1<<20],wbuf[1<<20],b,*p1,*p2;
   int rs,ws,S;
   public:
       IO():p1(rbuf),p2(wbuf),S(1000000),rs(1000000),ws(-1),b(1){}
       ~IO(){fwrite(wbuf,1,ws+1,stdout);}
       il char gc(){return rs==S&&(p1=rbuf,rs=-1,(S=fread(rbuf,1,S+1,stdin)-1)==-1)?(b=0,EOF):(++rs,*p1++);}
       il void pc(int x){ws==1000000&&(p2=wbuf,ws=-1,fwrite(wbuf,1,1000001,stdout)),++ws,*p2++=x;}
       il void puts(const char str[]){fwrite(wbuf,1,ws+1,stdout)?(ws=-1):0,fwrite(str,1,strlen(str),stdout);}
       il void gl(string& s){for(res char ch;(ch=gc())!='\n'&&fok;)s+=ch;}
       il IO& operator>>(int& x){x=0;res char f=0,ch=gc();while(dsep&&fok)f|=(ch=='-'),ch=gc();while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=gc();return x=f?-x:x,*this;}
       il IO& operator>>(ll& x){x=0;res char f=0,ch=gc();while(dsep&&fok)f|=(ch=='-'),ch=gc();while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=gc();return x=f?-x:x,*this;}
       il IO& operator>>(char& ch){return ch=gc(),*this;}
       il IO& operator>>(string& s){res char ch=gc();while(sep&&fok)ch=gc();while(!sep&&fok)s+=ch,ch=gc();return *this;}
       il IO& operator>>(double& x){x=0;res char f=0,ch=gc();double d=0.1;while(dsep&&fok)f|=(ch=='-'),ch=gc();while(isdigit(ch))x=x*10+(ch^48),ch=gc();if(ch=='.')while(isdigit(ch=gc()))x+=d*(ch^48),d*=0.1;return x=f?-x:x,*this;}
       il IO& operator<<(int x){res char ch[10],i=-1;!x?(pc('0'),0):0,x<0?(pc('-'),x=-x):0;while(x)ch[++i]=x%10+48,x/=10;while(~i)pc(ch[i]),--i;return *this;}
       il IO& operator<<(ll x){res char ch[20],i=-1;!x?(pc('0'),0):0,x<0?(pc('-'),x=-x):0;while(x)ch[++i]=x%10+48,x/=10;while(~i)pc(ch[i]),--i;return *this;}
       il IO& operator<<(char ch){return pc(ch),*this;}
       il IO& operator<<(char str[]){return puts(str),*this;}
       il IO& operator<<(double x){int y=int(x);*this<<y;x-=y;while(neq(x,int(x)))x*=10;x?*this<<'.'<<int(x):0;return *this;}
       il operator bool(){return b;}
}io;

#define N 151
int n,m,sz,cnt,dfn[N],low[N],head[N],t;
struct edge{int to,nxt;}e[10001],a[10001];
il void add(int x,int y){e[++cnt].to=y;e[cnt].nxt=head[x];head[x]=cnt;}
il bool cmp(edge x,edge y){if(x.to==y.to)return x.nxt<y.nxt;return x.to<y.to;}
il void tarjan(int x,int f){
    dfn[x]=low[x]=++sz;
    fl(i,x)
    if(to!=f){
        if(!dfn[to]){
            tarjan(to,x);
            if(dfn[x]<low[to])a[++t]=(edge){x,to};
            low[x]=MIN(low[x],low[to]);
        }
        else low[x]=MIN(low[x],dfn[to]);
    }
}
int main(){
    io>>n>>m;
    int x,y;
    Fur(i,1,m)io>>x>>y,add(x,y),add(y,x);
    Fur(i,1,n)if(!dfn[i])tarjan(i,-1);
    sort(a+1,a+t+1,cmp);
    Fur(i,1,t)io<<a[i].to<<' '<<a[i].nxt<<'\n';
}

猜你喜欢

转载自www.cnblogs.com/mimiorz/p/9758088.html