zoukankan      html  css  js  c++  java
  • 炸铁路

    【题目描述】:

    因为某国被某红色政权残酷的高压暴力统治。美国派出将军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

    闲话:

    被红色政权暴力统治。。。咳咳。。。

    分析:

    本题题意就是求一条边,该边链接两个强联通分量,那么显然不就是求桥么。。。所以本题事实上就是模板题QWQ。

    CODE:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cmath>
     5 using namespace std;
     6 const int M=100005;
     7 int n,m;
     8 int head[M],next[M],to[M];
     9 int tot;
    10 struct node{
    11     int x;int y;
    12 }ansl[M];
    13 int cnt;
    14 void add(int u,int v){
    15     next[++tot]=head[u];
    16     head[u]=tot;
    17     to[tot]=v;
    18     return ;
    19 }
    20 inline int get(){
    21     char c=getchar();
    22     int res=0;
    23     while (c>'9'||c<'0') c=getchar();
    24     while (c<='9'&&c>='0') {
    25         res=(res<<3)+(res<<1)+c-'0';
    26         c=getchar();
    27     }
    28     return res;
    29 }
    30 int dfn[M],low[M];  
    31 int ind;  
    32 void tarjan(int u,int fa){ 
    33     //cout<<u<<" "<<fa<<endl;
    34     dfn[u]=low[u]=++ind;      
    35     bool flag=1; 
    36     for(int i=head[u];i;i=next[i]){  
    37        int v=to[i]; 
    38        if(v==fa&&flag){flag=0;continue;}
    39        if(!dfn[v]){ 
    40            tarjan(v,u); 
    41            low[u]=min(low[u],low[v]);  
    42            if(low[v]>dfn[u]){
    43                ansl[++cnt].x=u,ansl[cnt].y=v;
    44                if (u>v) swap(ansl[cnt].x,ansl[cnt].y);
    45             }
    46         } 
    47         else low[u]=min(low[u],dfn[v]); 
    48     }
    49     return ;
    50 } 
    51 bool cmp(node xx,node yy){
    52     return xx.x<yy.x;
    53     return xx.y<yy.y;
    54 }
    55 int main(){
    56     n=get(),m=get();
    57     for (int i=1;i<=m;i++) {
    58         int a=get(),b=get();
    59         add(a,b);
    60         add(b,a);
    61     }
    62     for (int i=1;i<=n;i++) 
    63         if (!dfn[i]) tarjan(i,0);
    64     sort(ansl+1,ansl+cnt+1,cmp);
    65     for (int i=1;i<=cnt;i++) cout<<ansl[i].x<<" "<<ansl[i].y<<endl;
    66     return 0;
    67 }
  • 相关阅读:
    unicode下各种类型转换CString、string
    单元测试简介
    linux系统目录详解
    Python 几个重要的内置函数
    python join split
    将博客搬至CSDN
    COCOS2DX3.0的3种触摸响应机制
    [转] 千万别在最能吃苦的年纪选择安逸
    明白什么是“五险一金”
    MAC 安装更新 ANT
  • 原文地址:https://www.cnblogs.com/kanchuang/p/11156563.html
Copyright © 2011-2022 走看看