zoukankan      html  css  js  c++  java
  • zoj 1588(桥)

    寻找割边又叫桥。 和找割点的很类似

    Burning Bridges

    Time Limit: 5 Seconds      Memory Limit: 32768 KB

    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 
    

    Author: Andrew Stankevich
    Source: Andrew Stankevich's Contest #5

     

    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    #include <iostream>
    using namespace std;
    
    #define N 10100
    #define M 100100
    #define INF 0x3ffffff
    
    struct node
    {
        int to,next,id;
    }edge[2*M];
    
    int cnt,pre[N];
    int n,m;
    int low[N],mark[M];
    int save[M];
    int num;
    
    void add_edge(int u,int v,int w)
    {
        edge[cnt].to=v;
        edge[cnt].id=w;
        edge[cnt].next=pre[u];
        pre[u]=cnt++;
    }
    
    
    void dfs(int s,int cnt)
    {
        low[s]=cnt;
        int mi=cnt;
        for(int p=pre[s];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            int id=edge[p].id;
            if(mark[ id ]==1) continue; // 一条边只能用一次
            if(low[v]==-1) 
            {
                mark[id]=1;
                dfs(v,cnt+1);
                if(low[v]>cnt)
                {
                    save[num++]=edge[p].id;
                }
            }
            mi=min(mi,low[v]);
        }
        low[s]=mi;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        int flag=0;
        while(T--)
        {
            cnt=0;
            num=0;
            if(flag) 
            {
                printf("\n");
            }
            flag=1;
            memset(pre,-1,sizeof(pre));
            scanf("%d%d",&n,&m);
            for(int i=1;i<=m;i++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                add_edge(x,y,i);
                add_edge(y,x,i);
            }
            memset(low,-1,sizeof(low));
            memset(mark,0,sizeof(mark));
            dfs(1,cnt);
            sort(save,save+num);
            printf("%d\n",num);
            if(num!=0)
            {
                for(int i=0;i<num-1;i++)
                    printf("%d ",save[i]);
                printf("%d",save[num-1]);
                printf("\n");
            }
            //printf("\n");
        }
        return 0;
    }
  • 相关阅读:
    2.3、css颜色表示法
    2.2、css文本设置
    2.1、css基本语法及页面引用
    1.10、html内嵌框架
    1.9、html表单
    1.8、html表格
    1.7、html列表
    1.6、html链接
    1.5、html图像、绝对路径和相对路径
    1.4、html块、含样式的标签
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3079708.html
Copyright © 2011-2022 走看看