【UOJ 271】炸铁路

【题目描述】:

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

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

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

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

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

【输入描述】:

第一行n,m,分别表示有n个城市,总共m条铁路。

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

【输出描述】:

输出有若干行。

每行包含两个数字a,b(不保证a是key road。

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

【样例输入】:

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

【样例输出】:

1 2
5 6

【时间限制、数据范围及描述】:

时间:1s 空间:128M

1<=n<=10000, 1<=m<=100000

题解;Tarjan求桥嘤嘤嘤~

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=200003;
int dfn[N],low[N],cnt,head[N];
int cTime;
struct node{
    int to,next;
}e[N];
int n,m,x,y,tmp;

struct look{
    int yao;
    int chen;//琛琛 
}jjj[N];

void add(int x,int y){
    e[++cnt].to=y;
    e[cnt].next=head[x];
    head[x]=cnt;
}

void tarjan(int u,int fa){
    dfn[u]=low[u]=++cTime;
    bool flag=1;
    for(int i=head[u];i;i=e[i].next){
        int v=e[i].to;
        if(v==fa ) { flag=0; continue; }
        if(!dfn[v]){
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(low[v]>dfn[u]) {
                
                jjj[++tmp].yao=min(v,u);
                jjj[tmp].chen=max(u,v);
            }
        }
        else low[u]=min(low[u],dfn[v]); 
    }
} 

bool cmp(look xx,look yy){
    if(xx.yao==yy.yao) return xx.chen<yy.chen;
    return xx.yao<yy.yao; 
} 

int main(){

    scanf("%d %d",&n,&m);
    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);//cout<<tmp;
    sort(jjj+1,jjj+1+tmp,cmp);
    
    for(int i=1;i<=tmp;i++)
        printf("%d %d
",jjj[i].yao,jjj[i].chen);
    return 0;
}
原文地址:https://www.cnblogs.com/wuhu-JJJ/p/13539874.html