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决定烧毁所有的桥.这是个 ...
随机推荐
- 暑期训练狂刷系列——Lightoj 1084 - Winter bfs
题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1084 题目大意: 有n个点在一条以零为起点的坐标轴上,每个点最多可以移动k, ...
- 暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table
题目传送门 /* 题意:求最大矩形(全0)的面积 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 详细解释:http://www ...
- 419 Battleships in a Board 甲板上的战舰
给定一个二维的甲板, 请计算其中有多少艘战舰. 战舰用 'X'表示,空位用 '.'表示. 你需要遵守以下规则: 给你一个有效的甲板,仅由战舰或者空位组成. 战舰只能水平或者垂直放置.换句话 ...
- [转]Keyword Reference (F#)
Visual F# Development Portal http://msdn.microsoft.com/en-us/library/vstudio/ff730280.aspx 本文转自:http ...
- Web前端深思
WEB视图层技术从最初刀耕火种的时代到如今技术框架丛生,其中的感受只有经历过才知道.但到目前为止前端领域还只是整个IT行业比较边缘化的分支,因为目前的前端coder大多都还停留在视图层的处理上,利用前 ...
- 文件及文件的操作-读、写、追加的t和b模式
1.什么是文件? 文件是操作系统为用户或应用程序提供的一个读写硬盘的虚拟单位. 文件的操作核心:读和写 对文件进行读写操作就是向操作系统发出指令,操作系统将用户或者应用程序对文件的读写操作转换为具体的 ...
- [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串
题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...
- git ---查看工作状态和历史提交
1.git查看状态 -git status 2.版权声明 版权声明:新建一个 LICENSE.txt 文件 开源协议:MIT //开源许可里面的最宽松的一个协议,别人可以随便用你的代码,但 ...
- 掌握Spark机器学习库-07-最小二乘法
1)最小化残差平方和 2)原理,推导过程 3)例子
- YOLO模型对图片中车辆的识别比对
1,模型对比结果 ² 标准Yolo v3模型 ² 标准Yolo v3 tiny模型 ² 标准Yolo v2 tiny模型 ² 用户训练yolo ...