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. 李振杰:火狐Mozilla被黑事件的启发

    火狐浏览器开发商Mozilla近日宣布,因为数据库存在漏洞.Mozilla开发者的数万个电子邮件地址和加密password或遭到黑客窃取. 好多有为青年们往往刚刚获得了一个小成功,便開始沾沾自喜,自命 ...

  2. Linux命令apt-get apt的常见用法

    高级包装工具(英语:Advanced Packaging Tools,缩写为APT) apt-cache search foo //搜索和"foo"匹配的包. apt-cache ...

  3. PHP合并数组+与array_merge的区别

    http://www.phpernote.com/php-string/351.html PHP中合并两个数组可以使用+或者array_merge,但这两个还是有区别的   主要区别是当两个或者多个数 ...

  4. C#之stream

    在C#中经常要用stream stream下面主要有 FileStream:使用文件作为后备设备. BufferedStream:使用缓冲区作为后备设备,用来增强性能的中间存储. MemoryStre ...

  5. php生成唯一的串

    1.方法一: <?php md5(uniqid('aa',true)); ?> 2.方法2: //生成16位的串$randLength=6; $chars='abcdefghijklmno ...

  6. Win10上Python3通过pip安装时出现UnicodeDecodeError

    http://blog.csdn.net/qq_33530388/article/details/68933201 解决方法: 打开 c:\program files\python36\lib\sit ...

  7. wait() 区别 sleep()

    wait() notify() notifyAll() wait和notify方法必须写在synchronized方法内,即在调用wait和notify方法前,需先获得对象锁: 调用wait方法则释放 ...

  8. java的Access restriction错误

    问 :import sun.management.ManagementFactory,我在rt包下已经找到sun.management.ManagementFactory,但就是有错,请问怎么回事. ...

  9. 九度OJ 1019:简单计算器 (基础题、DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6725 解决:2454 题目描述:     读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. 输入:     ...

  10. Linux C语言 网络编程(二) server模型

    前面介绍了关于连接linux服务端方式,可是服务端的资源是有限的,所以我们通常须要又一次思考,设计一套server模型来处理相应的client的请求. 第一种:并发server.通过主进程统一处理cl ...