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. Zznu 1913: yifan and matrix (多路归并)

    题目链接: 1913: yifan and matrix 题目描述: 有一个n*n的矩阵,在每一行取出一个数,可以得到n个数的和,问前n小的和分别是多少? 解题思路: 对于两个数组a[n],b[n], ...

  2. Xors on Segments Codeforces - 620F

    http://codeforces.com/problemset/problem/620/F 此题是莫队,但是不能用一般的莫队做,因为是最优化问题,没有办法在删除元素的时候维护答案. 这题的方法(好像 ...

  3. (转)C语言运算符优先级 详细列表

    C语言运算符优先级 详细列表 文章转自:Slyar Home 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右   () 圆括号 (表达式)/函数 ...

  4. 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  5. Oracle报错:“ORA-18008: 无法找到 OUTLN 方案 ”的解决方案

    Oracle报错:“ORA-18008: 无法找到 OUTLN 方案 ”的解决方案   2.修改replication_dependency_tracking参数 SQL> alter syst ...

  6. 在面试官问你BS和CS区别的时候如何回答??

    这是我下来整理好的,如果哪里不全,望大家多多指教 C/S是Client/Server的缩写.服务器通常采用高性能的PC.工作站或小型机,并采用大型数据库系统,如Oracle.Sybase.Inform ...

  7. vue cli 3 打包过大问题

    vue cli 3 打包命令 npm run build,这种情况下的打包可以通过设置 vue.config.js里面的 productionSourceMap: false. 如果是自己设置的打包环 ...

  8. java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to

    在做android解析服务器传来的json时遇到的错误. 服务器传来的数据格式 [{"," id":"7ef6815938394fce88a5873312b66 ...

  9. .NET 原理之 ViewState

    1.从MSDN中我们可以知道一个页面生命周期大约可分为为:页请求.开始.初始化.加载.验证.回发事件处理.呈现.卸载这几个阶段.       HttpHandler是无状态的,aspx是高级的Http ...

  10. phpmyadmin在linux下通过sock安装教程

    当初是按照 http://www.cnblogs.com/freeweb/p/5262852.html 地址参考安装,因为疏忽,未考虑到版本差异带来的影响(自身安装的是最新版 phpMyAdmin-4 ...