题目链接: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( ≤ N ≤ ,) and M(N -  ≤ M ≤ ,).
Each of the following M lines contains two integers A and B ( ≤ A ≠ B ≤ N), which indicates a link between computer A and B. Computers are numbered from to N. It is guaranteed that any two computers are connected in the initial network.
The next line contains a single integer Q ( ≤ Q ≤ ,), 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 ( ≤ 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 ) 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 Sample Output Case : Case :

题目大意:有N个点,M条边,添加Q条边,问每填一条边,还有几条桥?

#include<stdio.h>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include <stack>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a))
#define mod 2147493647
#define N 100100
int low[N],dfn[N],vis[N];
int fa[N];
int n,t,ans;
int bin[N];
vector<vector<int> >Q;
void init()
{
met(low,);
met(dfn,);
t=;
Q.clear();
Q.resize(n+);
met(bin,);
met(fa,);
}
void tarjin(int u,int f)
{
low[u]=dfn[u]=++t;
fa[u]=f;
int l=Q[u].size();
for(int i=; i<l; i++)
{
int v=Q[u][i];
if(!dfn[v])
{
tarjin(v,u);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v])///开始没被查找,可能是桥
{
bin[v]++;
ans++;
}
}
else if(f!=v)
{
low[u]=min(low[u],dfn[v]);
if(dfn[u]<low[v])///被查找过,有两条路通这个点,不是桥
{
bin[v]--;
ans--;
}
}
}
}
void LCR(int a,int b)
{
if(a==b)
return ;
if(dfn[a]<dfn[b])
{
if(bin[b]==)
{
bin[b]=;
ans--;
}
LCR(a,fa[b]);
}
else
{
if(bin[a]==)
{
bin[a]=;
ans--;
}
LCR(fa[a],b);
}
}
int main()
{
int m,e,f;
int cot=,q;
while(scanf("%d %d",&n,&m),n+m)
{
init();
ans=;
for(int i=; i<m; i++)
{
scanf("%d %d",&e,&f);
Q[e].push_back(f);
Q[f].push_back(e);
}
tarjin(,-);
scanf("%d",&q);
printf("Case %d:\n",cot++);
while(q--)
{
scanf("%d %d",&e,&f);
LCR(e,f);
printf("%d\n",ans);
}
}
return ;
}

(POJ 3694) Network 求桥个数的更多相关文章

  1. poj 3694 无向图求桥+lca

    题意抽象为: 给一个无向图和一些询问 对于每一次询问: 每次询问都会在图上增加一条边 对于每一次询问输出此时图上桥的个数. 桥的定义:删除该边后原图变为多个连通块. 数据规模:点数N(1 ≤ N ≤ ...

  2. POJ 3694 Network ——(桥 + LCA)

    题意:给n个点和m条边,再给出q条边,问每次加一条边以后剩下多少桥. 分析:这题是结合了LCA和dfn的妙用._dfn数组和dfn的意义不一样,并非访问的时间戳,_dfn表示的是被访问的顺序,而且是多 ...

  3. Poj 3694 Network (连通图缩点+LCA+并查集)

    题目链接: Poj 3694 Network 题目描述: 给出一个无向连通图,加入一系列边指定的后,问还剩下多少个桥? 解题思路: 先求出图的双连通分支,然后缩点重新建图,加入一个指定的边后,求出这条 ...

  4. POJ 3694——Network——————【连通图,LCA求桥】

    Network Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  5. Network POJ - 3694 无向图找桥

    题意: 给你一个无向图,你需要找出来其中有几个桥 桥: 1.存在重边必定不为桥 2.low[v]>dfn[u] 代码: //题意很清晰 //就是这个需要先找出来原无向图中的桥个数,然后在判断添加 ...

  6. POJ 3694 无向图的桥

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10404   Accepted: 3873 Descript ...

  7. kuangbin专题 专题九 连通图 POJ 3694 Network

    题目链接:https://vjudge.net/problem/POJ-3694 题目:给定一个连通图,求桥的个数,每次查询,加入一条边,问加入这条边后还有多少个桥. 思路:tarjan + 并查集 ...

  8. POJ 3694 Network (求桥,边双连通分支缩点,lca)

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5619   Accepted: 1939 Descripti ...

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

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

随机推荐

  1. Nginx 1.4.7图片缓存服务器

    软件包版本: Nginx 1.4.7 Ngx_cache_purge-2.0 Openssl-1.0.1 Pcre-8.32 二.安装编译: a)         下载pcre-8.32.tar.gz ...

  2. 关于port的关闭——Linux

    本文出自:http://blog.csdn.net/svitter 引文出自:http://bbs.chinaunix.net/thread-775649-1-1.html 1.关闭服务 servic ...

  3. 简化版可用于多线程的logger

    logger 嘛要高效,要简单.废话不多话. GitHub 地址 https://github.com/goldli/logger 本文所说的logger使用System.Logger做为NameSp ...

  4. IOS 使用Interface Builder开发界面入门与技巧

    引言: 通过Interface Builder(简称IB)来制作界面一直是iOS开发界饱受争议的方式.主要争议的话题是不太适合团队协作开发,再就是对IB的使用比较生疏,觉得IB只能完成一些很简单的功能 ...

  5. 详细的OS X Yosemite 10.10懒人版安装教程

    永远记住一句话:难,是因为不会.先是要放宽心态,才更利于解决安装过程中这样那样的问题.多尝试多动脑,不要有过份的依赖.很多问题到解决以后,才发现是如此的简单,我装黑苹果是拿来使用的,所以我的目的是装好 ...

  6. Stupid Tower Defense

    Problem Description FSF is addicted to a stupid tower defense game. The goal of tower defense games ...

  7. Matrix multiplication hdu4920

    Problem Description Given two matrices A and B of size n×n, find the product of them. bobo hates big ...

  8. 杂乱无章之javascript(一)

    1.in 要求第一个(左边的)操作数必须是字符串类型或是可以转化成字符串类型的其他类型,而第二(右边的)操作数必须是数组或对象.只有第一个操作数的值是第二个操作数的属性名,才会返回true,否则返回f ...

  9. uva 10152 ShellSort 龟壳排序(希尔排序?)

    今天状态总是很糟,这种题目卡了一天... 是不是休息时间太少了,头脑迟钝了... 名字叫希尔排序,我还以为跟它有关,还搜索了下资料. 只要找到trick就会发现是很水的题目.只要对比下就能找到哪些是移 ...

  10. Adobe Edge Animate –地球自转动画的实现,类似flash遮罩层的效果

    Adobe Edge Animate –地球自转动画的实现,类似flash遮罩层的效果 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. 目前Edge的功能尚 ...