题目链接: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. 密码强度应用(js)

    <!-- 密码强度div --> <div id="tips" class="help-block"> <b class=&quo ...

  2. struts2源代码学习之初始化(一)

    看struts2源代码已有一段时日,从今天開始,就做一个总结吧. 首先,先看看怎么调试struts2源代码吧,主要是下面步骤: 使用Myeclipse创建一个webproject 导入struts2须 ...

  3. ssh连接阿里云一段时间不操作自动断开

    打开/etc/ssh/sshd_config 添加或修改: ClientAliveInterval 120 ClientAliveCountMax 0

  4. Build类

    在开发中 我们有时候会需要获取当前手机的系统版本来进行判断,或者需要获取一些当前手机的硬件信息. android.os.Build类中.包括了这样的一些信息.我们可以直接调用 而不需要添加任何的权限和 ...

  5. Getting started with Apache Camel--转载

    原文地址:http://www.javacodegeeks.com/2012/12/getting-started-with-apache-camel.html About Camel: Apache ...

  6. Android自定义View,高仿QQ音乐歌词滚动控件!

    最近在以QQ音乐为样板做一个手机音乐播放器,源码下篇博文放出.今天我想聊的是这个QQ音乐播放器中歌词显示控件的问题,和小伙伴们一起来探讨怎么实现这个歌词滚动的效果.OK,废话不多说,先来看看效果图: ...

  7. css笔记04:属性选择器

    1.属性选择器: 带有 title 属性的所有元素设置样式: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...

  8. 不支持关键字:metadata

    将 string sqlConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Cos ...

  9. Ubuntu环境下配置Nginx

    /etc/nginx目录文件下: drwxr-xr-x   5 root root 4096 Apr 27 12:47 ./ drwxr-xr-x 104 root root 4096 Apr 27 ...

  10. Mysql 中bitwise对效率的影响??

    一直很疑惑,这个谁可以解释一下? 我也正在了解这方面的知识!