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中ThreadLocal的使用

    文章目录 在Map中存储用户数据 在ThreadLocal中存储用户数据 java中ThreadLocal的使用 ThreadLocal主要用来为当前线程存储数据,这个数据只有当前线程可以访问. 在定 ...

  2. 【Linux常见命令】find命令

    find - search for files in a directory hierarchy find命令用来在指定目录下查找文件. 任何位于参数之前的字符串都将被视为欲查找的目录名. 如果使用该 ...

  3. webpack打包多入口配置

    在它的entry入口设置多文件入口即可,例: entry: { core: './src/core.js', design: './src/design.js' }, 单一出口输出: output: ...

  4. MySQL分页查询的性能优化

    MySQL limit分页查询的性能优化 Mysql的分页查询十分简单,但是当数据量大的时候一般的分页就吃不消了. 传统分页查询:SELECT c1,c2,cn… FROM table LIMIT n ...

  5. Longest XXX

    Longest Common Substring Brute Force 遍历a和b所有位置的组合,向后延伸,直到遇到两个不同的字符,复杂度是\(n^3\)级别. class Solution { p ...

  6. 动态规划经典算法--最长公共子序列 LCS

    转移方程 代码: //法一: #include <bits/stdc++.h> using namespace std; //---------------https://lunatic. ...

  7. 数学--数论--HDU 5019 revenge of GCD

    Revenge of GCD Problem Description In mathematics, the greatest common divisor (gcd), also known as ...

  8. 数学--数论--Hdu 1452 Happy 2004(积性函数性质+和函数公式+快速模幂+乘法逆元)

    Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your ...

  9. Appium-desktop 元素定位

    1.打开 appium-desktop ,点击 start session 2.打开后,点击屏幕右上角的搜索按钮 3.然后会打开配置页面,在本地服务配置信息同上面写的代码链接配置.填入正确的信息后,点 ...

  10. Android | 带你零代码实现安卓扫码功能

    目录 小序 背景介绍 前期准备 开始搬运 结语 小序   这是一篇纯新手教学,本人之前没有任何安卓开发经验(尴尬),本文也不涉及任何代码就可以使用一个扫码demo,华为scankit真是新手的福音-- ...