zoukankan      html  css  js  c++  java
  • ZOJ 2588 Burning Bridges

    Burning Bridges
    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2588

    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 

    题意:求桥,可能有重边

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define N 10001
    using namespace std;
    int tot,front[N],to[N*20],nxt[N*20];
    int sum;
    int dfn[N],low[N];
    struct BRIDGE
    {
        int u,v,id;
        bool f;
    }e[N*10];
    void add(int u,int v)
    {
        to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
        to[++tot]=u; nxt[tot]=front[v]; front[v]=tot;
    }
    void tarjan(int now,int last)
    {
        dfn[now]=low[now]=++tot;
        for(int i=front[now];i;i=nxt[i])
        {
            if(i==(last^1)) continue;
            if(!dfn[to[i]])
            {
                tarjan(to[i],i);
                low[now]=min(low[now],low[to[i]]);
            }
            else low[now]=min(low[now],dfn[to[i]]);
        }
        if(low[now]>=dfn[now] && last) 
        {
            e[++sum].v=now,
            e[sum].u=to[last^1],
            e[sum].id=last>>1,
            e[sum].f=false;
        }
    }
    bool cmp(BRIDGE p,BRIDGE q)
    {
        return p.id<q.id;
    }
    int main()
    {
        int T;
        int u,v,n,m,dont,cnt;
        bool ok1;
        scanf("%d",&T);
        while(T--)
        {
            memset(front,0,sizeof(front));
            memset(dfn,0,sizeof(dfn));
            memset(low,0,sizeof(low));
            sum=0;
            scanf("%d%d",&n,&m);
            tot=1;
            while(m--)
            {
                scanf("%d%d",&u,&v);
                add(u,v);
            }
            tot=0;
            tarjan(1,0);
            dont=0;
            for(int i=1;i<=sum;i++)
            {
                ok1=true;
                for(int j=front[e[i].u];j && ok1;j=nxt[j])
                 if(to[j]==e[i].v && j>>1!=e[i].id) ok1=false;
                if(ok1) e[i].f=true;
            }
            sort(e+1,e+sum+1,cmp);
            printf("%d
    ",sum-dont);
            cnt=0;
            for(int i=1;i<=sum;i++) 
            {
                if(!e[i].f) continue;
                cnt++;
                if(cnt!=sum-dont) printf("%d ",e[i].id);
                else printf("%d
    ",e[i].id);
            }
            if(T) puts("");
        }
    }


  • 相关阅读:
    数据库事务隔离级别+Spring 声明性事务隔离级别
    (面试题)如何查找Oracle数据库中的重复记录
    Spring提供的线程池支持--百度文库
    (面试题)输出下列程序结果(考察字符串与其他类型+连接)
    (面试)有两个木桶,一个3斤,一个5斤,水无限,要怎么样得到精确地4斤水
    (面试)涉及到继承和类加载
    (面试题)用折半查找法在一组整形数组中查找某个数据
    (面试)写出下面switch语句的输出结果
    HTML 鼠标坐标和元素坐标
    HTML5 元素属性介绍
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/7387591.html
Copyright © 2011-2022 走看看