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决定烧毁所有的桥.这是个 ...
随机推荐
- (WC2016模拟十一)【BZOJ4695】最假女选手
ps:好久没更博啦……这几天连着有模拟赛,等初赛前后休息的时候来疯狂补坑吧……顺便补一下前面的数论啥的? 题解: mdzz我场上写了个15分暴力长度跟标算差不多... 线段树大法好啊!这题听说很多人做 ...
- 【codeforces 379D】New Year Letter
[题目链接]:http://codeforces.com/contest/379/problem/D [题意] 让你构造出两个长度分别为n和m的字符串s[1]和s[2] 然后按照连接的规则,顺序连接s ...
- SPOJ 962 Intergalactic Map
Intergalactic Map Time Limit: 6000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Ori ...
- 【转】Java集合间的相互转换
下面代码演示了List<-->数组.List<-->Set.数组<-->Set.Map将键转化为Set.Map将值转化为Set.Map将值转化为List等集合常用转 ...
- 洛谷 P2665 [USACO08FEB]连线游戏Game of Lines
P2665 [USACO08FEB]连线游戏Game of Lines 题目背景 Farmer John最近发明了一个游戏,来考验自命不凡的贝茜. 题目描述 Farmer John has chall ...
- 电源管理ACPI、及APM、GNU/Linux系统下的相应命令使用
/********************************************************************* * Author : Samson * Date ...
- ListCtrl 控件数据动态改动
參考文章:ListCtrl 控件数据动态改动 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDIzNjU1MA==/font/5a6L5L2T/f ...
- [MST] Restore the Model Tree State using Hot Module Reloading when Model Definitions Change
n this lesson, we will set up Hot Module Reloading(HMR), making it possible to load new definitions ...
- JavaWeb利用cookie记住账号
JavaWeb利用cookie记住账号. 首先,来看看界面什么样子. 记住账号最普遍的做法,就是在点击登录时,将账号保存到cookie中. 材料准备 <script src="${ct ...
- echarts 地图 动态 展示 结合css+js
echarts地图展示功能非常强大,官网上静态展示的样例非常多了,动态的资料少.研究了下.我眼下实现的通过ajax从server获取数据动态展示地图. 另外,我们有时候希望在地图之上做些自己定义的东西 ...