xtu summer individual 5 E - Burning Bridges
Burning Bridges
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
解题:割边、桥。。。
#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的更多相关文章
- ZOJ 2588 Burning Bridges(求含重边的无向连通图的割边) - from lanshui_Yang
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little country ...
- zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...
- zoj——2588 Burning Bridges
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...
- ZOJ 2588 Burning Bridges(求桥的数量,邻接表)
题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2588 Burning Bridges Time Limit: 5 ...
- Burning Bridges 求tarjan求割边
Burning Bridges 给出含有n个顶点和m条边的连通无向图,求出所有割边的序号. 1 #include <cstdio> 2 #include <cstring> 3 ...
- ZOJ2588 Burning Bridges(割边模板)
题目要输出一个无向图的所有割边.用Tarjan算法: 一遍DFS,构造出一颗深度优先生成树,在原无向图中边分成了两种:树边(生成树上的边)和反祖边(非生成树上的边). 顺便求出每个结点的DFS序dfn ...
- ZOJ 2588 Burning Bridges (tarjan求割边)
题目链接 题意 : N个点M条边,允许有重边,让你求出割边的数目以及每条割边的编号(编号是输入顺序从1到M). 思路 :tarjan求割边,对于除重边以为中生成树的边(u,v),若满足dfn[u] & ...
- 【求无向图的桥,有重边】ZOJ - 2588 Burning Bridges
模板题——求割点与桥 题意,要使一个无向图不连通,输出必定要删掉的边的数量及其编号.求桥的裸题,可拿来练手. 套模板的时候注意本题两节点之间可能有多条边,而模板是不判重边的,所以直接套模板的话,会将重 ...
- zoj 2588 Burning Bridges
题目描述:Ferry王国是一个漂亮的岛国,一共有N个岛国.M座桥,通过这些桥可以从每个小岛都能到达任何一个小岛.很不幸的是,最近Ferry王国被Jordan征服了.Jordan决定烧毁所有的桥.这是个 ...
随机推荐
- 计算机视觉-SIFT特征匹配进行目标转换
Lowe将SIFT算法分解为如下四步: 1. 尺度空间极值检测:搜索所有尺度上的图像位置.通过高斯微分函数来识别潜在的对于尺度和旋转不变的兴趣点. 关键点定位:在每个候选的位置上,通过一个拟合精细的模 ...
- 题解报告:hdu 4704 Sum(扩展欧拉定理)
Problem Description Sample Input 2 Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input ...
- 鼠标适配器Adapter
先来看看概念: 现在我们要写一个这样的东西,就是一个窗口,然后鼠标点一下就有一个小圆点,like this: 来我们来看代码: import java.awt.*; import java.util. ...
- Lync客户端证书安装
安装完Lync客户端后,运行时Lync客户端时,报出如下错误: [原因解析] Lync客户端没有正确安装CA证书链. [解决办法] 第一种方法:将计算机加入域. 第二种方法:不加入域的处理方法: 1. ...
- D. Mahmoud and a Dictionary 种类并查集
http://codeforces.com/contest/766/problem/D 所谓种类并查集,题型一般如下:给定一些基本信息给你,然后又给出一些信息,要求你判断是真是假.例如给出a和b支持不 ...
- 腾讯云COS对象存储的简单使用
叮当哥之前买了一年的腾讯云服务器,昨日偶然发现腾讯云送了叮当哥半年的cos对象存储服务器,于是就撸起袖子传了几张珍藏的高清大图上去,现将其上传的简单使用步骤总结一波(其它操作参加官方SDK文档API) ...
- 51全志R58平台Android4.4下Camera的HAL层修改
51全志R58平台Android4.4下Camera的HAL层修改 2018/11/7 15:20 版本:V1.0 开发板:SC5806 1.系统编译: (略) 2.全志R58平台Android4.4 ...
- 掌握Spark机器学习库-01
第1章 初识机器学习 在本章中将带领大家概要了解什么是机器学习.机器学习在当前有哪些典型应用.机器学习的核心思想.常用的框架有哪些,该如何进行选型等相关问题. 1-1 导学 1-2 机器学习概述 1- ...
- github——团队合作
- [Windows Server 2008] IIS配置伪静态方法(Web.config模式的IIS rewrite)
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装伪静态(w ...