POJ3694(求割边)
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(求割边)的更多相关文章
- [学习笔记]tarjan求割边
上午打模拟赛的时候想出了第三题题解,可是我不会求割边只能暴力判割边了QAQ 所以,本文介绍求割边(又称桥). 的定义同求有向图强连通分量. 枚举当前点的所有邻接点: 1.如果某个邻接点未被访问过,则访 ...
- 【NOIP训练】【Tarjan求割边】上学
题目描述 给你一张图,询问当删去某一条边时,起点到终点最短路是否改变. 输入格式 第一行输入两个正整数,分别表示点数和边数.第二行输入两个正整数,起点标号为,终点标号为.接下来行,每行三个整数,表示有 ...
- ZOJ 2588 Burning Bridges (tarjan求割边)
题目链接 题意 : N个点M条边,允许有重边,让你求出割边的数目以及每条割边的编号(编号是输入顺序从1到M). 思路 :tarjan求割边,对于除重边以为中生成树的边(u,v),若满足dfn[u] & ...
- ZOJ Problem - 2588 Burning Bridges tarjan算法求割边
题意:求无向图的割边. 思路:tarjan算法求割边,访问到一个点,如果这个点的low值比它的dfn值大,它就是割边,直接ans++(之所以可以直接ans++,是因为他与割点不同,每条边只访问了一遍) ...
- HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】
Caocao's Bridges Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- tarjan求割边割点
tarjan求割边割点 内容及代码来自http://m.blog.csdn.net/article/details?id=51984469 割边:在连通图中,删除了连通图的某条边后,图不再连通.这样的 ...
- ZOJ 2588 求割边问题
题目链接:http://vjudge.net/problem/viewProblem.action?id=14877 题目大意: 要尽可能多的烧毁桥,另外还要保证图的连通性,问哪些桥是绝对不能烧毁的 ...
- 洛谷P1656 炸铁路 (求割边)
用tarjan变种求割边的模板题 其实还可以求出所有的边双(用栈),但本题不需要求. 1 #include<bits/stdc++.h> 2 using namespace std; 3 ...
- hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割
view code//hdu 3987 #include <iostream> #include <cstdio> #include <algorithm> #in ...
随机推荐
- 【转】【Pycharm大全】
感谢:陈俊岭的程序员之路 [Pycharm大全]:http://blog.csdn.net/u013088062/article/details/50388329
- 【文献阅读】Augmenting Supervised Neural Networks with Unsupervised Objectives-ICML-2016
一.Abstract 从近期对unsupervised learning 的研究得到启发,在large-scale setting 上,本文把unsupervised learning 与superv ...
- ios math.h 常用数学函数
1. 三角函数 double sin (double);正弦 double cos (double);余弦 double tan (double);正切 2 .反三角函数 double as ...
- 引用变量的类型强转以及InstanceOf方法的使用
引用到的类: class Person{ String name; } class Student extends Person{ String sut_no; } class ClassMate e ...
- 九度OJ 1078:二叉树遍历 (二叉树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3748 解决:2263 题目描述: 二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其右子树 ...
- cmake的外部编译
1 什么是外部编译 就是让源码文件和cmake生成的工程文件分开,将cmake生成的工程文件放在一个单独的目录下面. 2 怎样进行外部编译 第一,单独建立一个目录,这个目录在source code目录 ...
- Nginx Cache中$request_filename(转)
对于Nginx的$request_filename变量指的就是请求的资源路径.在原先 OpenCDN节点端配置里面是这样的. location ~ .*\.(png|html|htm|ico|jpg| ...
- Java中Iterator的fast-fail分析
1.fail-fast简介 fail-fast机制是java集合(Collection)中的一个错误机制.当多个线程对同一个集合的内容进行操作时,就可能会产生fail-fast事件. 例如:当某一个线 ...
- java基础之容器、集合、集合常用方法
一.容器(Collection):数组是一种容器,集合也是一种容器 java编程中,装其他各种各样的对象(引用类型)的一种东西,叫容器 注意: 1.数组的长度是固定的 2.集合:长度不固定, 可以随时 ...
- SlopeOne推荐算法
Slope One 算法 是一种基于评分的预测算法, 本质上也是一种基于项目的算法.与一般的基于项目的算法不同, 该算法不计算项目之间的相似度, 而是用一种简单的线性回归模型进行预测(可 ...