http://poj.org/problem?id=3694

这一题  为什么要找最小祖先呢

当两个节点连到一块的时候  找最小公共节点就相当于找强连通分支

再找最小公共节点的过程中直到找到  这个过程中所有的点就是一个强连通分支

现在要求桥   只需用没有加边的时候的桥数减去后来找到的强连通分支里的桥数就得到加边后的桥数

Network
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 7720   Accepted: 2823

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
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector> using namespace std;
#define N 200000 int low[N],dfn[N],n,fa[N],Stack[N],bridge[N];
int Time,top,ans;
vector<vector <int> >G; void Inn()
{
G.clear();
G.resize(n+);
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(fa,,sizeof(fa));
memset(bridge,,sizeof(bridge));
memset(Stack,,sizeof(Stack));
Time=top=ans=;
} void Tarjin(int u,int f)
{
dfn[u]=low[u]=++Time;
fa[u]=f;
int len=G[u].size(),v;
for(int i=; i<len; i++)
{
v=G[u][i];
if(!dfn[v])
{
Tarjin(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u])
{
bridge[v]++;
ans++;
}
}
else if(v!=f)
low[u]=min(low[u],dfn[v]);
}
}
void LCA(int a,int b)
{
if(a==b)
return;
if(dfn[a]>dfn[b])
{
int v=fa[a];
if(bridge[a]>)
{
bridge[a]=;
ans--;
}
LCA(v,b);
}
else
{
int v=fa[b];
if(bridge[b]>)
{
bridge[b]=;
ans--;
}
LCA(a,v);
}
} int main()
{
int m,a,b,q,i,t=;
while(scanf("%d %d",&n,&m),n+m)
{
Inn();
for(i=; i<=m; i++)
{
scanf("%d %d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
Tarjin(,);
scanf("%d",&q);
printf("Case %d:\n",t++);
while(q--)
{
scanf("%d %d",&a,&b);
LCA(a,b);
printf("%d\n",ans);
}
}
return ;
}

Network-POJ3694(最小公共祖先LCA+Tarjin)的更多相关文章

  1. Luogu 2245 星际导航(最小生成树,最近公共祖先LCA,并查集)

    Luogu 2245 星际导航(最小生成树,最近公共祖先LCA,并查集) Description sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为 ...

  2. 近期公共祖先(LCA)——离线Tarjan算法+并查集优化

    一. 离线Tarjan算法 LCA问题(lowest common ancestors):在一个有根树T中.两个节点和 e&sig=3136f1d5fcf75709d9ac882bd8cfe0 ...

  3. 51.Lowest Common Ancestor of a Binary Tree(二叉树的最小公共祖先)

    Level:   Medium 题目描述: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes ...

  4. 【lhyaaa】最近公共祖先LCA——倍增!!!

    高级的算法——倍增!!! 根据LCA的定义,我们可以知道假如有两个节点x和y,则LCA(x,y)是 x 到根的路 径与 y 到根的路径的交汇点,同时也是 x 和 y 之间所有路径中深度最小的节 点,所 ...

  5. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

  6. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  7. [模板] 最近公共祖先/lca

    简介 最近公共祖先 \(lca(a,b)\) 指的是a到根的路径和b到n的路径的深度最大的公共点. 定理. 以 \(r\) 为根的树上的路径 \((a,b) = (r,a) + (r,b) - 2 * ...

  8. [leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先

    如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的. 如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能 ...

  9. 最近公共祖先(LCA)的三种求解方法

    转载来自:https://blog.andrewei.info/2015/10/08/e6-9c-80-e8-bf-91-e5-85-ac-e5-85-b1-e7-a5-96-e5-85-88lca- ...

随机推荐

  1. Objective -C Memory Management 内存管理 第一部分

    Objective -C Memory Management  内存管理  第一部分 Memory management is part of a more general problem in pr ...

  2. sql把两值之和当作条件进行查询

    目的:把表中两个字段之和作为where条件进行过滤查询 //查询在没有过期的记录select a,b from test where a+b>now();// a:存入时间 b:有效期时间段 进 ...

  3. Vue 路由知识三(过渡动画及路由钩子函数)

    路由的过渡动画:让路由有过渡动画,需要在<router-view>标签的外部添加<transition>标签,标签还需要一个name属性. <transition nam ...

  4. connection timeout 和command timeout

    每次对数据库连接时,我们有时候会碰到连接超时或者命令超时,这两个超时是不一样的.以ADO.NET为例,当客户端和服务器端连接时,碰到的超时情况主要有下面几种: ''' 当从连接池获取一个连接时,碰到超 ...

  5. 程序员容易忽略的SQL Server错误集锦

    1.大小写 大写T-SQL 语言的所有关键字都使用大写,规范要求. 2.使用“;” 使用“;”作为 Transact-SQL 语句终止符.虽然分号不是必需的,但使用它是一种好的习惯,对于合并操作MER ...

  6. Android(java)学习笔记194:ContentProvider使用之获得系统联系人信息02(掌握)

    1.重要: 系统删除一个联系人,默认情况下并不是把这个联系人直接删除掉了,只是做了一个标记,标记为被删除. 2.前面一讲说过了如何获取系统联系人信息(通过ContentProvider),获取联系人信 ...

  7. Java泛型中的协变和逆变

    Java泛型中的协变和逆变 一般我们看Java泛型好像是不支持协变或逆变的,比如前面提到的List<Object>和List<String>之间是不可变的.但当我们在Java泛 ...

  8. CAD参数绘制椭圆(com接口)

    在CAD设计时,需要绘制椭圆,用户可以设置椭圆的基本属性. 主要用到函数说明: _DMxDrawX::DrawEllipse 绘制椭圆.详细说明如下: 参数 说明 DOUBLE dCenterX 椭圆 ...

  9. 【软件构造】(转)Java中的comparable和comparator

    为了方便阅读和复习,转载至此,原地址:温布利往事的博客 阅读目录 一.Comparable简介 二.Comparator简介 三.Comparable和Comparator区别比较 回到顶部 一.Co ...

  10. 02CSS基本语法

    CSS基本语法 id选择符 在HTML文档中,需要唯一标识一个元素时,就会赋予它一个id标识,以便在对整个文档进行处理时能够很快地找到这个元素. 而id选择符就是用来对这个单一元素定义单独的样式.#号 ...