POJ 3694 Network(Tarjan求割边+LCA)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 10969 | Accepted: 4096 |
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
Source
题目大意:
给出一张图,询问每次加边之后图中有多少割边
首先我们来一遍tarjan
这样实际上形成了一棵树
对于每次询问,我们找出它们的LCA
在往LCA走的过程中判断是否是割边,如果是就取消标记
LCA暴力就可以,父亲节点的信息可以在tarjan的过程中得到
// luogu-judger-enable-o2
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
//#define getchar() (S == T && (T = (S = BB) + fread(BB, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
//char BB[1 << 15], *S = BB, *T = BB;
using namespace std;
const int MAXN=1e6+;
inline int read()
{
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
struct node
{
int u,v,nxt;
}edge[MAXN];
int head[MAXN],num=;
inline void AddEdge(int x,int y)
{
edge[num].u=x;
edge[num].v=y;
edge[num].nxt=head[x];
head[x]=num++;
}
int N,M; int dfn[MAXN],low[MAXN],f[MAXN],deep[MAXN],tot=;
int bridge[MAXN],ans=;
void pre()
{
for(int i=;i<=N;i++) f[i]=i;
memset(head,-,sizeof(head));
num=;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(bridge,,sizeof(bridge));
tot=;
ans=;
}
void tarjan(int now,int fa)
{
dfn[now]=low[now]=++tot;
for(int i=head[now];i!=-;i=edge[i].nxt)
{
if(!dfn[edge[i].v])
{
deep[edge[i].v]=deep[now]+;
f[edge[i].v]=now;
tarjan(edge[i].v,now);
low[now]=min(low[now],low[edge[i].v]);
if(low[edge[i].v]>dfn[now])
{
bridge[edge[i].v]=;
ans++;
}
}
else if(edge[i].v!=fa) low[now]=min(low[now],dfn[edge[i].v]);
}
}
int Solve(int x,int y)
{
if(deep[x]<deep[y]) swap(x,y);
while(deep[x]!=deep[y])
{
if(bridge[x]) ans--,bridge[x]=;
x=f[x];
}
while(x!=y)
{
if(bridge[x]) ans--,bridge[x]=;
if(bridge[y]) ans--,bridge[y]=;
x=f[x];y=f[y];
}
return ans;
}
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
int QWQ=;
while(scanf("%d%d",&N,&M)!=EOF)
{
if(N==&&M==) break;
printf("Case %d:\n",++QWQ);
pre();
for(int i=;i<=M;i++)
{
int x=read(),y=read();
AddEdge(x,y);
AddEdge(y,x);
}
deep[]=;
tarjan(,);
int Q=read();
while(Q--)
{
int x=read(),y=read();
printf("%d\n",Solve(x,y));
}
putchar('\n');
}
return ;
}
POJ 3694 Network(Tarjan求割边+LCA)的更多相关文章
- poj 3694 pku 3694 Network tarjan求割边 lca
题意:给你一个连通图,然后再给你n个询问,每个询问给一个点u,v表示加上u,v之后又多少个桥.一个最容易想到的办法就是先加边找桥,加边找桥,这样可定超时.那么就可以缩点,因为如果一条边不是桥那么无论怎 ...
- 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 ...
- poj 3694 Network 边双连通+LCA
题目链接:http://poj.org/problem?id=3694 题意:n个点,m条边,给你一个连通图,然后有Q次操作,每次加入一条边(A,B),加入边后,问当前还有多少桥,输出桥的个数. 解题 ...
- POJ 3694 Network (tarjan + LCA)
题目链接:http://poj.org/problem?id=3694 题意是给你一个无向图n个点,m条边,将m条边连接起来之后形成一个图,有Q个询问,问将u和v连接起来后图中还有多少个桥. 首先用t ...
- POJ 3694 Network (求桥,边双连通分支缩点,lca)
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5619 Accepted: 1939 Descripti ...
- POJ 3694 Network(无向图求桥+重边处理+LCA)
题目大意: 给你一个无向图,然后再给你一个Q代表有Q次询问,每一次加一条边之后还有几座桥.在这里要对重边进行处理. 每次加入一条边之后,在这条搜索树上两个点的公共祖先都上所有点的桥都没了. 这里重边的 ...
- [poj 1144]Network[Tarjan求割点]
题意: 求一个图的割点. 输入略特别: 先输入图中点的总数, 接下来每一行首先给出一个点u, 之后给出一系列与这个点相连的点(个数不定). 行数也不定, 用0作为终止. 这样的输入还是要保证以数字读入 ...
- POJ 3694 Network 无向图双联通+LCA
一开始题目没看清楚,以为是增加那条边后还有多少桥,所以就当做是无向图tarjan缩点后建树,然后求u,v的最近公共祖先,一直wa. 后来再看题目后才发现边放上去后不会拿下来了,即增加i条边后桥的数量. ...
随机推荐
- dozer初探
简介 Dozer是一款javaBean的映射工具,用于解决一个类到另外一个类的自动适配功能,它即支持简单的映射,也支持复杂类型的双向递归映射(官网). 示例 举个例子,假设说我们现在有User(用户) ...
- vs的任务列表
前几天才刚开始看到这个 很多时候,一些任务,怕忘记了,或者已经做好的东西,由于各种原因,暂时不用等等这种情况 这时候,就可以在vs上,随时加到任务列表中,方面下次直接来修改或实现等等 在vs的视图&g ...
- js面向对象概念解析
ECMAScript有两种开发模式: 1.函数式(过程化) 2.面向对象(OOP). 面向对象的语言有一个标志,那就是类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.但是,ECMAScri ...
- node.js流复制文件
转自:http://segmentfault.com/a/1190000000519006 nodejs的fs模块并没有提供一个copy的方法,但我们可以很容易的实现一个,比如: var source ...
- ContentType 列表
CONTENTTYPE是html里面都带有的,ASP只是设置当前的CONTENTTYPE在浏览器中,ContentType一般指定当前的文档内容,浏览器即根据相应的MIME及Content-Type映 ...
- 「JavaSE 重新出发」05.03 反射
能够分析类能力的程序称为反射(reflection). 反射库(reflection library)提供了一个非常丰富且精心设计的工具集,以便编写能够动态操纵 Java 代码的程序. 反射机制可以用 ...
- Android回炉系列之四大组件之首Activity
有段时间没有认认真真研习过android了,android毕竟是我进这个软件开发圈子接触的第一门技术,android已经成了口头禅之类的东西了.当初学习android的时候大都是草草了 ...
- this self指针
this 和 self指针 为函数提供了运行上下问:为函数提供了当前对象的其实地址,方便函数的对对象的访问.
- Unity2D 小游戏之 RocketMouse
这个小游戏源自这里.这几天闲时捡了点 Unity(很久没有摸它了),顺手将这个小游戏移植到了 Unity5.5.0,除了 Parallax Scrolling 还有点小问题外,其它功能全部完整.部分代 ...
- UVALive-8077 Brick Walls 找规律
题目链接:https://cn.vjudge.net/problem/UVALive-8077 题意 有一个用砖头磊起来的墙,现在又有一只蚂蚁,想沿着砖缝从起点跑到终点. 问最短路长度. 思路 找规律 ...