Network
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 10261   Accepted: 3807

Description

A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers are connected directly or indirectly by successive links, so data can be transformed between any two computers. The administrator finds that some links are vital to the network, because failure of any one of them can cause that data can't be transformed between some computers. He call such a link a bridge. He is planning to add some new links one by one to eliminate all bridges.

You are to help the administrator by reporting the number of bridges in the network after each new link is added.

Input

The input consists of multiple test cases. Each test case starts with a line containing two integers N(1 ≤ N ≤ 100,000) and M(N - 1 ≤ M ≤ 200,000).
Each of the following M lines contains two integers A and B ( 1≤ AB ≤ N), which indicates a link between computer A and B. Computers are numbered from 1 to N. It is guaranteed that any two computers are connected in the initial network.
The next line contains a single integer Q ( 1 ≤ Q ≤ 1,000), which is the number of new links the administrator plans to add to the network one by one.
The i-th line of the following Q lines contains two integer A and B (1 ≤ ABN), which is the i-th added new link connecting computer A and B.

The last test case is followed by a line containing two zeros.

Output

For each test case, print a line containing the test case number( beginning with 1) and Q lines, the i-th of which contains a integer indicating the number of bridges in the network after the first i new links are added. Print a blank line after the output for each test case.

Sample Input

3 2
1 2
2 3
2
1 2
1 3
4 4
1 2
2 1
2 3
1 4
2
1 2
3 4
0 0

Sample Output

Case 1:

1 0

Case 2:

2 0

我怎么能这么菜

因为会有如下图

1-2
2-3
3-4
4-5
5-6
6-7
7-4
5-8
8-1
那么实际上1.2.3.4.5.8的low值为1
而6.7的low值为4,如果下面标有必须这么写的地方换成low[v]>low[u]就会造成误判

这份代码有个bug

数据 3 3

1 2

2 1

2 3

1

3 2

可以看出他不能判断消除的话要记录一下反向边,然后强行将flag搞成0就可以了吧 因为没有更严格的数据评测也无法判断

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int head[N],dfn[N],low[N],dep[N],fa[N];
int tot,cnt,ans,n,m;
bool flag[N];
struct node{
int to,next;
}e[N<<];
void init(){
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(flag,,sizeof(flag));
fa[]=;
tot=cnt=ans=;
}
void add(int u,int v){
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
void Tajan(int u,int pre){
dep[u]=dep[pre]+;
dfn[u]=low[u]=++cnt;
for(int i=head[u];i+;i=e[i].next){
int v=e[i].to;
if(v==pre) continue;
if(!dfn[v]) {
fa[v]=u;
Tajan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u]) //必须这么写
{
flag[v]=;
++ans;
}
}
else low[u]=min(low[u],dfn[v]);
}
}
void LCA(int a,int b){
if(dep[a]<dep[b]) swap(a,b);
while(dep[a]>dep[b]){
if(flag[a]) --ans,flag[a]=;
a=fa[a];
}
while(a!=b){
if(flag[a]) --ans,flag[a]=;
if(flag[b]) --ans,flag[b]=;
a=fa[a],b=fa[b];
}
}
int main(){
int a,b,T=;
while(scanf("%d%d",&n,&m),n+m){
init();
while(m--){
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
Tajan(,);
scanf("%d",&m);
printf("Case %d:\n",T++);
while(m--){
scanf("%d%d",&a,&b);
if(ans) LCA(a,b);
printf("%d\n",ans);
}
puts("");
}
}

poj3694 连通无向图图加边后有多少桥的更多相关文章

  1. POJ 1144 Network(无向图的割顶和桥模板题)

    http://poj.org/problem?id=1144 题意: 给出图,求割点数. 思路: 关于无向图的割顶和桥,这篇博客写的挺不错,有不懂的可以去看一下http://blog.csdn.net ...

  2. tarjan算法与无向图的连通性(割点,桥,双连通分量,缩点)

    基本概念 给定无向连通图G = (V, E)割点:对于x∈V,从图中删去节点x以及所有与x关联的边之后,G分裂为两个或两个以上不相连的子图,则称x为割点割边(桥)若对于e∈E,从图中删去边e之后,G分 ...

  3. DFS的运用(二分图判定、无向图的割顶和桥,双连通分量,有向图的强连通分量)

    一.dfs框架: vector<int>G[maxn]; //存图 int vis[maxn]; //节点访问标记 void dfs(int u) { vis[u] = ; PREVISI ...

  4. POJ3694 Network(Tarjan双联通分图 LCA 桥)

    链接:http://poj.org/problem?id=3694 题意:给定一个有向连通图,每次增加一条边,求剩下的桥的数量. 思路: 给定一个无向连通图,添加一条u->v的边,求此边对图剩余 ...

  5. 无向图求割(找桥)tarjan

    本博客参考了李煜东的<算法竞赛进阶指南>,大家要是觉得这篇文章写的不错请大家支持正版.豆瓣图书 我在之前的博客中讲解了搜索序时间戳,这次我们讲讲追溯值的概念. 追溯值: 设subtree( ...

  6. 无向图求割点(找桥)tarjan

    本博客参考了李煜东的<算法竞赛进阶指南>,大家要是觉得这篇文章写的不错请大家支持正版.豆瓣图书 我在之前的博客中讲解了搜索序时间戳,这次我们讲讲追溯值的概念. 追溯值: 设subtree( ...

  7. 图论之tarjan真乃神人也,强连通分量,割点,桥,双连通他都会

    先来%一下Robert Tarjan前辈 %%%%%%%%%%%%%%%%%% 然后是热情感谢下列并不止这些大佬的博客: 图连通性(一):Tarjan算法求解有向图强连通分量 图连通性(二):Tarj ...

  8. 在无向图中找最短桥(tarjan)

    题目:hdu 4738 题目意思:  曹操有N个岛,这些岛用M座桥连接起来 每座桥有士兵把守(也可能没有) 周瑜想让这N个岛不连通,但只能炸掉一座桥 并且炸掉一座桥需要派出不小于守桥士兵数的人去 解题 ...

  9. Tarjan找桥和割点与点连通分量与边连通分量【未成形】

    之前只学了个强连通Tarjan算法,然后又摸了缩点操作: 然后今天在lightoj摸了一道模板题,是求所有桥的题: 然后发现,要把:割点,割点集合,双连通,最小割边集合(桥),点连通分量,边连通分量都 ...

随机推荐

  1. java 8 Stream中操作类型和peek的使用

    目录 简介 中间操作和终止操作 peek 结论 java 8 Stream中操作类型和peek的使用 简介 java 8 stream作为流式操作有两种操作类型,中间操作和终止操作.这两种有什么区别呢 ...

  2. 理解分布式一致性:拜占庭容错与PBFT

    理解分布式一致性:拜占庭容错与PBFT 拜占庭问题 拜占庭容错BFT PBFT(Practical Byzantine Fault Tolerance) why 3f+1 ? PBFT 的优点 PBF ...

  3. Spring5参考指南:AspectJ高级编程之Configurable

    文章目录 遇到的问题 @Configurable 原理 重要配置 遇到的问题 前面的文章我们讲到了在Spring中使用Aspect.但是Aspect的都是Spring管理的Bean. 现在有一个问题, ...

  4. js 函数对象的继承 inherit 带 插件完整解析版[helpers.js]

    前言:         本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽.         本篇文章为您分析一下原生JS的对象继承方法 需求分析: 1. ...

  5. Pytorch使用PIL的读取单张图片并显示

    1. Image.open(fp, mode="r") 调用此方法需要引入头文件:from PIL import Image. 参数说明: fp:图片路径,可为绝对路径或相对路径. ...

  6. Mina Basics 02-基础

    基础 在第1章中,我们简要介绍了Apache MINA.在本章中,我们将了解客户端/服务器体系结构以及有关基于MINA的服务器和客户端的详细信息. 我们还将基于TCP和UDP公开一些非常简单的服务器和 ...

  7. Shiro(二):Spring-boot如何集成Shiro(上)

    这篇文章主要介绍了spring-boot是如何集成shiro的authentication流程的. 从shiro-spring-boot-web-starter说起 shiro-spring-boot ...

  8. R 语言命令行参数处理

    在unix.windows外部需要调用R脚本执行,然后又需要输入不同的参数,类似shell脚本的命令行参数输入,可以使用Rcript命令实现. 命令格式:Rscript [options] [-e e ...

  9. 信息奥赛一本通1486: CH 6202 黑暗城堡 最短路径生成树计数

    1486:黑暗城堡 [题目描述] 知道黑暗城堡有 N 个房间,M 条可以制造的双向通道,以及每条通道的长度. 城堡是树形的并且满足下面的条件: 设 Di为如果所有的通道都被修建,第 i 号房间与第 1 ...

  10. win10 安装Maven

    1.将apache-maven-3.0.5-bin.zip解压到指定目录(最好不要有中文字符) 2.配MAVEN_HOME 3.验证是否安装成功  代开cmd窗口  mvn -v 4.修改本地仓库位置 ...