Network
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 8669   Accepted: 3175

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次操作。每次操作,添加变(x,y),问添加这条边后,有多少个桥。
 
思路:
对于给予的图,先进行缩点,新的图一定是一棵树。然后每次操作都是在树上进行操作。
对于每次操作,可以从x点对应的树上的点出发,进行dfs,在x,y这条链上的点,用并查集合并,
这样只要判断并查集中根的个数即可。
/*
* Author: sweat123
* Created Time: 2016/6/22 15:00:44
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int to;
int next;
}edge[MAXN<<];
int pre[MAXN],vis[MAXN],dfn[MAXN],low[MAXN],n,m,ind,pa[MAXN];
int px[MAXN],py[MAXN],cnt,f[MAXN],num;
void add(int x,int y){
edge[ind].to = y;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void init(){
cnt = ;
num = ;
memset(f,-,sizeof(f));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
}
int find(int x){
if(x != pa[x])pa[x] = find(pa[x]);
return pa[x];
}
void dfs(int rt,int k,int fa){
dfn[rt] = low[rt] = k;
for(int i = pre[rt]; i != - ; i = edge[i].next){
int t = edge[i].to;
if(!dfn[t]){
dfs(t,k+,rt);
low[rt] = min(low[rt],low[t]);
if(low[t] > dfn[rt]){
px[cnt] = rt,py[cnt++] = t;
} else{
int fx = find(rt);
int fy = find(t);
pa[fx] = fy;
}
} else if(t != fa){
low[rt] = min(low[rt],dfn[t]);
}
}
}
int dfs2(int rt,int k){
vis[rt] = ;
if(rt == k){
return ;
}
for(int i = pre[rt]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!vis[t]){
int p = dfs2(t,k);
if(p == ){
int fx = find(rt);
int fy = find(t);
pa[fx] = fy;
return ;
}
}
}
return ;
}
int main(){
int ff = ;
while(~scanf("%d%d",&n,&m)){
if(n == && m == )break;
ind = ;
memset(pre,-,sizeof(pre));
for(int i = ; i <= m; i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y),add(y,x);
}
for(int i = ; i <= n; i++){
pa[i] = i;
}
init();
dfs(,,-);
for(int i = ; i <= n; i++){
int fx = find(i);
if(f[fx] == -)f[fx] = ++num;
f[i] = f[fx];
}
ind = ;
memset(pre,-,sizeof(pre));
for(int i = ; i < cnt; i++){
int x = f[px[i]];
int y = f[py[i]];
add(x,y),add(y,x);
}
int q;
printf("Case %d:\n",++ff);
scanf("%d",&q);
for(int i = ; i <= n; i++){
pa[i] = i;
}
while(q--){
int x,y;
scanf("%d%d",&x,&y);
x = f[x];
y = f[y];
memset(vis,,sizeof(vis));
dfs2(x,y);
int ans = ;
for(int i = ; i <= num; i++){
int fx = find(i);
if(fx == i)ans += ;
}
printf("%d\n",ans - );
}
printf("\n");
}
return ;
}

poj3694 缩点边双连通分量的更多相关文章

  1. POJ3694 Network(边双连通分量+缩点+LCA)

    题目大概是给一张图,动态加边动态求割边数. 本想着求出边双连通分量后缩点,然后构成的树用树链剖分+线段树去维护路径上的边数和..好像好难写.. 看了别人的解法,这题有更简单的算法: 在任意两点添边,那 ...

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

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

  3. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  4. Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】

    一.基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成 ...

  5. POJ3177 Redundant Paths(边双连通分量+缩点)

    题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...

  6. HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)

    Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...

  7. (转)Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)

    基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个 ...

  8. Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)

    Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...

  9. [HDOJ4612]Warm up(双连通分量,缩点,树直径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 所有图论题都要往树上考虑 题意:给一张图,仅允许添加一条边,问能干掉的最多条桥有多少. 必须解决 ...

随机推荐

  1. 调用newtonsoft.json反序列出错

    调用newtonsoft.json反序列出错: Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current J ...

  2. [No00000B]MS OFFICE 2013 快捷键大全

    常用快捷键 快捷键 作用 Ctrl+Shift+Spacebar 创建不间断空格 Ctrl+-(连字符) 创建不间断连字符 Ctrl+B 使字符变为粗体 Ctrl+I 使字符变为斜体 Ctrl+U 为 ...

  3. gnuplot 的安装

    需要同时安装gnuplot和gnuplot-x11才能画出图 sudo apt-get install gnuplot gnuplot-x11 gnuplot not showing the grap ...

  4. Python的高级特性6:使用__slots__真的能省很多内存

    在伯乐在线上看到了这篇文章,用Python的 __slots__ 节省9G内存,于是想测试下,对单个类,用__slots__节省内存效果会不会明显. 看完这个例子后,我们也会明白__slots__是用 ...

  5. Java:对象的强、软、弱和虚引用

    1.对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序才能使用它.从JDK ...

  6. 产品经理技能之BRD的笔记之菜鸟入门

    链接:http://www.woshipm.com/pmd/178527.html?utm_source=tuicool 要学习MRD.PRD,先从BRD开始,才能做到知其然知其所以然. BRD是什么 ...

  7. NET Core HTTP 管道

    ASP.NET Core HTTP 管道中的那些事儿   前言 马上2016年就要过去了,时间可是真快啊. 上次写完 Identity 系列之后,反响还不错,所以本来打算写一个 ASP.NET Cor ...

  8. nginx认证配置

      rpm -qa|grep httpd-tools yum install httpd-tools ###这样不仅可以使用ab工具,还可以使用htpasswd工具了     虚拟主机 ->&g ...

  9. linux磁盘分区-系统安装

    零 系统下载: https://lists.centos.org/pipermail/centos-announce/2016-May/021895.html 往下拉可以看到 一 系统安装 1, 2, ...

  10. Linux设置环境变量(解决许多命令找不到)

    不知道服务器被谁给改坏了,许多命令都不能使用找不到,但是可以在/usr/bin/,/usr/local/bin等里面找到源程序,当时首先想到的就是环境变量,因为Windows在设置了环境变量之后就可以 ...