Burning Bridges-ZOJ1588(割边求解)
Burning Bridges
Time Limit: 5 Seconds Memory Limit: 32768 KB
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
求解割边的方法和求解割点的方法是一样的,判断方法:
无向图中的一条边(u,v),当且仅当(u,v)是生成树的边,并且满足dfn[u]
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int Max = 101000;
//前向星存边
typedef struct Node
{
int v;
int num;
int sum;
int next;
}Line;
Line Li[Max*2];
int top;
int Head[Max];
// 标记数组 0 表示没有遍历 1表示已遍历 2表示遍历完其相连的节点
int vis[Max];
// 表示所能连接的最先遍历的顺序
int low[Max];
// 标记边是不是割边
bool flag[Max];
// 记录遍历的顺序
int dfn[Max];
int Num;
// 割边的数目
int Total;
//初始化
void init()
{
memset(Head,-1,sizeof(Head));
top = 0; Num = 0; Total = 0;
memset(flag,false,sizeof(flag));
memset(vis,0,sizeof(vis));
}
void AddEdge(int u,int v,int num)
{
for(int i=Head[u];i!=-1;i=Li[i].next)
{
if(Li[i].v==v)//判断是不是重边
{
Li[i].sum++;
return ;
}
}
Li[top].v=v; Li[top].num = num;
Li[top].sum = 1;
Li[top].next = Head[u];
Head[u]=top++;
}
void dfs(int u,int father)
{
dfn[u]=low[u]=Num++;
vis[u]=1;
for(int i=Head[u];i!=-1;i=Li[i].next)
{
if(Li[i].v!=father&&vis[Li[i].v]==1)//不能是父节点
{
low[u]=min(low[Li[i].v],low[u]);
}
if(vis[Li[i].v]==0)
{
dfs(Li[i].v,u);
low[u]=min(low[u],low[Li[i].v]);
if(low[Li[i].v]>dfn[u]&&Li[i].sum==1)//重边肯定不是割点
{
flag[Li[i].num]=true;
Total ++;
}
}
}
vis[u]=2;
}
int n,m;
int main()
{
int T;
int z=1;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
init();
int u,v;
for(int i=1;i<=m;i++)
{
scanf("%d %d",&u,&v);
AddEdge(u,v,i);
AddEdge(v,u,i);
}
dfs(1,0);
int ans = 0;
printf("%d\n",Total);
for(int i=1;i<=m;i++)
{
if(flag[i])
{
if(ans)
{
printf(" ");
}
else
{
ans = 1;
}
printf("%d",i);
}
}
if(Total)
{
printf("\n");
}
if(T)
{
printf("\n");
}
}
return 0;
}
Burning Bridges-ZOJ1588(割边求解)的更多相关文章
- ZOJ2588 Burning Bridges(割边模板)
题目要输出一个无向图的所有割边.用Tarjan算法: 一遍DFS,构造出一颗深度优先生成树,在原无向图中边分成了两种:树边(生成树上的边)和反祖边(非生成树上的边). 顺便求出每个结点的DFS序dfn ...
- ZOJ 2588 Burning Bridges(求含重边的无向连通图的割边) - from lanshui_Yang
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little country ...
- Burning Bridges 求tarjan求割边
Burning Bridges 给出含有n个顶点和m条边的连通无向图,求出所有割边的序号. 1 #include <cstdio> 2 #include <cstring> 3 ...
- zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...
- xtu summer individual 5 E - Burning Bridges
Burning Bridges Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...
- 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 ...
- ZOJ 2588 Burning Bridges (tarjan求割边)
题目链接 题意 : N个点M条边,允许有重边,让你求出割边的数目以及每条割边的编号(编号是输入顺序从1到M). 思路 :tarjan求割边,对于除重边以为中生成树的边(u,v),若满足dfn[u] & ...
- ZOJ Problem - 2588 Burning Bridges tarjan算法求割边
题意:求无向图的割边. 思路:tarjan算法求割边,访问到一个点,如果这个点的low值比它的dfn值大,它就是割边,直接ans++(之所以可以直接ans++,是因为他与割点不同,每条边只访问了一遍) ...
随机推荐
- 示例说明Oracle RMAN两种库增量备份的差别
1差异增量实验示例 1.1差异增量备份 为了演示增量备份的效果,我们在执行一次0级别的备份后,对数据库进行一些改变. 再执行一次1级别的差异增量备份: 执行完1级别的备份后再次对数据库进行更改: 再执 ...
- Learn ZYNQ (3)
移植android3.3到ZedBoard follow doc:Android移植Guide1.3.pdf follow website: http://elinux.org/Zedboard_An ...
- java中内部类使用小结
内部类是指在一个外部类中再定义一个类,类名不需要和文件名相同 内部类可以是静态的,类的修饰符可以是private,default,protect,public修饰 ,而外部类只能是public 和 d ...
- 有效的PhoneGap CSS: WebKit Tap Highlight Color
原文链接:文章1:http://phonegap-tips.com/articles/essential-phonegap-css-webkit-tap-highlight-color.html(此文 ...
- IOS第16天(5,Quartz2D雪花)
*** #import "HMView.h" @interface HMView() { int count; } @property (nonatomic, assign) CG ...
- EL表达式Expression Language
表达式语言Expression Language目的:简化jsp代码 EL内置对象 1.pageContext2.pageScope3.requestScope4.sessionScope5.appl ...
- DS Tree 已知先序、中序 => 建树 => 求后序
参考:二叉树--前序和中序得到后序 思路历程: 在最初敲的时候,经常会弄混preorder和midorder的元素位置.大体的思路就是在preorder中找到根节点(根节点在序列的左边),然后在mid ...
- Extjs 表格横坐标显示问题
在项目中显示chart时,当横坐标的标签名称过长时,extjs会自动隐藏部分的标签. 我想,如果能让标签斜着,或者纵向显示的话,就能够节省x轴上的长度. 经过在网上查找,解决方案如下. //在表格的a ...
- App软件开发的10个常用技巧
移动应用市场用户争夺战日益激烈,原来做APP拼想法拼创意拼是否抓住用户痛点.现在,精细化用户体验成为了一个APP能否留存用户的关键问题,一旦用户觉得体验不畅,马上就有竞品APP后补,如何开发高性能的移 ...
- 《奥威Power-BI智能分析报表制作方法》精彩回顾
年的最后一个月,一年又快过去.工作和学习都不能耽误,本周三奥威公开课又如约与大家见面咯!不知老师教的图文报表在课后你们都有练习吗?趁热打铁,我们现在再次来温习一下吧. 本期分享的内容:<奥威Po ...