题目地址: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(无向连通图求割边)的更多相关文章

  1. zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】

    Burning Bridges Time Limit: 5 Seconds      Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...

  2. ZOJ 2588 Burning Bridges(求桥的数量,邻接表)

    题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2588 Burning Bridges Time Limit: 5 ...

  3. ZOJ 2588 Burning Bridges(求含重边的无向连通图的割边) - from lanshui_Yang

    Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little country ...

  4. 【求无向图的桥,有重边】ZOJ - 2588 Burning Bridges

    模板题——求割点与桥 题意,要使一个无向图不连通,输出必定要删掉的边的数量及其编号.求桥的裸题,可拿来练手. 套模板的时候注意本题两节点之间可能有多条边,而模板是不判重边的,所以直接套模板的话,会将重 ...

  5. ZOJ 2588 Burning Bridges (tarjan求割边)

    题目链接 题意 : N个点M条边,允许有重边,让你求出割边的数目以及每条割边的编号(编号是输入顺序从1到M). 思路 :tarjan求割边,对于除重边以为中生成树的边(u,v),若满足dfn[u] & ...

  6. zoj——2588 Burning Bridges

    Burning Bridges Time Limit: 5 Seconds      Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...

  7. zoj 2588 Burning Bridges(割边/桥)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1588 题意:Ferry王国有n个岛,m座桥,每个岛都可以互达,现在要 ...

  8. ZOJ2588:Burning Bridges(无向连通图求割边)

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1588 吐下槽,不得不说ZOJ好坑,模版题做了一个多小时. 题意:*    ...

  9. zoj 2588 Burning Bridges

    题目描述:Ferry王国是一个漂亮的岛国,一共有N个岛国.M座桥,通过这些桥可以从每个小岛都能到达任何一个小岛.很不幸的是,最近Ferry王国被Jordan征服了.Jordan决定烧毁所有的桥.这是个 ...

随机推荐

  1. 树上倍增求LCA

    大概思想就是,节点$i$的第$2^{j}$个父节点是他第$2^{j-1}$个父亲的第$2^{j-1}$个父亲 然后可以$O(nlogn)$时间内解决…… 没了? //fa[i][j]表示i的第2^j个 ...

  2. Linux 文件系统的层次化结构

    FHS,Filesystem Hierarchy Standard,文件系统层次化标准.这是一个推荐标准,可以从 http://www.pathname.com/fhs/ 获取. 本文不讨论 FHS, ...

  3. Python格式化字符串、占位符、合并数组

    合并数组 参考链接:https://www.cnblogs.com/chaihy/p/7243143.html >>> a=[2] >>> b=[3] >&g ...

  4. 题解 P2532 【[AHOI2012]树屋阶梯】

    本题运用卡特兰数求解. 卡特兰数有两种表达方式: 1)\(h_i=\sum^{k=0}_{i-1}h_kh_{i-k-1}\) 2)\(h_i=\frac{1}{n+1}C^{n}_{2n}\) 运用 ...

  5. 【codeforces 314C】Sereja and Subsequences

    [题目链接]:http://codeforces.com/problemset/problem/314/C [题意] 让你从n个元素的数组中选出所有的不同的非递减子数列; 然后计算比这个子数列小的和它 ...

  6. python __future__ 的几种特性

    今天看tensorflow的代码,看到python里面有这么几句: from __future__ import absolute_import from __future__ import divi ...

  7. C++ 数字、string 简便互转

    一.数字转为 string 类型 借用 sprintf 函数: char buffer[256]; int counter = 10; sprintf(buffer,"%04i", ...

  8. linux清除邮件队列

    [root@localhost mail]#tmp=`mailq | grep -E "root" | awk '{print $1}'` [root@localhost mail ...

  9. 微信iOS SDK文档总结

    至今共19个类.分3大类. (1)请求与响应类:微信终端和第三方程序:第三方程序和微信server. BaseReq:全部请求类的基类. GetMessageFromWXReq:微信终端向第三方程序请 ...

  10. iOS自动布局高级用法 && 纯代码约束写法

    本文主要介绍几个我遇到的总结的高级用法(当然我相信肯定有不少比这还高级的). 简单的storyboard中上下左右约束,固定宽高啥的用法在这里就不做赘述了. autolayout自动布局是iOS6以后 ...