P1656 炸铁路

https://www.luogu.org/problemnew/show/P1656

#include 
#include 
#include 
#include 
#include 
using namespace std;

const int MAXN=10005;
const int MAXM=200005;
int n,m,p[MAXN],ec,ans;
struct cyq{
    int v,next;
}edge[MAXM];
struct qyc{
    int aa,bb;
}rpyy[MAXM];

void add(int u, int v){	 
    edge[++ec].v = v;
    edge[ec].next = p[u];
    p[u] = ec;
}
int dfn[MAXN],cTime,low[MAXN];	
int gid[MAXN],gc,cnt,num[MAXN],de[MAXN];

bool cmp(qyc uuu,qyc vvv){
    if(uuu.aa==vvv.aa) return uuu.bb<vvv.bb;
    return uuu.aa<vvv.aa;	
}

void Tarjan(int u,int fa){
    dfn[u]=low[u]=++cTime;
    for(int i=p[u];i;i=edge[i].next){	
        int v=edge[i].v;
        if(v!=fa && dfn[v]) low[u]=min(low[u],dfn[v]);
        if(!dfn[v]){
            Tarjan(v,u);
            if(low[v]>dfn[u]){
                rpyy[++cnt].aa=u,rpyy[cnt].bb=v;
                if(u>v) swap(rpyy[cnt].aa,rpyy[cnt].bb);
            } low[u]=min(low[u],low[v]);
        }
    }
}

int main(){              
    scanf("%d%d",&n,&m);int x,y;
    for(int i=1;i<=m;i++){scanf("%d%d",&x,&y);add(x,y);add(y,x);}
    for(int i=1;i<=n;i++)
        if(!dfn[i]) Tarjan(i,0);
    sort(rpyy+1,rpyy+1+cnt,cmp);
    for(int i=1;i<=cnt;i++)
        printf("%d %d\n",rpyy[i].aa,rpyy[i].bb);
    return 0;
}

你可能感兴趣的:(Tarjan)