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决定烧毁所有的桥.这是个 ...
随机推荐
- BZOJ 3166 [HEOI2013]Alo (可持久化01Trie+链表)
题目大意:给你一个长度为$n$的序列,让你找出一段子序列,求其中的 次大值 异或 序列里一个数 能得到的最大值 先对序列建出可持久化$Trie$ 按元素的值从小到大遍历,设当前元素的位置是i,找出它左 ...
- 【Paper Reading】Improved Textured Networks: Maximizing quality and diversity in Feed-Forward Stylization and Texture Synthesis
Improved Textured Networks: Maximizing quality and diversity in Feed-Forward Stylization and Texture ...
- HTTP 文件共享服务器工具 - chfs
CuteHttpFileServer/chfs是一个免费的.HTTP协议的文件共享服务器,使用浏览器可以快速访问.它具有以下特点: 单个文件,整个软件只有一个可执行程序,无配置文件等其他文件 跨平台运 ...
- numpy学习笔记 - numpy常用函数、向量化操作及基本数学统计方法
# -*- coding: utf-8 -*-"""主要记录代码,相关说明采用注释形势,供日常总结.查阅使用,不定时更新.Created on Fri Aug 24 19 ...
- 题解 P3369 【【模板】普通平衡树】
在网上某篇神奇的教程和@codesonic 大佬的标程帮助下,我又肝完了Leafy Tree,跑过来写篇题解(好像以前写过一篇?) 什么是Leafy Tree? Leafy Tree由两种节点组成:辅 ...
- O(1)复杂度增加和删除和随机取
题目: https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed 非常好的解法: https://disc ...
- Codeforces 558E 线段树处理字符串内排序
给出长度为n的字符串,m个操作. 每一个操作有三个值 l,r,op. op==1,表示将字符串中[ l ,r ]的部分依照升序排列. op==0,表示将字符串中[ l ,r ]的部分依照降序排列. 输 ...
- [MST] Test mobx-state-tree Models by Recording Snapshots or Patches
Testing models is straightforward. Especially because MST provides powerful tools to track exactly h ...
- 工作笔记5.JAVA图片验证码
本文主要内容为:利用JAVA图片制作验证码. 设计思路: 1.拷贝AuthImageServlet.class图片验证码 2.配置web.xml 3.JSP中,调用封装好的AuthImageServl ...
- Python库之pyudev (一)
库pyudev是libudev的python封装,libudev提拱了对本地设备的列举与查询API. 1.安装 pip install pyudev 2. 使用 2.1 开始 导入pyudev,验证库 ...