POJ 3694 无向图的桥
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
#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 无向图的桥的更多相关文章
- Network POJ - 3694 无向图找桥
题意: 给你一个无向图,你需要找出来其中有几个桥 桥: 1.存在重边必定不为桥 2.low[v]>dfn[u] 代码: //题意很清晰 //就是这个需要先找出来原无向图中的桥个数,然后在判断添加 ...
- poj 3694 无向图求桥+lca
题意抽象为: 给一个无向图和一些询问 对于每一次询问: 每次询问都会在图上增加一条边 对于每一次询问输出此时图上桥的个数. 桥的定义:删除该边后原图变为多个连通块. 数据规模:点数N(1 ≤ N ≤ ...
- Network POJ - 3694 (LCA+tarjan+桥)
题目链接:https://vjudge.net/problem/POJ-3694 具体思路:首先可以通过缩点的方式将整个图变成一个树,并且树的每条边是桥,但是我们可以利用dfn数组将整个图变成树,这样 ...
- POJ 3694 Network ——(桥 + LCA)
题意:给n个点和m条边,再给出q条边,问每次加一条边以后剩下多少桥. 分析:这题是结合了LCA和dfn的妙用._dfn数组和dfn的意义不一样,并非访问的时间戳,_dfn表示的是被访问的顺序,而且是多 ...
- 【POJ 3694】 Network(割边<桥>+LCA)
[POJ 3694] Network(割边+LCA) Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7971 ...
- POJ 3694——Network——————【连通图,LCA求桥】
Network Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- poj 3694 Network(割边+lca)
题目链接:http://poj.org/problem?id=3694 题意:一个无向图中本来有若干条桥,有Q个操作,每次加一条边(u,v),每次操作后输出桥的数目. 分析:通常的做法是:先求出该无向 ...
- Poj 3694 Network (连通图缩点+LCA+并查集)
题目链接: Poj 3694 Network 题目描述: 给出一个无向连通图,加入一系列边指定的后,问还剩下多少个桥? 解题思路: 先求出图的双连通分支,然后缩点重新建图,加入一个指定的边后,求出这条 ...
- Tarjan算法各种&RMQ& POJ 3694
关于tarjan 的思想可以在网上搜到,具体我也不太清楚,应该说自己理解也不深,下面是做题经验得到的一些模板. 其中有很多转载,包括BYVoid等,感谢让我转...望各路大神愿谅 有向图求连通分量的一 ...
随机推荐
- MongoChef
简介 开源且免费,有商业版 可自动化生成查询语句 使用 最下面的 _id 是自动生成的,手动指定 { .0, "_id" : ObjectId("58 ...
- Java基础20-构造代码块
特点: 对象一建立就运行了,而且优先于构造函数执行 作用:给对象初始化的 构造代码块和构造方法的区别: 构造方法是对应的对象进行初始化 构造代码块是给所有的对象进行统一的初始化 public clas ...
- 从指定Dictionary中移除指定值项
void Removeltems(Dictionary<int, ltem> _dicltemMap, ltem _item) { List<ltem> keys=new Li ...
- Android:Sqlitedatabase学习小结
今天刚刚学习完Sqlite数据库的基础知识,随即把学到的东西记录下来,以便随后查阅,以下是自己对Sqlite数据库的小结:1.Sqlite简介 Sqlite是一款轻型的数据库,它包含在一个 ...
- Fastjson解析多级泛型的几种方式—使用class文件来解析多级泛型
Fastjson解析多级泛型 前言 现在网上大多数是使用TypeReference 方式来解析JSON数据,这里我提供另外一种方式来解析,使用类文件进行解析,两种方式我都会给出实际代码 实例 Type ...
- unity消息队列
解决一些当一些消息事件需要处理时,但是 相应的系统还没有初始化来解决的问题 每个系统执行层也有一个消息队列,这样系统没有做好初始化,不执行就好了. 参考:http://blog.csdn.net/ws ...
- emacs使用笔记
C-h t tutorial [移动基本操作]C-f C-b C-p C-n 前后上下 C-v C-a 行首 C-e行尾C-a 和 C-e 可以将光标移动到"一行"的头部和尾部.M ...
- BJFU 1549 ——Candy——————【想法题】
Candy 时间限制(C/C++):1000MS/3000MS 运行内存限制:65536KByte总提交:40 测试通过:20 描述 There are N c ...
- js 中标签的增删 方法
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- SpringSecurity 3.2入门(6)简单介绍默认使用的十一个过滤器
Security提供了20多个filter,每个过滤器都提供特定的功能.这些filter在Spring Security filter过滤器链中的缺省顺序由 org.springframework.s ...