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

题意:n个岛屿m条桥 其中有重边,现在要烧掉没用的桥(保留的是双连通分量的割边)让你输出割边的总数以及 编号 题解:求出桥 输出(注意对重边的处理)
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#define MAX 200100
using namespace std;
struct node
{
int beg,end,chon,next;
int id;
}edge[MAX];
int dfsclock;
int n,m,ans;
int low[MAX],dfn[MAX];
int head[MAX],mark;
int bridge[MAX],sum;
void init()
{
ans=0;
sum=0;
memset(bridge,0,sizeof(bridge));
memset(head,-1,sizeof(head));
}
void add(int u,int v,int id)
{
int i;
for(i=head[u];i!=-1;i=edge[i].next) //处理重边
{
if(edge[i].end==v)
break;
}
if(i!=-1)//i!=-1证明未处理完所有的边(即发现了重边)
edge[i].chon=1;//标记重边
else
{
edge[ans].beg=u;
edge[ans].end=v;
edge[ans].chon=0;
edge[ans].id=id;
edge[ans].next=head[u];
head[u]=ans++;
}
}
void getmap()
{
int a,b;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
add(a,b,i);
add(b,a,i);
}
}
void tarjan(int u,int fa)
{
int v;
dfn[u]=low[u]=++dfsclock;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].end;
if(v==fa)
continue;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v]&&edge[i].chon==0)//有桥并且不是重边
{
bridge[sum++]=edge[i].id;
}
}
else
low[u]=min(low[u],dfn[v]);
}
}
void find()
{
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
dfsclock=0;
tarjan(1,-1);
}
void solve()
{
printf("%d\n",sum);
if(sum==0)
return ;
sort(bridge,bridge+sum);
for(int i=0;i<sum;i++)
{
if(!i)
printf("%d",bridge[i]);
else
printf(" %d",bridge[i]);
}
printf("\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
getmap();
find();
solve();
if(t)
printf("\n")//注意输出格式
}
return 0;
}

  

zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】的更多相关文章

  1. ZOJ 2588 Burning Bridges(求桥的数量,邻接表)

    题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2588 Burning Bridges Time Limit: 5 ...

  2. 【求无向图的桥,有重边】ZOJ - 2588 Burning Bridges

    模板题——求割点与桥 题意,要使一个无向图不连通,输出必定要删掉的边的数量及其编号.求桥的裸题,可拿来练手. 套模板的时候注意本题两节点之间可能有多条边,而模板是不判重边的,所以直接套模板的话,会将重 ...

  3. ZOJ 2588 Burning Bridges(求含重边的无向连通图的割边) - from lanshui_Yang

    Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little country ...

  4. ZOJ 2588 Burning Bridges(无向连通图求割边)

    题目地址:ZOJ 2588 由于数组开小了而TLE了..这题就是一个求无向连通图最小割边.仅仅要推断dfn[u]是否<low[v],由于low指的当前所能回到的祖先的最小标号,增加low[v]大 ...

  5. ZOJ Problem - 2588 Burning Bridges tarjan算法求割边

    题意:求无向图的割边. 思路:tarjan算法求割边,访问到一个点,如果这个点的low值比它的dfn值大,它就是割边,直接ans++(之所以可以直接ans++,是因为他与割点不同,每条边只访问了一遍) ...

  6. zoj——2588 Burning Bridges

    Burning Bridges Time Limit: 5 Seconds      Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...

  7. ZOJ 2588 Burning Bridges (tarjan求割边)

    题目链接 题意 : N个点M条边,允许有重边,让你求出割边的数目以及每条割边的编号(编号是输入顺序从1到M). 思路 :tarjan求割边,对于除重边以为中生成树的边(u,v),若满足dfn[u] & ...

  8. zoj 2588 Burning Bridges(割边/桥)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1588 题意:Ferry王国有n个岛,m座桥,每个岛都可以互达,现在要 ...

  9. zoj 2588 Burning Bridges

    题目描述:Ferry王国是一个漂亮的岛国,一共有N个岛国.M座桥,通过这些桥可以从每个小岛都能到达任何一个小岛.很不幸的是,最近Ferry王国被Jordan征服了.Jordan决定烧毁所有的桥.这是个 ...

随机推荐

  1. 学无止境,学习AJAX(二)

    POST 请求 一个简单 POST 请求: xmlhttp.open("POST","demo_post.asp",true); xmlhttp.send(); ...

  2. 用JAVA实现数字水印(可见)

    数字水印有可见不可见之分,可见的比如课件上印有学校校徽,微博发图片会水印上上传者的信息及微博logo等. 用java实现可见的数字水印,草人主要是用到了java.awt包中的AlphaComposit ...

  3. Automotive Security的一些资料和心得(6):AUTOSAR

    1.1 Introduction AUTOSAR(汽车开放系统架构)是一个开放的,标准化的汽车软件架构,由汽车制造商,供应商和开发工具共同开发.它联合了汽车OEM ,供应商和开发工具供应商,其目标是创 ...

  4. 回首Java(始)

    自接触Java开始,已然5载春秋. 如今每每在深入学习过程中,时刻感到力不从心. It's time!该拾起J2SE基石. 平地高楼,日积跬步.根底不坚实,如今才寸步维艰. 回头再温故.

  5. linux运维面试题

    一.有文件file1 1.查询file1 里面空行的所在行号 grep -n "^#" file1 or awk ‗{if($0~/^$/)print NR}‘ file or g ...

  6. android usb Host模式下与usb Hid 设备的通信

    做android 与USB HID设备的通信有段时间了,总结一下遇到的问题和解决方法: 1,第一次遇到的问题:android 版本低不支持usb hid, 被要求做相关项目的时候,就从mUsbMana ...

  7. Android 应用自动更新功能的代码

    由于Android项目开源所致,市面上出现了N多安卓软件市场.为了让我们开发的软件有更多的用户使用,我们需要向N多市场发布,软件升级后,我们也必须到安卓市场上进行更新,给我们增加了工作量.因此我们有必 ...

  8. FastJSON学习

    这几天在用FastJSON,发现需要测试一些关键点,包括: 1.是否支持内部类:测试结果是支持,但是需要设置为静态类(static) 2.是否支持继承的自动序列化及反序列化:测试结果是支持 3.缺字段 ...

  9. hadoop2.2编程:用ruby跑hadoop的完整实例

    Becareful!  All nodes include  need to install ruby! #!/usr/bin/ruby # Ruby code for map.rb ARGF.eac ...

  10. Oracle MySQL Server 拒绝服务漏洞

    漏洞名称: Oracle MySQL Server 拒绝服务漏洞 CNNVD编号: CNNVD-201401-316 发布时间: 2014-01-22 更新时间: 2014-01-22 危害等级: 中 ...