(POJ 3694) Network 求桥个数
题目链接: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 求桥个数的更多相关文章
- poj 3694 无向图求桥+lca
题意抽象为: 给一个无向图和一些询问 对于每一次询问: 每次询问都会在图上增加一条边 对于每一次询问输出此时图上桥的个数. 桥的定义:删除该边后原图变为多个连通块. 数据规模:点数N(1 ≤ N ≤ ...
- POJ 3694 Network ——(桥 + LCA)
题意:给n个点和m条边,再给出q条边,问每次加一条边以后剩下多少桥. 分析:这题是结合了LCA和dfn的妙用._dfn数组和dfn的意义不一样,并非访问的时间戳,_dfn表示的是被访问的顺序,而且是多 ...
- Poj 3694 Network (连通图缩点+LCA+并查集)
题目链接: Poj 3694 Network 题目描述: 给出一个无向连通图,加入一系列边指定的后,问还剩下多少个桥? 解题思路: 先求出图的双连通分支,然后缩点重新建图,加入一个指定的边后,求出这条 ...
- POJ 3694——Network——————【连通图,LCA求桥】
Network Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- Network POJ - 3694 无向图找桥
题意: 给你一个无向图,你需要找出来其中有几个桥 桥: 1.存在重边必定不为桥 2.low[v]>dfn[u] 代码: //题意很清晰 //就是这个需要先找出来原无向图中的桥个数,然后在判断添加 ...
- POJ 3694 无向图的桥
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10404 Accepted: 3873 Descript ...
- kuangbin专题 专题九 连通图 POJ 3694 Network
题目链接:https://vjudge.net/problem/POJ-3694 题目:给定一个连通图,求桥的个数,每次查询,加入一条边,问加入这条边后还有多少个桥. 思路:tarjan + 并查集 ...
- 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.我们知道 ...
随机推荐
- cuda-convnet 卷积神经网络 一般性结构卷积核个数 和 输入输出的关系以及输入输出的个数的说明:
卷积神经网络 一般性结构卷积核个数和 输入输出的关系以及输入输出的个数的说明: 以cifar-10为例: Initialized data layer 'data', producing3072 ou ...
- pt-online-schema-change使用说明、限制与比较
http://seanlook.com/2016/05/27/mysql-pt-online-schema-change/ http://blog.itpub.net/22664653/viewspa ...
- Playing with ptrace, Part I
X86_64 的 Redhat / Centos / Scientific 下面,若要编译.运行32位程序,需要安装以下包: yum install libgcc.i686 yum install g ...
- Exploring Python Code Objects
Exploring Python Code Objects https://late.am/post/2012/03/26/exploring-python-code-objects.html Ins ...
- subline的安装
简单的安装方法 使用Ctrl+`快捷键或者通过View->Show Console菜单打开命令行,粘贴如下代码: import urllib.request,os; pf = 'Package ...
- vi/vim使用进阶: 剑不离手 – quickfix
转载:http://easwy.com/blog/archives/advanced-vim-skills-quickfix-mode/ 本节所用命令的帮助入口: :help quickfix :he ...
- E - 最短的名字
Description 在一个奇怪的村子中,很多人的名字都很长,比如aaaaa, bbb and abababab. 名字这么长,叫全名显然起来很不方便.所以村民之间一般只叫名字的前缀.比如叫'aaa ...
- TensorFlow学习之运行label_image实例
前段时间,搞了搞编译label_image中cc的实例,最后终于搞定...但想在IDE中编译还没成功,继续摸索中. 现分享一下,探究过程,欢迎叨扰,交流. 个人地址:http://home.cnblo ...
- Java操作图片的工具类
操作图片的工具类: import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.a ...
- Scala中的类和对象
类的定义 使用class定义 类的字段 在类中使用var,val定义字段 类的方法 scala中,使用var定义字段默认提供setter和getter方法对应名称为 value_= 和value /* ...