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. linux uart驱动——uart原理(一)

    UART(Universal Asynchronous Receiver and Transmitter)通用异步收发器(异步串行通信口),是一种通用的数据通信协议,它包括了RS232.RS499.R ...

  2. Boost.Asio c++ 网络编程翻译(11)

    *_at方法 这些方法在一个流上面做随机存取操作.你来指定read和write操作从什么地方開始(offset): async_read_at(stream, offset, buffer [, co ...

  3. python 安装protobuf

    安装准备:python和protoc(编译proto到各个语言) 下载protobuf源代码(各种语言实现):https://github.com/google/protobuf  1.到Python ...

  4. Appium python Uiautomator2 多进程问题

    appium更新uiautomator后可以获取tost了,大家都尝试,课程中也讲解了,但是这些跑的时候都在单机上,当我们多机并发的时候会出现一个端口问题,因为我们appium最后会调用uiautom ...

  5. oracle数据库表格操作

    create table dept--创建表格( deptno number(2) primary key, dname varchar2(9) check(dname=Upper(dname)), ...

  6. [转]Struts form传值

    Struts form传值 大约三四个月没用过struts框架,突然想拾起来,却发现好多都忘了.出现传值传不过来的问题.没办法,上网查了一下,看见了一位老师的帖子,总结的很好.特此转载与分享,文末附链 ...

  7. 九度OJ 1051:数字阶梯求和 (大数运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6718 解决:2280 题目描述: 给定a和n,计算a+aa+aaa+a...a(n个a)的和. 输入: 测试数据有多组,输入a,n(1&l ...

  8. Mac下终端常用命令

    一.删除文件 1 打开终端应用程序 2 输入命令:sudo (空格) rm (空格)-r (空格)-f (空格)(注意-f后面还有空格),还要注意,全部小写. 3 把你要删的文件或者文件夹用mouse ...

  9. 洛谷2483 k短路([SDOI2010]魔法猪学院)

    题目请戳这里 一句话题意: 给你一张n个节点,m条单向边的图,求1到n第k短的路. emmm,纪念第一个黑题(我是真的菜啊!!) 这题目还是很难的,本蒟蒻只会被洛谷卡掉的A(所以就愉快地特判了),首先 ...

  10. 我的Android进阶之旅------>Android利用Sensor(传感器)实现水平仪功能的小例

    这里介绍的水平仪,指的是比较传统的气泡水平仪,在一个透明圆盘内充满液体,液体中留有一个气泡,当一端翘起时,该气泡就会浮向翘起的一端.    利用方向传感器返回的第一个参数,实现了一个指南针小应用. 我 ...