题目链接: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. Codeforces Round #326 (Div. 2) B. Duff in Love 分解质因数

    B. Duff in Love Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/proble ...

  2. Codeforces GYM 100114 B. Island 水题

    B. Island Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description O ...

  3. spring mvc 框架核心文档

    http://docs.spring.io/spring-data/ Parent Directory - cassandra/ 01-Apr-2014 01:50 - commons/ 29-Jan ...

  4. C 高级编程 1

    内存管理层次: 硬件层次: 内存结构管理 内核算层次: 内存映射 堆扩展 数据结构层次: 智能指针: stl :在多线程,共享内存有问题 SGI公司实现了STL ,开发了OPENGL库 语言层次:C: ...

  5. FTP服务器简易有效的访问方法

    访问FTP服务器传统的方法是使用专用的客户端程序,如CuteFTP,8UFTP等,也包括命令行的FTP客户端c:\windows\system32\ftp.exe程序. FTP服务器也有简易访问方法 ...

  6. php执行的困惑

    最近在用php语言实现各种数据结构算法排序,可以说是很蛋疼的一件事,最近遇到了一个问题,不知道是什么原因,姑且放到这里,希望能看到的人予以帮助 首先我用php写了这样一个类 class ListNod ...

  7. ie提示jquer缺少标识符,字符串或数字

    属性之间是要用","分隔的,但最后一个属性的后面在IE中是不能有的,firefox可有可无. 至于最后的";"是另外一回事了.这是Javascript的语法问题 ...

  8. 让 BAT 的 Offer 不再难拿

    随着各大公司春招的开始,很多小伙伴都行动起来了,我有幸能够加入百度并和大家分享自己的经验心得.由于我面试的都是比较大的公司,所以自然也是做了这方面的准备,因此这篇总结并不一定适合想去创业公司的同学.另 ...

  9. jq选择器 第一部分

    没有什么新意,全是从网上摘抄的,如果哪天忘了,就来查查吧. 1. id选择器(指定id元素) 将id="one"的元素背景色设置为黑色.(id选择器返单个元素) $(documen ...

  10. ASP.NET MVC 4 让数据库自动迁移

    今天实际测试了下这个方法,可以保持数据库与实体类同步,同时不会出现数据库迁移的提示.但是只能更改实体类来改变数据库,而不能改数据库来改变实体类.所以这才是Code frist,如果通过改数据库表来改动 ...