ZOJ 2588 Burning Bridges(无向连通图求割边)
题目地址:ZOJ 2588
由于数组开小了而TLE了。。这题就是一个求无向连通图最小割边。仅仅要推断dfn[u]是否<low[v],由于low指的当前所能回到的祖先的最小标号,增加low[v]大于dfn[u]时,说明v无法通过其它边回到u之前的点。也就是说v假设想要回到u的祖先点。必需要经过u点,那这条边非常明显就是一条割边。这题还要去重边,假如有重边的话。说明怎么销毁哪条边总能通过还有一条边,所以仅仅要有重边。说明这两点之间没有割边。
代码例如以下;
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
int head[20020], cnt, index1, ans;
int low[20103], dfn[20103], road[20103];
struct node
{
int num, u, v, next, tag;
} edge[220000];
void add(int num, int u, int v)
{
int i;
for(i=head[u]; i!=-1; i=edge[i].next)
{
if(edge[i].v==v)
break;
}
if(i!=-1)
{
edge[i].tag=1;
return ;
}
edge[cnt].tag=0;
edge[cnt].num=num;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void tarjan(int u, int fa)
{
low[u]=dfn[u]=++index1;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(v==fa) continue ;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v]&&!edge[i].tag)
{
road[++ans]=edge[i].num;
//printf("%d %d %d %d\n",u,v,dfn[u],low[v]);
}
}
else
low[u]=min(low[u],dfn[v]);
}
}
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
index1=ans=0;
memset(dfn,0,sizeof(dfn));
}
int main()
{
int t, n, m, u, v, i, j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
for(i=1; i<=m; i++)
{
scanf("%d%d",&u,&v);
add(i,u,v);
add(i,v,u);
}
tarjan(1,0);
printf("%d\n",ans);
if(ans)
{
sort(road+1,road+ans+1);
for(i=1; i<ans; i++)
{
printf("%d ",road[i]);
}
printf("%d\n",road[ans]);
}
if(t!=0)
printf("\n");
}
return 0;
}
ZOJ 2588 Burning Bridges(无向连通图求割边)的更多相关文章
- 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(求含重边的无向连通图的割边) - from lanshui_Yang
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little country ...
- 【求无向图的桥,有重边】ZOJ - 2588 Burning Bridges
模板题——求割点与桥 题意,要使一个无向图不连通,输出必定要删掉的边的数量及其编号.求桥的裸题,可拿来练手. 套模板的时候注意本题两节点之间可能有多条边,而模板是不判重边的,所以直接套模板的话,会将重 ...
- ZOJ 2588 Burning Bridges (tarjan求割边)
题目链接 题意 : N个点M条边,允许有重边,让你求出割边的数目以及每条割边的编号(编号是输入顺序从1到M). 思路 :tarjan求割边,对于除重边以为中生成树的边(u,v),若满足dfn[u] & ...
- 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?problemId=1588 题意:Ferry王国有n个岛,m座桥,每个岛都可以互达,现在要 ...
- ZOJ2588:Burning Bridges(无向连通图求割边)
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1588 吐下槽,不得不说ZOJ好坑,模版题做了一个多小时. 题意:* ...
- zoj 2588 Burning Bridges
题目描述:Ferry王国是一个漂亮的岛国,一共有N个岛国.M座桥,通过这些桥可以从每个小岛都能到达任何一个小岛.很不幸的是,最近Ferry王国被Jordan征服了.Jordan决定烧毁所有的桥.这是个 ...
随机推荐
- (五)Redux入门
1 Redux概念简述 flux推出的时候有一些缺点.比如store可以存在多个,不是特别好用 于是逐渐进化为了redux. 2 Redux的工作流程 拿借书作举例: action creators是 ...
- unity 调用 .dll 或 .so时遇到的问题
1.32位的 .dll 无法在64位的unity编辑器下运行. System.DllNotFoundException: xxx , 64位的程序运行32位的dll是会报这种错 2.Failed t ...
- PatentTips - Method for network interface sharing among multiple virtual machines
BACKGROUND Many computing systems include a network interface card (NIC) to provide for communicatio ...
- Ubuntu ctrl+alt会导致窗口还原的问题
Ubuntu ctrl+alt会导致窗口还原的问题 本来以为是compizConfig的问题,后来在系统config中找到键盘>快捷键:恢复窗口,删除这个快捷键,就好了: 原来这里写的是ctrl ...
- win7笔记本设置wifi热点
1.打开cmd 输入netsh wlan set hostednetwork mode=allow ssid=ACE-PC key=12345678 2.等待1-2分钟后,网络连接里会出现一个&quo ...
- 登录那些事儿+ Session原理
http://cnodejs.org/topic/5671441a1d2912ce2a35aaa1 登录那些事儿 http://www.jianshu.com/p/2b7c10291aad Sess ...
- js之insertBefore(newElement,oldElement)
insertBefore的用法,以及注意事项,并且模仿编写insertAfter()方法 DOM提供的一个名为insertBefore()的方法,用来将一个新元素插入到现有的元素的前面. 使用这个方法 ...
- animation和transition做动画的区别
animation做动画,是不需要去触发的,可以定义一开始就执行 transition做动画,是需要人为触发,才能执行的
- springMVC中跳转问题
在使用SpringMVC时遇到了这个跳转的问题很头疼.现在总结出来,对以后的开发有所帮助. . 1.可以采用ModelAndView: @RequestMapping("test1" ...
- 515Nod 1126 求递推序列的第n项【矩阵快速幂】
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...