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

解题:割边、桥。。。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,num,id;
arc():num(){}
};
vector<arc>g[maxn];
int low[maxn],dfn[maxn],ans[maxn*],id,_index;
int tot,n,m;
void addArc(int u,int v){
bool flag = true;
int i,j;
for(i = ; i < g[u].size(); i++){
if(v == g[u][i].to){
flag = false;
g[u][i].num++;
for(j = ; j < g[v].size(); j++){
if(g[v][j].to == u){
g[v][j].num++;
break;
}
}
id++;
break;
}
}
if(flag){
arc temp;
temp.num = ;
temp.id = id++;
temp.to = v;
g[u].push_back(temp);
temp.to = u;
g[v].push_back(temp);
}
}
void tarjan(int u,int fa){
int v,i,j;
dfn[u] = low[u] = _index++;
for(i = ; i < g[u].size(); i++){
v = g[u][i].to;
if(dfn[v] == -){
tarjan(v,u);
low[u] = min(low[u],low[v]);
}else if(v != fa) low[u] = min(low[u],dfn[v]);
}
if(dfn[u] == low[u]){
v = u;
u = fa;
for(i = ; i < g[u].size(); i++){
if(v == g[u][i].to){
if(g[u][i].num == ) ans[tot++] = g[u][i].id;
break;
}
}
}
}
int main(){
int t,i,j,u,v;
scanf("%d",&t);
while(t--){
tot = ;
id = _index = ;
scanf("%d %d",&n,&m);
for(i = ; i <= n; i++){
dfn[i] = low[i] = -;
g[i].clear();
}
for(i = ; i < m; i++){
scanf("%d%d",&u,&v);
addArc(u,v);
}
dfn[] = low[] = _index++;
tarjan(,);
printf("%d\n",tot);
if(tot){
sort(ans,ans+tot);
printf("%d",ans[]);
for(i = ; i < tot; i++)
printf(" %d",ans[i]);
puts("");
}
if(t) puts("");
}
return ;
}

比较好理解的

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,id,next;
arc(int x = ,int y = ,int z = -){
to = x;
id = y;
next = z;
}
};
int head[maxn],ans[maxn],num,tot;
int low[maxn],dfn[maxn],idx,n,m;
arc e[];
void init(){
for(int i = ; i <= n; i++){
head[i] = -;
dfn[i] = low[i] = ;
}
idx = num = tot = ;
}
void add(int u,int v,int id){
e[tot] = arc(v,id,head[u]);
head[u] = tot++;
e[tot] = arc(u,id,head[v]);
head[v] = tot++;
}
void tarjan(int u,int fa){
dfn[u] = low[u] = ++idx;
bool flag = true;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa && flag){
flag = false;
continue;
}
if(!dfn[e[i].to]){
tarjan(e[i].to,u);
low[u] = min(low[u],low[e[i].to]);
if(low[e[i].to] > dfn[u]) ans[num++] = e[i].id;
}else low[u] = min(low[u],dfn[e[i].to]);
}
}
int main(){
int t,u,v;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
init();
for(int i = ; i <= m; i++){
scanf("%d %d",&u,&v);
add(u,v,i);
}
for(int i = ; i <= n; i++)
if(!dfn[i]) tarjan(i,-);
sort(ans,ans+num);
printf("%d\n",num);
if(num){
for(int i = ; i < num; i++)
printf("%d%c",ans[i],i + == num?'\n':' ');
}
if(t) puts("");
}
return ;
}

xtu summer individual 5 E - Burning Bridges的更多相关文章

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

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

  2. zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】

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

  3. zoj——2588 Burning Bridges

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

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

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

  5. Burning Bridges 求tarjan求割边

    Burning Bridges 给出含有n个顶点和m条边的连通无向图,求出所有割边的序号. 1 #include <cstdio> 2 #include <cstring> 3 ...

  6. ZOJ2588 Burning Bridges(割边模板)

    题目要输出一个无向图的所有割边.用Tarjan算法: 一遍DFS,构造出一颗深度优先生成树,在原无向图中边分成了两种:树边(生成树上的边)和反祖边(非生成树上的边). 顺便求出每个结点的DFS序dfn ...

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

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

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

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

  9. zoj 2588 Burning Bridges

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

随机推荐

  1. hdu 2063 过山车 (最大匹配 匈牙利算法模板)

    匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最 ...

  2. 转】Cassandra单集群实验2个节点

    原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/3/ 感谢! Cassandra单集群实验2个节点 前言 A ...

  3. 转】在Ubuntu中安装Cassandra

    原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/ 感谢! Posted: Mar 22, 2014 Tags: cas ...

  4. WPF学习07:MVVM 预备知识之数据绑定

    MVVM是一种模式,而WPF的数据绑定机制是一种WPF内建的功能集,两者是不相关的. 但是,借助WPF各种内建功能集,如数据绑定.命令.数据模板,我们可以高效的在WPF上实现MVVM.因此,我们需要对 ...

  5. AJPFX详解jsp的九大内置对象和四大作用域

    定义:可以不加声明就在JSP页面脚本(Java程序片和Java表达式)中使用的成员变量 JSP共有以下9种基本内置组件(可与ASP的6种内部组件相对应): 1.request对象(作用域) 客户端的请 ...

  6. serialize可以获取form表单里面的数值

    serialize属性 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  7. 短视频SDK在广电系统的解决方案

    锐动视频编辑SDK集手机视频拍摄和视频剪辑主要功能于一体,同时包含手机端视频配音配乐,字幕特效,滤镜,转场特效等各种功能,全方位满足开发者的需求,并可以快速植入到APP中. 手机端的摄像头录制和直播功 ...

  8. 【转】Android Activity/Fragment Lifecycle

    原文来自:http://stormzhang.github.io/android/2014/08/08/activity-fragment-lifecycle/ 说Activity和Fragment是 ...

  9. 有意思的String字符工具类

    对String的操作是Java攻城师必备的,一个优秀的攻城师是懒惰,他会把自己的一些常见的代码写成可提供拓展和复用的工具类或者工具库,这些是这些优秀工程师的法宝. 我就先从String这个基本操作开始 ...

  10. t470安装win7

    终于把win7安装好了,写了个文档 https://files.cnblogs.com/files/cookies9/t470%E5%AE%89%E8%A3%85win7%E6%96%B9%E6%B3 ...