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等,感谢让我转...望各路大神愿谅 有向图求连通分量的一 ...
随机推荐
- Oracle RAC集群删除节点
一,节点环境 [root@node1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost ...
- DP Intro - Tree DP
二叉苹果树 题目 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点 ...
- 重拾简单的linux指令之info 【转】
info命令 可以利用该命令获取帮助 1. 语法格式:info <command> 2. 语法简述: 类似于man命令的获取帮助信息,比较于man命令更容易读.是以网页的结构来显示内容.而 ...
- 1、java线程模型
要了解多线程,先需要把java线程模型搞清楚,否则有时候很难理清楚一个问题. 硬件多线程: 物理机硬件的并发问题跟jvm中的情况有不少相似之处,物理机的并发处理方案对于虚拟机也有相当大的参考意义.在买 ...
- 工作采坑札记:4. Hadoop获取InputSplit文件信息
1. 场景 基于客户的数据处理需求,客户分发诸多小数据文件,文件每行代表一条记录信息,且每个文件以"类型_yyyyMMdd_批次号"命名.由于同一条记录可能存在于多个文件中,且处于 ...
- How to Configure Tomcat/JBoss and Apache HTTPD for Load Balancing and Failover
http://java.dzone.com/articles/how-configure-tomcatjboss-and In this post we will see how to setup a ...
- git提交代码报错 trailing whitespace的解决方法
1. git提交代码报错 trailing whitespace 禁止执行pre-commit脚本 进入到项目目录中 chmod a-x .git/hooks/pre-commit 2.git提交代码 ...
- 创建Django项目时,settings的静态文件的配置
STATICFILES_DIRS = ( os.path.join(BASE_DIR,'static'), )
- 深入理解JavaScript系列(2):揭秘命名函数表达式
前言 网上还没用发现有人对命名函数表达式进去重复深入的讨论,正因为如此,网上出现了各种各样的误解,本文将从原理和实践两个方面来探讨JavaScript关于命名函数表达式的优缺点. 简单的说,命名函数表 ...
- Table 边框合并(collapse)
border-collapse:collapse 用于表格属性, 表示表格的两边框合并为一条; <style type="text/css"> table { bord ...