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. 升级TeeChart pro

    teechart 安装流程如下: 1.  将生成的 LIB中的 选中文件copy到C:\Users\Public\Documents\RAD Studio\8.0\Dcp 图1 1.  fastrep ...

  2. vue 浏览器顶部有载入(进度)动画插件vue-progressbar

    1.安装 npm install --save nprogress 2.在main.js中引入 import NProgress from "nprogress" import & ...

  3. QiyeProject SpringMVC 项目 d15866p148.iok.la 主要做主页应用,消息应用不管了 用户微信号有点像乱码的那个是openID 找同伴:在项目的GitHub页面里找提问过的人,还有fork,star的人

    消息型应用支持文本.图片.语音.视频.文件.图文等消息类型. 主页型应用只支持文本消息类型,且文本长度不超过20个字. 填写必要信息 URL /QiyeProject/src/org/oms/qiye ...

  4. 如何实现一个简单的MVVM框架

    接触过web开发的同学想必都接触过MVVM,业界著名的MVVM框架就有AngelaJS.今天闲来无事,决定自己实现一个简单的MVVM框架玩一玩.所谓简单,就是仅仅实现一个骨架,仅表其意,不摹其形. 分 ...

  5. go语言解析网页利器goquery使用教程(爬虫必备)

    某些时候需要爬取网页中指定信息时,通常需要一些框架解析网页行成dom模型,然后来操作节点来获取相应的信息.在java中很显然就是Jsoup,而在Golang里,应该就是这个goquery了吧. goq ...

  6. Struts2中Action对象的set方法和get方法调用规则

    Struts的Action是采用的是多实例多线程设计,而不是像Servlet那样采用单实例多线程设计,因此在struts中,一个请求就对应一个Action对象,个对象之间的数据相互之间互不干扰.没接到 ...

  7. html结构内容拾忆

    文本格式化: <b>This text is bold</b><!--定义粗体文本.--> <strong>This text is strong< ...

  8. 分支结构case……end

    语法: case when 条件1 then 结果1 when 条件2 then 结果2 ……….. else 其它结果 end 执行顺序: 条件1成立执行结果1 条件2成立执行结果2 如果所有的wh ...

  9. C# 判断List集合中是否有重复的项

    /*在.Net 3.5 以上*/ ).Count() >= ;

  10. 关于Java中用Double型运算时精度丢失的问题

    注:转自 https://blog.csdn.net/bleach_kids/article/details/49129943 在使用Java,double 进行运算时,经常出现精度丢失的问题,总是在 ...