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. opencv java小应用:比较两个图片的相似度

    package com.company; import org.opencv.core.*; import org.opencv.imgcodecs.Imgcodecs; import org.ope ...

  2. MHA 高可用架构部署

    一, MHA 介绍 MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公 ...

  3. 018-面向接口编程的BeanFactory模板代码

    1 BeanFactory工具类 package www.test.utils; import org.dom4j.Document; import org.dom4j.Element; import ...

  4. java io 学习笔记(三) 字符流读写

    1.字符流读取 字符流读取的所有类都是从Reader这个超类继承的,都是用于读取字符的,这些类分别是InputSteamReader(从字符流读取).FileReader(继承与InputStream ...

  5. 跨域策略文件crossdomain.xml文件

    使用crossdomain.xml让Flash可以跨域传输数据 一.crossdomain.xml文件的作用    跨域,顾名思义就是需要的资源不在自己的域服务器上,需要访问其他域服务器.跨域策略文件 ...

  6. .net生成cookie,读取cookie,创建特性

    1.登录或注册成功时生成票据和cookie 注释:__JZY_Common_User_Login_Cookie__:就是为了判断当前登录人(例如:管理员和普通用户,可以定义两个常量,可有可无) 2.读 ...

  7. Windows进程间通信--命名管道

    1 相关概述 命名管道(Named Pipes)是一种简单的进程间通信(IPC)机制.命名管道可以在同一台计算机的不同进程之间,或者跨越一个网络的不同计算机的不同进程之间的可靠的双向或单向的数据通信. ...

  8. js之箭头函数

    原文 ES6标准新增了一种新的函数:Arrow Function(箭头函数). 为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: ...

  9. sublime常用设置

    原文地址 https://segmentfault.com/a/1190000002596724 前言 Sublime Text3 在文中简称为ST. ST是个不错的编辑器,我用了有段时间了,所以我觉 ...

  10. 位运算(5)——Power of Two

    判断一个整数是不是2的幂. 关键是弄明白2的幂的二进制形式只有一个1. public class Solution { public boolean isPowerOfTwo(int n) { int ...