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 所有图论题都要往树上考虑 题意:给一张图,仅允许添加一条边,问能干掉的最多条桥有多少. 必须解决 ...
随机推荐
- BeJavaGod - 如何正确使用数据字典进行分类统一操作(一)
先说说什么是数据字典,这个玩意一般不太会解释,举个栗子吧~ 每个系统都会有用户表,性别:男(1)女(0) 另外我们做物流的会涉及到车型:卡车(1),轿车(2),挂车(3) 货物类型:危险品(1),普通 ...
- Oracle取TOP N条记录(转载)
在SQL Server里面有top关键字可以很方便的取出前N条记录,但是Oracle里面却没有top的使用,类似实现取出前N条记录的简单方法如下: 方法1:利用ROW_NUMBER函数 取出前5条记录 ...
- 修复ext4日志(jbd2)bug( Ext4 文件系统有以下 Bug)
from:http://blog.donghao.org/2013/03/20/%E4%BF%AE%E5%A4%8Dext4%E6%97%A5%E5%BF%97%EF%BC%88jbd2%EF%BC% ...
- Win10添加简体中文美式键盘的方法
在Win10中很多朋友发现没有简体中文(美式键盘)的选项,而如果使用“英语-美式键盘”作为默认输入法,有ModernApp的界面会变成英文,这十分不方便,那么有没有方可以在Win10中添加一个 简体中 ...
- Html5 Egret游戏开发 成语大挑战(四)选关界面
通过前面的开始界面基本上了解了eui的使用方法,可以简单快速的制作一个UI界面,本篇使用第二界面选关界面展示更为难一点的代码控制,来展现关卡地图的内容,请确保素材和资源完整,可以在前面的教程中找到下载 ...
- [转]ReactPHP── PHP版的Node.js
FROM : http://www.csdn.net/article/2015-10-12/2825887 摘要:ReactPHP作为Node.js的PHP版本.在实现思路,使用方法,应用场景上的确有 ...
- HTML5添加 video 视频标签后仍然无法播放的解决方法 IIS添加MIEI类型
现象:插入如下代码后仍然无法看视频(注:视频已确认为浏览器支持格式) <video controls="controls" width="500px" h ...
- C#获取文件MD5字符串
备注 哈希函数将任意长度的二进制字符串映射为固定长度的小型二进制字符串.加密哈希函数有这样一个属性:在计算不大可能找到散列为相同的值的两个不同的输入:也就是说,两组数据的哈希值仅在对应的数据也匹配时才 ...
- Linux 网络编程详解三(p2p点对点聊天)
//p2p点对点聊天多进程版--服务器(信号的使用) #include <stdio.h> #include <stdlib.h> #include <string.h& ...
- LINQ 查询表达式(C# 编程指南)
语言集成查询 (LINQ) 是一组技术的名称,这些技术建立在将查询功能直接集成到 C# 语言(以及 Visual Basic 和可能的任何其他 .NET 语言)的基础上. 借助于 LINQ,查询现在 ...