Network
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 7943   Accepted: 2893

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≤ A ≠ B ≤ 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 ≤ A ≠ B ≤ N), 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 题意:给定结点和边确定一幅无向图,然后增加Q条边,输出每增加一条边之后图中的割边数目。
思路:先利用tarjan求割边。新增加的一条边两端的结点u、v,u和v到它们的LCA之间的割边全部消失。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=;
struct Edge{
int to,net;
}es[MAXN*];
int head[MAXN],tot;
void addedge(int u,int v)
{
es[tot].to=v;
es[tot].net=head[u];
head[u]=tot++;
} int n,m,q;
int dfn[MAXN],low[MAXN],key;
bool bridge[MAXN];
int par[MAXN],depth[MAXN];
int cnt;
void tarjan(int u,int fa,int dep)
{
par[u]=fa;
depth[u]=dep;
dfn[u]=++key;
low[u]=key;
for(int i=head[u];i!=-;i=es[i].net)
{
int to=es[i].to;
if(!dfn[to])
{
tarjan(to,u,dep+);
low[u]=min(low[u],low[to]);
if(dfn[u]<low[to])
{
bridge[to]=true;
cnt++;
}
}
else if(to!=fa) low[u]=min(low[u],dfn[to]);
}
} void query(int u,int v)
{
if(depth[u]>depth[v]) swap(u,v);
while(depth[v]>depth[u])
{
if(bridge[v])
{
bridge[v]=false;
cnt--;
}
v=par[v];
}
while(u!=v)
{
if(bridge[u])
{
bridge[u]=false;
cnt--;
}
u=par[u]; if(bridge[v])
{
bridge[v]=false;
cnt--;
}
v=par[v];
}
} int main()
{
int cas=;
while(scanf("%d%d",&n,&m)!=EOF&&(n+m)!=)
{
cnt=;
memset(head,-,sizeof(head));
key=;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(bridge,false,sizeof(bridge));
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
tarjan(,,);
scanf("%d",&q);
printf("Case %d:\n",++cas);
while(q--)
{
int u,v;
scanf("%d%d",&u,&v);
query(u,v);
printf("%d\n",cnt);
}
printf("\n");
}
return ;
}

POJ3694(求割边)的更多相关文章

  1. [学习笔记]tarjan求割边

    上午打模拟赛的时候想出了第三题题解,可是我不会求割边只能暴力判割边了QAQ 所以,本文介绍求割边(又称桥). 的定义同求有向图强连通分量. 枚举当前点的所有邻接点: 1.如果某个邻接点未被访问过,则访 ...

  2. 【NOIP训练】【Tarjan求割边】上学

    题目描述 给你一张图,询问当删去某一条边时,起点到终点最短路是否改变. 输入格式 第一行输入两个正整数,分别表示点数和边数.第二行输入两个正整数,起点标号为,终点标号为.接下来行,每行三个整数,表示有 ...

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

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

  4. ZOJ Problem - 2588 Burning Bridges tarjan算法求割边

    题意:求无向图的割边. 思路:tarjan算法求割边,访问到一个点,如果这个点的low值比它的dfn值大,它就是割边,直接ans++(之所以可以直接ans++,是因为他与割点不同,每条边只访问了一遍) ...

  5. HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】

     Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  6. tarjan求割边割点

    tarjan求割边割点 内容及代码来自http://m.blog.csdn.net/article/details?id=51984469 割边:在连通图中,删除了连通图的某条边后,图不再连通.这样的 ...

  7. ZOJ 2588 求割边问题

    题目链接:http://vjudge.net/problem/viewProblem.action?id=14877 题目大意: 要尽可能多的烧毁桥,另外还要保证图的连通性,问哪些桥是绝对不能烧毁的 ...

  8. 洛谷P1656 炸铁路 (求割边)

    用tarjan变种求割边的模板题 其实还可以求出所有的边双(用栈),但本题不需要求. 1 #include<bits/stdc++.h> 2 using namespace std; 3 ...

  9. hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割

    view code//hdu 3987 #include <iostream> #include <cstdio> #include <algorithm> #in ...

随机推荐

  1. JavaScrip函数与声明表达式

    首先我们看下函数的两种命名方式 1.函数声明,声明一个函数 function test1(){ var a=0; console.log(a); //左一些操作... } 执行结果如下 我们看一下,无 ...

  2. js document.queryCommandState() 各个参数

    命令标识符 2D-Position 允许通过拖曳移动绝对定位的对象. AbsolutePosition 设定元素的 position 属性为“absolute”(绝对). BackColor 设置或获 ...

  3. SQL Server 存储过程的几种常见写法分析,我们该用那种写法

    本文出处: http://www.cnblogs.com/wy123/p/5958047.html 最近发现还有不少做开发的小伙伴,在写存储过程的时候,在参考已有的不同的写法时,往往很迷茫,不知道各种 ...

  4. strpos与strstr之间的区别

    string strstr(string haystack,string needle) 返回haystack中从第一 个needle开头到haystack末尾的字符串. 如果未找到needle 返回 ...

  5. fzu 2039 Pets (简单二分图 + (最大流 || 二分图))

    Are you interested in pets? There is a very famous pets shop in the center of the ACM city. There ar ...

  6. ifndef/define/endif 和 #ifdef 、#if 作用和用法

    为了能简单的看看某些linux内核源码,复习了一下c语音,今天汇总了一下关于宏定义的相关内容: 一.ifndef/define/endif用法: .h文件,如下: #ifndef XX_H #defi ...

  7. 【BZOJ4373】算术天才⑨与等差数列 线段树+set

    [BZOJ4373]算术天才⑨与等差数列 Description 算术天才⑨非常喜欢和等差数列玩耍.有一天,他给了你一个长度为n的序列,其中第i个数为a[i].他想考考你,每次他会给出询问l,r,k, ...

  8. sed命令使用举例

    选择操作的行范围 sed -n '1,2p' testsed2.txt  匹配第1到2行 sed -n '/a/,/b/p' testsed2.txt  匹配从包含a的行到包含b的行 sed -n ' ...

  9. [容易]合并排序数组 II

    题目来源:http://www.lintcode.com/zh-cn/problem/merge-sorted-array/

  10. STM32 ~ USART接收不定长数据

    IDLE中断什么时候发生? IDLE就是串口收到一帧数据后,发生的中断.什么是一帧数据呢?比如说给单片机一次发来1个字节,或者一次发来8个字节,这些一次发来的数据,就称为一帧数据,也可以叫做一包数据. ...