Network
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 7943   Accepted: 2893

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 题意:给定结点和边确定一幅无向图,然后增加Q条边,输出每增加一条边之后图中的割边数目。
思路:先利用tarjan求割边。新增加的一条边两端的结点u、v,u和v到它们的LCA之间的割边全部消失。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=;
struct Edge{
int to,net;
}es[MAXN*];
int head[MAXN],tot;
void addedge(int u,int v)
{
es[tot].to=v;
es[tot].net=head[u];
head[u]=tot++;
} int n,m,q;
int dfn[MAXN],low[MAXN],key;
bool bridge[MAXN];
int par[MAXN],depth[MAXN];
int cnt;
void tarjan(int u,int fa,int dep)
{
par[u]=fa;
depth[u]=dep;
dfn[u]=++key;
low[u]=key;
for(int i=head[u];i!=-;i=es[i].net)
{
int to=es[i].to;
if(!dfn[to])
{
tarjan(to,u,dep+);
low[u]=min(low[u],low[to]);
if(dfn[u]<low[to])
{
bridge[to]=true;
cnt++;
}
}
else if(to!=fa) low[u]=min(low[u],dfn[to]);
}
} void query(int u,int v)
{
if(depth[u]>depth[v]) swap(u,v);
while(depth[v]>depth[u])
{
if(bridge[v])
{
bridge[v]=false;
cnt--;
}
v=par[v];
}
while(u!=v)
{
if(bridge[u])
{
bridge[u]=false;
cnt--;
}
u=par[u]; if(bridge[v])
{
bridge[v]=false;
cnt--;
}
v=par[v];
}
} int main()
{
int cas=;
while(scanf("%d%d",&n,&m)!=EOF&&(n+m)!=)
{
cnt=;
memset(head,-,sizeof(head));
key=;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(bridge,false,sizeof(bridge));
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
tarjan(,,);
scanf("%d",&q);
printf("Case %d:\n",++cas);
while(q--)
{
int u,v;
scanf("%d%d",&u,&v);
query(u,v);
printf("%d\n",cnt);
}
printf("\n");
}
return ;
}

POJ3694(求割边)的更多相关文章

  1. [学习笔记]tarjan求割边

    上午打模拟赛的时候想出了第三题题解,可是我不会求割边只能暴力判割边了QAQ 所以,本文介绍求割边(又称桥). 的定义同求有向图强连通分量. 枚举当前点的所有邻接点: 1.如果某个邻接点未被访问过,则访 ...

  2. 【NOIP训练】【Tarjan求割边】上学

    题目描述 给你一张图,询问当删去某一条边时,起点到终点最短路是否改变. 输入格式 第一行输入两个正整数,分别表示点数和边数.第二行输入两个正整数,起点标号为,终点标号为.接下来行,每行三个整数,表示有 ...

  3. ZOJ 2588 Burning Bridges (tarjan求割边)

    题目链接 题意 : N个点M条边,允许有重边,让你求出割边的数目以及每条割边的编号(编号是输入顺序从1到M). 思路 :tarjan求割边,对于除重边以为中生成树的边(u,v),若满足dfn[u] & ...

  4. ZOJ Problem - 2588 Burning Bridges tarjan算法求割边

    题意:求无向图的割边. 思路:tarjan算法求割边,访问到一个点,如果这个点的low值比它的dfn值大,它就是割边,直接ans++(之所以可以直接ans++,是因为他与割点不同,每条边只访问了一遍) ...

  5. HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】

     Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  6. tarjan求割边割点

    tarjan求割边割点 内容及代码来自http://m.blog.csdn.net/article/details?id=51984469 割边:在连通图中,删除了连通图的某条边后,图不再连通.这样的 ...

  7. ZOJ 2588 求割边问题

    题目链接:http://vjudge.net/problem/viewProblem.action?id=14877 题目大意: 要尽可能多的烧毁桥,另外还要保证图的连通性,问哪些桥是绝对不能烧毁的 ...

  8. 洛谷P1656 炸铁路 (求割边)

    用tarjan变种求割边的模板题 其实还可以求出所有的边双(用栈),但本题不需要求. 1 #include<bits/stdc++.h> 2 using namespace std; 3 ...

  9. hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割

    view code//hdu 3987 #include <iostream> #include <cstdio> #include <algorithm> #in ...

随机推荐

  1. .NET C# 【小技巧】控制台程序,运行是否弹出窗口选择!

    选中控制台程序项目,右键→属性→应用程序栏→输出类型: 1.Windows 应用程序(不弹出提示框)! 2.控制台应用程序(弹出提示框)! 3.类库(类库生成dll,是不能直接运行的,类库供应用程序调 ...

  2. PHP和mysql的长连接

    关于 PHP MySQL 长连接.连接池的一些探索 PHP连接MySQL的方式,用的多的是mysql扩展.mysqli扩展.pdo_mysql扩展,是官方提供的.php的运行机制是页面执行完会释放所有 ...

  3. 【BZOJ3997】[TJOI2015]组合数学 最长反链

    [BZOJ3997][TJOI2015]组合数学 Description 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格 ...

  4. 九度OJ 1012:畅通工程 (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7052 解决:3034 题目描述: 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工 ...

  5. cmake是什么

    1 cmake是什么 cmake是一个管理软件build过程的工具.它并不会直接build处软件可执行文件本身,而是build出可以build出软件本身的全部工程文件,比如makefiles.xcod ...

  6. Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fromHere.com' is therefore not allowed access.

    Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is presen ...

  7. Hadoop-2.2.0中文文档—— MapReduce 下一代--容量调度器

    目的 这份文档描写叙述 CapacityScheduler,一个为Hadoop能同意多用户安全地共享一个大集群的插件式调度器,如他们的应用能适时被分配限制的容量. 概述 CapacitySchedul ...

  8. JavaScript toFixed() 方法注意点

    定义和用法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 语法 NumberObject.toFixed(num) 参数 描述 num 必需.规定小数的位数,是 0 ~ ...

  9. ABAP alv report

    *&---------------------------------------------------------------------* *& Report  YTST_FF_ ...

  10. Java AQS详解(转)

    原文地址 一.概述 谈到并发,不得不谈ReentrantLock:而谈到ReentrantLock,不得不谈AbstractQueuedSynchronizer(AQS)! 类如其名,抽象的队列式的同 ...