Network
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 10404   Accepted: 3873

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

 
题意:
动态加边,求桥的数目,当然不能一直dfs(tarjin)。
分析:
操作是,加边后可以通过第一次的桥数目推出来,这条路上有桥,则,可能存在多条桥,还要继续DFS上去。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm> using namespace std; const int maxn = ; vector<int> g[maxn];
int low[maxn],dfn[maxn];
int fa[maxn];
int bin[maxn];
int t;
int ans;
void tarjin(int u,int f) {
low[u] = dfn[u] = ++t;
fa[u] = f; for(int i = ; i < (int)g[u].size(); i++) {
int v = g[u][i];
if(!dfn[v]) {
tarjin(v,u);
low[u] = min(low[u],low[v]);
if(low[v]>dfn[u]) {
bin[v]++;
ans++;
}
}
else if(f!=v) {
low[u] = min(low[u],dfn[v]);
if(low[v]>dfn[u]) { //有反向边,不是桥
bin[v]--;
ans--;
}
}
} } void LCR(int a,int b) {
if(a==b)
return;
if(dfn[a]<dfn[b]) {
if(bin[b]==) {
bin[b] = ;
ans--;
}
LCR(a,fa[b]);
}
else {
if(bin[a]==) {
bin[a] = ;
ans--;
}
LCR(fa[a],b);
}
} int main()
{
//freopen("in.txt","r",stdin);
int n,m;
int kase = ;
while(scanf("%d%d",&n,&m),n) { for(int i = ; i <= n; i++)
g[i].clear();
t = ;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low)); for(int i=; i < m; i++) {
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
tarjin(,-);
// printf("%d\n",ans); printf("Case %d:\n",kase++); int q;
scanf("%d",&q);
while(q--) {
int u,v;
scanf("%d%d",&u,&v);
LCR(u,v);
printf("%d\n",ans);
}
puts(""); }
return ;
}

POJ 3694 无向图的桥的更多相关文章

  1. Network POJ - 3694 无向图找桥

    题意: 给你一个无向图,你需要找出来其中有几个桥 桥: 1.存在重边必定不为桥 2.low[v]>dfn[u] 代码: //题意很清晰 //就是这个需要先找出来原无向图中的桥个数,然后在判断添加 ...

  2. poj 3694 无向图求桥+lca

    题意抽象为: 给一个无向图和一些询问 对于每一次询问: 每次询问都会在图上增加一条边 对于每一次询问输出此时图上桥的个数. 桥的定义:删除该边后原图变为多个连通块. 数据规模:点数N(1 ≤ N ≤ ...

  3. Network POJ - 3694 (LCA+tarjan+桥)

    题目链接:https://vjudge.net/problem/POJ-3694 具体思路:首先可以通过缩点的方式将整个图变成一个树,并且树的每条边是桥,但是我们可以利用dfn数组将整个图变成树,这样 ...

  4. POJ 3694 Network ——(桥 + LCA)

    题意:给n个点和m条边,再给出q条边,问每次加一条边以后剩下多少桥. 分析:这题是结合了LCA和dfn的妙用._dfn数组和dfn的意义不一样,并非访问的时间戳,_dfn表示的是被访问的顺序,而且是多 ...

  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)

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

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

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

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

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

随机推荐

  1. PIE SDK图层属性

    1. 功能简介 通过查看图层属性可以对图层的基本信息(一般信息,来源,注释,字段信息等)有所了解 ,下面就基于PIE SDK,介绍查看图层属性功能的实现. 2. 功能实现说明 2.1. 实现思路及原理 ...

  2. Merge Sorted Array II

    Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B= ...

  3. Vue如何封装多个全局过滤器到一个文件

    #### 在写vue项目时,所用的过滤器很多时,把所有的过滤器方法封装在一个文件中,然后导出,并绑定在vue实例上 1.在src下创建filters文件夹,并新建index.js文件 2. index ...

  4. kali 安装命令类

    apt-get常用命令:update – 取回更新的软件包列表信息upgrade – 进行一次升级install – 安装新的软件包(注:软件包名称是 libc6 而非 libc6.deb)remov ...

  5. Long 和 Integer

    Integer 32位 其范围为 -2^31 到 2^31-1 之间,所以最大值是 2^31-1 Long 64位

  6. lua输入函数名字符串执行函数

    str = "testA()"loadstring(str)() function testA() ------end 使用loadstring即可执行后面在xlua用了下发现不能 ...

  7. 【算法】K-Means聚类算法(k-平均或k-均值)

    1.聚类算法和分类算法的区别 a)分类 分类(Categorization or Classification)就是按照某种标准给对象贴标签(label),再根据标签来区分归类. 举例: 假如你有一堆 ...

  8. git打补丁、还原补丁

    打补丁.还原补丁 1.两个commit间的修改(包含两个commit,<r1>.<r2>表示两个提交的版本号,<r1>是最近提交) git format-patch ...

  9. tomcat常用技巧

    1. 修改Tomcat的名称 适用场景: 在测试服务器资源有限或是在本机服务器部署多套应用系统时,由于要启动多个TOMCAT服务,且TOMCAT服务没有用名称去区分,会造成维护使用上存在一定晨读的不方 ...

  10. oracle OTT 学习

    1.OTT概念 OTT 是 Object Type Translator 的缩写,对象类型转换器.它是用来将数据库中定义的类型(UDT)转换为C结构体类型的工具.借助OTT 可以用C语言调用OCI来访 ...