poj3694 缩点边双连通分量
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 8669 | Accepted: 3175 |
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
1
0
Case 2:
2
0
/*
* Author: sweat123
* Created Time: 2016/6/22 15:00:44
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int to;
int next;
}edge[MAXN<<];
int pre[MAXN],vis[MAXN],dfn[MAXN],low[MAXN],n,m,ind,pa[MAXN];
int px[MAXN],py[MAXN],cnt,f[MAXN],num;
void add(int x,int y){
edge[ind].to = y;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void init(){
cnt = ;
num = ;
memset(f,-,sizeof(f));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
}
int find(int x){
if(x != pa[x])pa[x] = find(pa[x]);
return pa[x];
}
void dfs(int rt,int k,int fa){
dfn[rt] = low[rt] = k;
for(int i = pre[rt]; i != - ; i = edge[i].next){
int t = edge[i].to;
if(!dfn[t]){
dfs(t,k+,rt);
low[rt] = min(low[rt],low[t]);
if(low[t] > dfn[rt]){
px[cnt] = rt,py[cnt++] = t;
} else{
int fx = find(rt);
int fy = find(t);
pa[fx] = fy;
}
} else if(t != fa){
low[rt] = min(low[rt],dfn[t]);
}
}
}
int dfs2(int rt,int k){
vis[rt] = ;
if(rt == k){
return ;
}
for(int i = pre[rt]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!vis[t]){
int p = dfs2(t,k);
if(p == ){
int fx = find(rt);
int fy = find(t);
pa[fx] = fy;
return ;
}
}
}
return ;
}
int main(){
int ff = ;
while(~scanf("%d%d",&n,&m)){
if(n == && m == )break;
ind = ;
memset(pre,-,sizeof(pre));
for(int i = ; i <= m; i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y),add(y,x);
}
for(int i = ; i <= n; i++){
pa[i] = i;
}
init();
dfs(,,-);
for(int i = ; i <= n; i++){
int fx = find(i);
if(f[fx] == -)f[fx] = ++num;
f[i] = f[fx];
}
ind = ;
memset(pre,-,sizeof(pre));
for(int i = ; i < cnt; i++){
int x = f[px[i]];
int y = f[py[i]];
add(x,y),add(y,x);
}
int q;
printf("Case %d:\n",++ff);
scanf("%d",&q);
for(int i = ; i <= n; i++){
pa[i] = i;
}
while(q--){
int x,y;
scanf("%d%d",&x,&y);
x = f[x];
y = f[y];
memset(vis,,sizeof(vis));
dfs2(x,y);
int ans = ;
for(int i = ; i <= num; i++){
int fx = find(i);
if(fx == i)ans += ;
}
printf("%d\n",ans - );
}
printf("\n");
}
return ;
}
poj3694 缩点边双连通分量的更多相关文章
- POJ3694 Network(边双连通分量+缩点+LCA)
题目大概是给一张图,动态加边动态求割边数. 本想着求出边双连通分量后缩点,然后构成的树用树链剖分+线段树去维护路径上的边数和..好像好难写.. 看了别人的解法,这题有更简单的算法: 在任意两点添边,那 ...
- tarjan算法与无向图的连通性(割点,桥,双连通分量,缩点)
基本概念 给定无向连通图G = (V, E)割点:对于x∈V,从图中删去节点x以及所有与x关联的边之后,G分裂为两个或两个以上不相连的子图,则称x为割点割边(桥)若对于e∈E,从图中删去边e之后,G分 ...
- HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...
- Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】
一.基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成 ...
- POJ3177 Redundant Paths(边双连通分量+缩点)
题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...
- HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...
- (转)Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)
基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个 ...
- Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)
Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...
- [HDOJ4612]Warm up(双连通分量,缩点,树直径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 所有图论题都要往树上考虑 题意:给一张图,仅允许添加一条边,问能干掉的最多条桥有多少. 必须解决 ...
随机推荐
- HTML:图片热点 网页划区 表单
图片热点: 划出图片中的区域,做超链接,点击该区域就可以直接跳转到链接网站 <img src="../../../3.jpg" title="血精灵" u ...
- [No00001A]天天换图,百词斩到底在折腾啥
- C#手工注入辅助工具
看了某牛出版的MySql手注天书一神书,基本上解决了SQL注入上的知识点,于是打完(酱油)省赛回来通宵了一晚上写了个工具 方便语句构造SQL 联合查询 报错注入 盲注 读写 命令执行 基本都有整合 遇 ...
- 程序Bug---易错点
只有当图像与HTML文档在同一目录时,才能用相对地址显示.
- NPM 如何升级?
- toodifficult 题解
名字听起来十分厉害啊...一道lzz的提交答案题. 提答题,我们看看题目,给出一个解密程序,叫你加密. 每个点有一个加密的sample和一些要加密的文本. 从题目中我们可以得到一些信息: 加密后一般为 ...
- SQL探险
两张表,取相同字段比较 相同则显示true 否则FALSE.
- jquery 时间运算、格式化的方法扩张
/* 函数:日期 加n天 参数:n是天数 返回:n天后的日期 */ Date.prototype.addDays = Date.prototype.addDays || function (n) { ...
- BZOJ 1096 【ZJOI2007】 仓库建设
Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用.突然有一天, ...
- python 二叉树
class Node(object): def __init__(self, data=None, left=None, right=None): self.data = data self.left ...