Network

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 13172   Accepted: 4774

题目链接:http://poj.org/problem?id=3694

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

题意:

首先给出一个无向图,然后不断加边,每次加一条边就输出当前图中桥有多少个。

题解:

首先单独计算桥很容易,但这个加边操作有点烦人,不可能每次加条边就求次桥吧。然后我们主要想的就是新边和原图的关系。

因为原图是连通的,在原图中,我们很容易把桥求出来,并且将相应的点进行缩点(这里我用的并查集),最后的图中的边都为桥,且无向图变成了树。

那么每次新加入一条边,如果它连接的为不在一个集合中的点,那么必然会影响到从u到v简单路径上面的桥;否则就不影响。

下面关键就是求这个简单路径,由于这个题数据量较小,用个朴素的lca就行了,这里的lca没有用深度来,而是根据dfn,很好地利用了时间戳。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 1e5+,M = 2e5+;
int n,m,ans;
int head[N];
struct Edge{
int u,v,next;
}e[M<<];
int T,tot;
int dfn[N],low[N],cut[N],f[N],pa[N];
void adde(int u,int v){
e[tot].u=u;e[tot].v=v;e[tot].next=head[u];head[u]=tot++;
}
void init(){
T=;tot=;ans=;
memset(head,-,sizeof(head));
memset(cut,,sizeof(cut));
memset(dfn,,sizeof(dfn));
memset(pa,,sizeof(pa));
for(int i=;i<=n;i++) f[i]=i;
}
int find(int x){
return f[x]==x ? x : f[x]=find(f[x]);
}
void Union(int u,int v){
int fx=find(u),fy=find(v);
if(fx!=fy) f[fx]=fy;
return ;
}
void Tarjan(int u,int pre){
dfn[u]=low[u]=++T;
int son=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(v==pre) continue ;
if(!dfn[v]){
pa[v]=u;
Tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u]){
cut[v]=;
ans++;
}else Union(u,v);
}else{
low[u]=min(low[u],dfn[v]);
}
}
}
int lca(int u,int v){
if(dfn[u]<dfn[v]) swap(u,v);
while(dfn[u]>dfn[v]){
int fx=find(u),fy=find(pa[u]);
if(fx!=fy){
ans--;
f[fx]=fy;
}
u=pa[u];
}
while(dfn[v]>dfn[u]){
int fx=find(v),fy=find(pa[v]);
if(fx!=fy){
ans--;
f[fx]=fy;
}
v=pa[v];
}
return ans ;
}
int main(){
int cnt = ;
while(scanf("%d%d",&n,&m)!=EOF){
if(n+m<=) break ;
cnt++;
init();
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
adde(u,v);adde(v,u);
}
Tarjan(,-);
int q;
printf("Case %d:\n",cnt);
scanf("%d",&q);
while(q--){
int u,v;
scanf("%d%d",&u,&v);
printf("%d\n",lca(u,v));
}
}
return ;
}

POJ3694:Network(并查集+缩点+lca)的更多相关文章

  1. POJ 3694 Network(并查集缩点 + 朴素的LCA + 无向图求桥)题解

    题意:给你一个无向图,有q次操作,每次连接两个点,问你每次操作后有几个桥 思路:我们先用tarjan求出所有的桥,同时我们可以用并查集缩点,fa表示缩点后的编号,还要记录每个节点父节点pre.我们知道 ...

  2. 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D

    目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...

  3. POJ 2236 Wireless Network (并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 761 ...

  4. BestCoder冠军赛 - 1009 Exploration 【Tarjan+并查集缩点】

    [题意] 给一个图,这个图中既有有向边,又有无向边,每条边只能走一次,问图中是否存在环. 最多10^6个点,10^6个无向边,10^6个有向边 [题解] 因为既有有向边又有无向边,所以不能单纯的用ta ...

  5. [LA] 3027 - Corporative Network [并查集]

    A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...

  6. LA 3027 Corporative Network 并查集记录点到根的距离

    Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [S ...

  7. POJ2236 Wireless Network 并查集简单应用

    Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have ...

  8. hdu1811 拓扑排序+并查集缩点

    /*给定两个点之间的三种关系 = < >如果是=就将两点放到同一个集合里进行缩点 离线处理所有关系,先用并查集将等于关系缩成一个点 */ #include<bits/stdc++.h ...

  9. Wireless Network 并查集

    An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...

随机推荐

  1. python基本数据类型——集合

    集合 无序可变序列,集合中元素不允许重复,即每个元素都是唯一的 集合中的元素按照升序排列 # 创建集合 >>aset = set([0,2,4,5,7,2,3,5,9,0]) >&g ...

  2. struts2源码分析-初始化流程

    这一篇文章主要是记录struts.xml的初始化,还原struts2.xml的初始化流程.源码依据struts2-2.3.16.3版本. struts2初始化入口,位于web.xml中: <fi ...

  3. 剑指offer-二叉树搜索树与双向链表25

    题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. class Solution: def Convert(self, pRo ...

  4. MyBatis 注解配置及动态SQL

      一.注解配置 目前MyBatis支持注解配置,用注解方式来替代映射文件,但是注解配置还是有点不完善,在开发中使用比较少,大部分的企业还是在用映射文件来进行配置.不完善的地方体现在于当数据表中的字段 ...

  5. [精通Python自然语言处理] Ch1 - 将句子切分为单词

    实验对比了一下三种切分方式: 1,2 : nltk.word_tokenize :  分离缩略词,(“Don't” =>'Do', "n't") 表句子切分的“,” &quo ...

  6. Windows下PATH等环境变量详解(转载)

    本文转载自http://legend2011.blog.51cto.com/3018495/553255 在学习JAVA的过程中,涉及到多个环境变量(environment variable)的概念, ...

  7. 六: Image Viewer 离线镜像查看器

    参考:http://hadoop.apache.org/docs/r2.6.3/hadoop-project-dist/hadoop-hdfs/HdfsImageViewer.html   离线镜像查 ...

  8. StrBlobPtr类——weak_ptr访问vector元素

    #include <iostream> #include <memory> #include <string> #include <initializer_l ...

  9. 【IdentityServer4文档】- 支持和咨询选项

    支持和咨询选项 我们为 IdentityServer 提供多个免费和商业支持及咨询选项. 免费支持 免费支持是基于社区的,而且使用的是公共论坛 StackOverflow 有越来越多的使用 Ident ...

  10. Median of Two Sorted Arrays(hard)

    题目要求: 有两个排序的数组nums1和nums2分别为m和n大小. 找到两个排序数组的中位数.整体运行时间复杂度应为O(log(m + n)). 示例: 我的方法: 分别逐个读取两个数组的数,放到一 ...