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 所有图论题都要往树上考虑 题意:给一张图,仅允许添加一条边,问能干掉的最多条桥有多少. 必须解决 ...
随机推荐
- 云计算之路-阿里云上:RDS用户的烦恼
http://www.cnblogs.com/cmt/p/3586029.html *博主注:阿里云数据库真的这么可笑?
- 在Ubuntu14.04下安装vsftp服务器
猜想在Ubuntu下搭建ftp服务器来实现windows和ubuntu下文件互传是一件很简单的事儿,但是在网上找了好几篇文章都不行,故自己在这里总结一下方法. 首先安装vsftp服务器 sudo ap ...
- 当一名黑客获得一份WebShell后,会做什么
当你获得一份webshell后,你会干嘛? 我曾获得N多webshell.什么discuz.dedecms.phpwind.phpweb.aspcms等等,甚至还包括N多自己研发的线上平台. 可是,问 ...
- tyvj[1087]sumsets
描述 正整数N可以被表示成若干2的幂次之和.例如,N = 7时,共有下列6种不同的方案:1) 1+1+1+1+1+1+12) 1+1+1+1+1+23) 1+1+1+2+24) 1+1+1+4 ...
- zkw费用流+当前弧优化
zkw费用流+当前弧优化 var o,v:..] of boolean; f,s,d,dis:..] of longint; next,p,c,w:..] of longint; i,j,k,l,y, ...
- ToolkitScriptManager, ScriptManager AjaxControlToolkit 出错
在使用ASP.NET AJAX应用时需添加ToolkitScriptManager或者ScriptManager.如果只是使用updatepanel,两者均可.如果有涉及到AJAX Control T ...
- Java设计模式之-----策略模式
首先,我们来看下策略模式的概念.一般的解释如下: 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化.(原文:The St ...
- 产品经理技能之BRD的笔记之菜鸟入门
链接:http://www.woshipm.com/pmd/178527.html?utm_source=tuicool 要学习MRD.PRD,先从BRD开始,才能做到知其然知其所以然. BRD是什么 ...
- BZOJ 1009 【HNOI2008】 GT考试
Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学A1A2...Am(0< ...
- Git开发备忘
1.在Git中,上传了中文命名的文件,但是后面想删除的时候,发现中文命名被转义了. 利用Git add是无法添加这类文件的,所以这里我们需要用到 git add -u命令,即可实现成功添加. 2.在G ...