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决定烧毁所有的桥.这是个 ...
随机推荐
- Oracle10g修改数据库字符集
Oracle10g修改字符集记录: 版本:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production 参考 ...
- B/S和C/S示意图
B/S C/S
- Oracle报错:“ORA-18008: 无法找到 OUTLN 方案 ”的解决方案
Oracle报错:“ORA-18008: 无法找到 OUTLN 方案 ”的解决方案 2.修改replication_dependency_tracking参数 SQL> alter syst ...
- ambari-server启动报错500 status code received on GET method for API:/api/v1/stacks/HDP/versions/2.4/recommendations Error message : Server Error解决办法(图文详解)
问题详情 来源是,我在Ambari集群里,安装Hue. 给Ambari集群里安装可视化分析利器工具Hue步骤(图文详解 所遇到的这个问题. 然后,去ambari-server的log日志,查看,如下 ...
- mysql之修改字符编码
目录 统一修改字段编码 修改单个字段编码 修改表字符编码 统一修改字段编码: alter table `tablename` convert to character set utf8; 修改表字符编 ...
- js 将XML字符串解析成XML文档 --- attribute construct error--- 空白字符与空格问题
最近在做xml在线编辑器,遇到一个字符串解析成xml文档的问题,记录一下. 原始xml内容读取自xml文档 <label class="test" id="labe ...
- 安卓(Android )软键盘的控制(显示和隐藏)
Activity 启动时软键盘默认状态 在清单文件(manifest .xml)中可以通过在 Activity 标签中增加属性控制软键盘的默认状态: android:windowSoftInputMo ...
- 详解Android Activity启动模式
相关的基本概念: 1.任务栈(Task) 若干个Activity的集合的栈表示一个Task. 栈不仅仅只包含自身程序的Activity,它也可以跨应用包含其他应用的Activity,这样有利于 ...
- Python学习 Day 9 property 多重继承 Mixin
在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 为了限制score的范围,可以通过一 ...
- java将字段映射成另一个字段,关于 接口传参 字段不对应转换
在接口开发中我们经常会遇到一个问题,打个比方,我们的实体类A中有两个字段user和pwd但是接口中需要username和password这怎么办呢,我想到了两种方法:1.新创建一个实体类B或者new一 ...