Network
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 7298   Accepted: 2651

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

Source

 
题目意思:
n个节点,m条无向边的图。然后q个操作,每个操作连接u、v点,然后输出连接后图中剩余桥的个数。
 
思路:
先tarjan标记一下桥,然后lca把路径上所有桥去掉标记。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <stack>
using namespace std; #define N 100005 int n, m;
int dfn[N], low[N], Time;
int dep[N];
bool brg[N];
int father[N];
int brg_num; struct Edge{
int u, v, next;
}e[]; int cnt;
int head[N]; void setEdge(int u,int v){
e[cnt].u=u;e[cnt].v=v;
e[cnt].next=head[u];head[u]=cnt++;
} void tarjan(int u,int fa){
int i, v;
dfn[u]=low[u]=Time++;
for(i=head[u];i!=-;i=e[i].next){
v=e[i].v;
if(v==fa) continue;
if(!dfn[v]){
dep[v]=dep[u]+;
father[v]=u;
tarjan(v,u);
if(low[v]>dfn[u]){
brg_num++;
brg[v]=true;
}
low[u]=min(low[u],low[v]);
}
else low[u]=min(dfn[v],low[u]);
}
} void lca(int u,int v){
while(dep[u]>dep[v]){
if(brg[u]) {
brg[u]=false,brg_num--;
}
u=father[u];
}
while(dep[v]>dep[u]){
if(brg[v]) {
brg[v]=false,brg_num--;
}
v=father[v];
}
while(u!=v){
if(brg[u]) {
brg[u]=false,brg_num--;
}
u=father[u];
if(brg[v]) {
brg[v]=false,brg_num--;
}
v=father[v];
}
} main()
{
int i, j, k;
int u, v;
int kase=;
while(scanf("%d %d",&n,&m)==){
if(!n&&!m) break;
memset(head,-,sizeof(head));
cnt=;
for(i=;i<m;i++){
scanf("%d %d",&u,&v);
setEdge(u,v);
setEdge(v,u);
}
Time=;
memset(dfn,,sizeof(dfn));
memset(brg,false,sizeof(brg));
brg_num=;
for(i=;i<=n;i++){
if(!dfn[i]){
dep[i]=;
tarjan(i,i);
}
}
printf("Case %d:\n",kase++);
int q;
scanf("%d",&q);
for(i=;i<q;i++){
scanf("%d %d",&u,&v);
lca(u,v);
printf("%d\n",brg_num);
}
cout<<endl;
}
}

POJ 3694 tarjan 桥+lca的更多相关文章

  1. POJ 3694 (tarjan缩点+LCA+并查集)

    好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. #include <iostream> #include <algorithm& ...

  2. poj 3694 Network(割边+lca)

    题目链接:http://poj.org/problem?id=3694 题意:一个无向图中本来有若干条桥,有Q个操作,每次加一条边(u,v),每次操作后输出桥的数目. 分析:通常的做法是:先求出该无向 ...

  3. hdu 2460 poj 3694 (双联通+LCA)

    在给出的两个点上加一条边,求剩下桥的数量,,不会LCA在线,就用了最普通的,先Tarjan双联通缩点,然后将缩完的图建成一棵树,树的所有边就是桥了,如果在任意两点间加一条边的话,那么从两点到最近公共祖 ...

  4. poj 3694(割边+lca)

    题意:给你一个无向图,可能有重边,有q次询问,问你每次我添加一条边,添加后这个图还有多少个桥 解题思路:首先先把所有没有割边的点对缩成一个联通块,无向图一般并查集判环,然后就得到一个割边树,给你一条新 ...

  5. 【POJ 3694】 Network(割边&lt;桥&gt;+LCA)

    [POJ 3694] Network(割边+LCA) Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7971 ...

  6. POJ 3694——Network——————【连通图,LCA求桥】

    Network Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  7. Poj 3694 Network (连通图缩点+LCA+并查集)

    题目链接: Poj 3694 Network 题目描述: 给出一个无向连通图,加入一系列边指定的后,问还剩下多少个桥? 解题思路: 先求出图的双连通分支,然后缩点重新建图,加入一个指定的边后,求出这条 ...

  8. Tarjan算法各种&RMQ& POJ 3694

    关于tarjan 的思想可以在网上搜到,具体我也不太清楚,应该说自己理解也不深,下面是做题经验得到的一些模板. 其中有很多转载,包括BYVoid等,感谢让我转...望各路大神愿谅 有向图求连通分量的一 ...

  9. 【Tarjan】洛谷P3379 Tarjan求LCA

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

随机推荐

  1. 多列布局——column-width

    column-width的使用和CSS中的width属性一样,不过不同的是,column-width属性在定义元素列宽的时候,既可以单独使用,也可以和多列属性中其他属性配合使用.其基本语法如下所示 : ...

  2. Android之ViewHolder用法

    http://www.cnblogs.com/mengdd/p/3254323.html (zhuan) http://blog.csdn.net/mrzhang_happy/article/deta ...

  3. 日常开发使用SVN命令

    现在把我日常开发中用到的svn命令总结出来,做个备忘,其实真正用到也就那几个. 如果遇到参数不知道使用或其它困难请使用:svn --help 得到帮助 1)检出: svn co svn地址 本地路径 ...

  4. 批处理命令——choice

    [1]choice命令简介 使用此命令可以提示用户输入一个选择项,根据用户输入的选择项再决定执行具体的过程. 使用时应该加/c:参数,c: 后应写提示可输入的字符或数字,之间无空格.冒号是可选项. 使 ...

  5. jQuery 选择器 (基础恶补之二)

    返回 CSS 属性 如需返回指定的 CSS 属性的值,请使用如下语法: css("propertyname"); 下面的例子将返回首个匹配元素的 background-color ...

  6. java语言的认识

    class Hello{ public static void main(String [] args) { System.out.println("Hello Word 你好") ...

  7. Android自定义View (二) 进阶

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自定义View之旅,前面已经介绍过一个自定义View的基础的例 ...

  8. 【Java】关于JVM运行时内存空间、JVM垃圾回收机制

    参考的优秀文章 <深入理解Java虚拟机 JVM高级特性与最佳实线>(机械工业出版社) Java虚拟机的堆.栈.堆栈如何去理解? 聊聊JVM的年轻代 前言 本文是<深入理解Java虚 ...

  9. ArcGIS Javascript地图上添加json数据格式的点

    /** * 显示地图点. * json的格式[{"name":"name1","x":"x1","y" ...

  10. 使用VB6制作RTD函数

    以前模仿大神在vs里使用c#实现RTD函数功能.(真是很生僻的东东啊)C#制作RTD参考:大神博客跳转.最近想VB里能不能做?就试着做了做,好像基本成了,整套代码有些毛病,勉强能算个样子,暂时不打算再 ...