zoukankan      html  css  js  c++  java
  • xtu summer individual 5 E

    Burning Bridges

    Time Limit: 5000ms
    Memory Limit: 32768KB
    This problem will be judged on ZJU. Original ID: 2588
    64-bit integer IO format: %lld      Java class name: Main
     

    Ferry Kingdom is a nice little country located on N islands that are connected by M bridges. All bridges are very beautiful and are loved by everyone in the kingdom. Of course, the system of bridges is designed in such a way that one can get from any island to any other one.

    But recently the great sorrow has come to the kingdom. Ferry Kingdom was conquered by the armies of the great warrior Jordan and he has decided to burn all the bridges that connected the islands. This was a very cruel decision, but the wizards of Jordan have advised him no to do so, because after that his own armies would not be able to get from one island to another. So Jordan decided to burn as many bridges as possible so that is was still possible for his armies to get from any island to any other one.

    Now the poor people of Ferry Kingdom wonder what bridges will be burned. Of course, they cannot learn that, because the list of bridges to be burned is kept in great secret. However, one old man said that you can help them to find the set of bridges that certainly will not be burned.

    So they came to you and asked for help. Can you do that?


    Input

    The input contains multiple test cases. The first line of the input is a single integer T (1 <= T <= 20) which is the number of test cases. T test cases follow, each preceded by a single blank line.

    The first line of each case contains N and M - the number of islands and bridges in Ferry Kingdom respectively (2 <= N <= 10 000, 1 <= M <= 100 000). Next M lines contain two different integer numbers each and describe bridges. Note that there can be several bridges between a pair of islands.


    Output

    On the first line of each case print K - the number of bridges that will certainly not be burned. On the second line print K integers - the numbers of these bridges. Bridges are numbered starting from one, as they are given in the input.

    Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.


    Sample Input

    2
    
    6 7
    1 2
    2 3
    2 4
    5 4
    1 3
    4 5
    3 6
    
    10 16
    2 6
    3 7
    6 5
    5 9
    5 4
    1 2
    9 8
    6 4
    2 10
    3 8
    7 9
    1 4
    2 4
    10 5
    1 6
    6 10
    

    Sample Output

    2
    3 7
    
    1
    4 
    

     

    Source

    Author

    Andrew Stankevich

    解题:割边、桥。。。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <vector>
     6 #include <climits>
     7 #include <algorithm>
     8 #include <cmath>
     9 #define LL long long
    10 #define INF 0x3f3f3f3f
    11 using namespace std;
    12 const int maxn = 10010;
    13 struct arc{
    14     int to,num,id;
    15     arc():num(0){}
    16 };
    17 vector<arc>g[maxn];
    18 int low[maxn],dfn[maxn],ans[maxn*10],id,_index;
    19 int tot,n,m;
    20 void addArc(int u,int v){
    21     bool flag = true;
    22     int i,j;
    23     for(i = 0; i < g[u].size(); i++){
    24         if(v == g[u][i].to){
    25             flag = false;
    26             g[u][i].num++;
    27             for(j = 0; j < g[v].size(); j++){
    28                 if(g[v][j].to == u){
    29                     g[v][j].num++;
    30                     break;
    31                 }
    32             }
    33             id++;
    34             break;
    35         }
    36     }
    37     if(flag){
    38         arc temp;
    39         temp.num = 1;
    40         temp.id = id++;
    41         temp.to = v;
    42         g[u].push_back(temp);
    43         temp.to = u;
    44         g[v].push_back(temp);
    45     }
    46 }
    47 void tarjan(int u,int fa){
    48     int v,i,j;
    49     dfn[u] = low[u] = _index++;
    50     for(i = 0; i < g[u].size(); i++){
    51         v = g[u][i].to;
    52         if(dfn[v] == -1){
    53             tarjan(v,u);
    54             low[u] = min(low[u],low[v]);
    55         }else if(v != fa) low[u] = min(low[u],dfn[v]);
    56     }
    57     if(dfn[u] == low[u]){
    58         v = u;
    59         u = fa;
    60         for(i = 0; i < g[u].size(); i++){
    61             if(v == g[u][i].to){
    62                 if(g[u][i].num == 1) ans[tot++] = g[u][i].id;
    63                 break;
    64             }
    65         }
    66     }
    67 }
    68 int main(){
    69     int t,i,j,u,v;
    70     scanf("%d",&t);
    71     while(t--){
    72         tot = 0;
    73         id = _index = 1;
    74         scanf("%d %d",&n,&m);
    75         for(i = 0; i <= n; i++){
    76             dfn[i] = low[i] = -1;
    77             g[i].clear();
    78         }
    79         for(i = 0; i < m; i++){
    80             scanf("%d%d",&u,&v);
    81             addArc(u,v);
    82         }
    83         dfn[1] = low[1] = _index++;
    84         tarjan(1,1);
    85         printf("%d
    ",tot);
    86         if(tot){
    87             sort(ans,ans+tot);
    88             printf("%d",ans[0]);
    89             for(i = 1; i < tot; i++)
    90                 printf(" %d",ans[i]);
    91             puts("");
    92         }
    93         if(t) puts("");
    94     }
    95     return 0;
    96 }
    View Code

    比较好理解的

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define INF 0x3f3f3f3f
    15 using namespace std;
    16 const int maxn = 10010;
    17 struct arc{
    18     int to,id,next;
    19     arc(int x = 0,int y = 0,int z = -1){
    20         to = x;
    21         id = y;
    22         next = z;
    23     }
    24 };
    25 int head[maxn],ans[maxn],num,tot;
    26 int low[maxn],dfn[maxn],idx,n,m;
    27 arc e[200100];
    28 void init(){
    29     for(int i = 0; i <= n; i++){
    30         head[i] = -1;
    31         dfn[i] = low[i] = 0;
    32     }
    33     idx = num = tot = 0;
    34 }
    35 void add(int u,int v,int id){
    36     e[tot] = arc(v,id,head[u]);
    37     head[u] = tot++;
    38     e[tot] = arc(u,id,head[v]);
    39     head[v] = tot++;
    40 }
    41 void tarjan(int u,int fa){
    42     dfn[u] = low[u] = ++idx;
    43     bool flag = true;
    44     for(int i = head[u]; ~i; i = e[i].next){
    45         if(e[i].to == fa && flag){
    46             flag = false;
    47             continue;
    48         }
    49         if(!dfn[e[i].to]){
    50             tarjan(e[i].to,u);
    51             low[u] = min(low[u],low[e[i].to]);
    52             if(low[e[i].to] > dfn[u]) ans[num++] = e[i].id;
    53         }else low[u] = min(low[u],dfn[e[i].to]);
    54     }
    55 }
    56 int main(){
    57     int t,u,v;
    58     scanf("%d",&t);
    59     while(t--){
    60         scanf("%d %d",&n,&m);
    61         init();
    62         for(int i = 1; i <= m; i++){
    63             scanf("%d %d",&u,&v);
    64             add(u,v,i);
    65         }
    66         for(int i = 1; i <= n; i++)
    67             if(!dfn[i]) tarjan(i,-1);
    68         sort(ans,ans+num);
    69         printf("%d
    ",num);
    70         if(num){
    71             for(int i = 0; i < num; i++)
    72                 printf("%d%c",ans[i],i + 1 == num?'
    ':' ');
    73         }
    74         if(t) puts("");
    75     }
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    采购标准流程及底层分析
    ORACLE FORM ZA 常用子程序
    在R12中实现多OU编程
    FORM未找到数据的原因
    在Oracle的FORM中高亮显示鼠标点击或光标所在的行
    MPICH运行程序时出错之解决方法
    两个基于C++的MPI编辑例子
    面向对象PHP面向对象的特性
    PHP 数组遍历 foreach 语法结构
    php BC高精确度函数库
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3895626.html
Copyright © 2011-2022 走看看