POJ 3694——Network——————【连通图,LCA求桥】
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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 andB.
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
题目大意:n个点,m条边。q次询问,问你新加入无向边ui,vi后,图中的桥还有多少。
解题思路:求桥,标记桥,LCA过程中消去桥。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int maxn = 100100;
struct Edge{
int from,to,dist,next;
Edge(){}
Edge(int _to,int _next):to(_to),next(_next){}
}edges[maxn*4];
int dfn[maxn];
int fa[maxn],bridge[maxn],dep[maxn];
int head[maxn];
int tot, brinum;
int dfs_clock;
void init(){
tot = 0;
dfs_clock = 0;
brinum = 0;
memset(head,-1,sizeof(head));
memset(bridge,0,sizeof(bridge));
memset(dfn,0,sizeof(dfn));
}
void AddEdge(int _u,int _v){
edges[tot].to = _v;
edges[tot].next = head[_u];
head[_u] = tot++;
}
int dfs(int u,int f){
int lowu = dfn[u] = ++dfs_clock;
int child = 0;
for(int i = head[u]; i != -1; i = edges[i].next){
int v = edges[i].to;
if(!dfn[v]){
child++;
fa[v] = u;
int lowv = dfs(v,i);
lowu = min(lowv,lowu);
if(lowv > dfn[u]){ //标记桥
bridge[v] = 1;
brinum++;
}
}else if(dfn[v] < dfn[u] && (f^1) != i){
lowu = min(lowu,dfn[v]);
}
}
// low[u] = lowu;
return lowu;
}
void LCA(int u,int v){ //简化的LCA,把求LCA过程中的桥减掉
while(dfn[u] < dfn[v]){
if(bridge[v] == 1){
brinum--;
bridge[v] = 0;
}
v = fa[v];
}
while(dfn[u] > dfn[v]){
if(bridge[u] == 1){
brinum--;
bridge[u] = 0;
}
u = fa[u];
}
while( u != v ){
if(bridge[u]) {
brinum--;
bridge[u] = 0;
}
if(bridge[v]){
brinum--;
bridge[v] = 0;
}
u = fa[u];
v = fa[v];
}
}
int main(){
int n, m, q, cas = 0;
while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
int a,b;
init();
for(int i = 0; i < m; i++){
scanf("%d%d",&a,&b);
AddEdge(a,b);
AddEdge(b,a);
}
for(int i = 1; i <= n; i++){
if(!dfn[i])
dfs(i,-1);
}
printf("Case %d:\n",++cas);
scanf("%d",&q);
for(int i = 0; i < q; i++){
scanf("%d%d",&a,&b);
LCA(a,b);
printf("%d\n",brinum);
}puts("");
}
return 0;
}
POJ 3694——Network——————【连通图,LCA求桥】的更多相关文章
- Poj 3694 Network (连通图缩点+LCA+并查集)
题目链接: Poj 3694 Network 题目描述: 给出一个无向连通图,加入一系列边指定的后,问还剩下多少个桥? 解题思路: 先求出图的双连通分支,然后缩点重新建图,加入一个指定的边后,求出这条 ...
- poj 3694 Network(割边+lca)
题目链接:http://poj.org/problem?id=3694 题意:一个无向图中本来有若干条桥,有Q个操作,每次加一条边(u,v),每次操作后输出桥的数目. 分析:通常的做法是:先求出该无向 ...
- Network POJ - 3694 (连通图标求桥)
有上述两个数组定义可知:对于某点root,其有一儿子v,则有: 1. 如果dfn[root]<=low[v]此点是割点(对于dfs树的根,即最初节点需要两个儿子才是割点) 2. ...
- POJ 3694 Network (tarjan + LCA)
题目链接:http://poj.org/problem?id=3694 题意是给你一个无向图n个点,m条边,将m条边连接起来之后形成一个图,有Q个询问,问将u和v连接起来后图中还有多少个桥. 首先用t ...
- kuangbin专题 专题九 连通图 POJ 3694 Network
题目链接:https://vjudge.net/problem/POJ-3694 题目:给定一个连通图,求桥的个数,每次查询,加入一条边,问加入这条边后还有多少个桥. 思路:tarjan + 并查集 ...
- poj 3417 Network(tarjan lca)
poj 3417 Network(tarjan lca) 先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 我们设 ...
- POJ 3694 Network (求桥,边双连通分支缩点,lca)
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5619 Accepted: 1939 Descripti ...
- POJ 3694 Network(并查集缩点 + 朴素的LCA + 无向图求桥)题解
题意:给你一个无向图,有q次操作,每次连接两个点,问你每次操作后有几个桥 思路:我们先用tarjan求出所有的桥,同时我们可以用并查集缩点,fa表示缩点后的编号,还要记录每个节点父节点pre.我们知道 ...
- POJ 3694 Network(无向图求桥+重边处理+LCA)
题目大意: 给你一个无向图,然后再给你一个Q代表有Q次询问,每一次加一条边之后还有几座桥.在这里要对重边进行处理. 每次加入一条边之后,在这条搜索树上两个点的公共祖先都上所有点的桥都没了. 这里重边的 ...
随机推荐
- Sql Server中常用的6个自定义函数分享
转自:http://www.jb51.net/article/56691.htm IF OBJECT_ID('DBO.DISTINCT_STR') IS NOT NULL DROP FUNCTION ...
- requests模块demo
import urllib.request import requests from requests.auth import HTTPBasicAuth from requests.auth imp ...
- 洛谷P2526 [SHOI2001]小狗散步(二分图匹配)
题目背景 Grant喜欢带着他的小狗Pandog散步.Grant以一定的速度沿着固定路线走,该路线可能自交.Pandog喜欢游览沿途的景点,不过会在给定的N个点和主人相遇.小狗和主人同时从(X1,Y1 ...
- 洛谷P4074 [WC2013]糖果公园(莫队)
传送门 总算会树形莫队了…… 上次听说树形莫队是给树分块,实在看不懂.然后用括号序列的方法做总算能弄明白了 先说一下什么是括号序列,就是在$dfs$的时候,进入的时候记录一下,出去的时候也记录一下 拿 ...
- cuda编程知识普及
本帖经过多方整理,大多来自各路书籍<GPGPU编程技术><cuda高性能> 1 grid 和 block都可以用三元向量来表示: grid的数组元素是block blo ...
- P2245 星际导航 瓶颈路
\(\color{#0066ff}{ 题目描述 }\) sideman 做好了回到 \(\text{Gliese}\) 星球的硬件准备,但是 \(\text{sideman}\) 的导航系统还没有完全 ...
- 生成对抗网络(GAN)相关链接汇总
1.基础知识 创始人的介绍: “GANs之父”Goodfellow 38分钟视频亲授:如何完善生成对抗网络?(上) “GAN之父”Goodfellow与网友互动:关于GAN的11个问题(附视频) 进一 ...
- Python学习之路--1.0 Python概述及基本数据类型
Python是一门解释性语言,弱类型语言 python程序的两种编写方式: 1.进入cmd控制台,输入python进入编辑模式,即可直接编写python程序 2.在.朋友文件中编写python代码,通 ...
- MYSQL查询字段全部为中文的字段
在实际使用mysql的过程中,会遇到这样的问题,查询字段内容全部为中文内容的数据,对于刚用mysql的小伙伴可能就比较迷失了,不知道怎么使用,其实这个问题很简单,使用下面这个sql语句就可以了 SEL ...
- Activiti工作流开发准备(一)
一:开发工作流需要配合所画流程图以及根据流程图所生成的.bpmn文件进行开发,Activiti提供了eclipse插件,开发人员可以通过插件直接绘画出业务流程图. 二:eclipse插件安装 1.打开 ...