poj3694 连通无向图图加边后有多少桥
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 10261 | Accepted: 3807 |
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
我怎么能这么菜
因为会有如下图
1-2
2-3
3-4
4-5
5-6
6-7
7-4
5-8
8-1
那么实际上1.2.3.4.5.8的low值为1
而6.7的low值为4,如果下面标有必须这么写的地方换成low[v]>low[u]就会造成误判
这份代码有个bug
数据 3 3
1 2
2 1
2 3
1
3 2
可以看出他不能判断消除的话要记录一下反向边,然后强行将flag搞成0就可以了吧 因为没有更严格的数据评测也无法判断
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int head[N],dfn[N],low[N],dep[N],fa[N];
int tot,cnt,ans,n,m;
bool flag[N];
struct node{
int to,next;
}e[N<<];
void init(){
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(flag,,sizeof(flag));
fa[]=;
tot=cnt=ans=;
}
void add(int u,int v){
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
void Tajan(int u,int pre){
dep[u]=dep[pre]+;
dfn[u]=low[u]=++cnt;
for(int i=head[u];i+;i=e[i].next){
int v=e[i].to;
if(v==pre) continue;
if(!dfn[v]) {
fa[v]=u;
Tajan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u]) //必须这么写
{
flag[v]=;
++ans;
}
}
else low[u]=min(low[u],dfn[v]);
}
}
void LCA(int a,int b){
if(dep[a]<dep[b]) swap(a,b);
while(dep[a]>dep[b]){
if(flag[a]) --ans,flag[a]=;
a=fa[a];
}
while(a!=b){
if(flag[a]) --ans,flag[a]=;
if(flag[b]) --ans,flag[b]=;
a=fa[a],b=fa[b];
}
}
int main(){
int a,b,T=;
while(scanf("%d%d",&n,&m),n+m){
init();
while(m--){
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
Tajan(,);
scanf("%d",&m);
printf("Case %d:\n",T++);
while(m--){
scanf("%d%d",&a,&b);
if(ans) LCA(a,b);
printf("%d\n",ans);
}
puts("");
}
}
poj3694 连通无向图图加边后有多少桥的更多相关文章
- POJ 1144 Network(无向图的割顶和桥模板题)
http://poj.org/problem?id=1144 题意: 给出图,求割点数. 思路: 关于无向图的割顶和桥,这篇博客写的挺不错,有不懂的可以去看一下http://blog.csdn.net ...
- tarjan算法与无向图的连通性(割点,桥,双连通分量,缩点)
基本概念 给定无向连通图G = (V, E)割点:对于x∈V,从图中删去节点x以及所有与x关联的边之后,G分裂为两个或两个以上不相连的子图,则称x为割点割边(桥)若对于e∈E,从图中删去边e之后,G分 ...
- DFS的运用(二分图判定、无向图的割顶和桥,双连通分量,有向图的强连通分量)
一.dfs框架: vector<int>G[maxn]; //存图 int vis[maxn]; //节点访问标记 void dfs(int u) { vis[u] = ; PREVISI ...
- POJ3694 Network(Tarjan双联通分图 LCA 桥)
链接:http://poj.org/problem?id=3694 题意:给定一个有向连通图,每次增加一条边,求剩下的桥的数量. 思路: 给定一个无向连通图,添加一条u->v的边,求此边对图剩余 ...
- 无向图求割(找桥)tarjan
本博客参考了李煜东的<算法竞赛进阶指南>,大家要是觉得这篇文章写的不错请大家支持正版.豆瓣图书 我在之前的博客中讲解了搜索序时间戳,这次我们讲讲追溯值的概念. 追溯值: 设subtree( ...
- 无向图求割点(找桥)tarjan
本博客参考了李煜东的<算法竞赛进阶指南>,大家要是觉得这篇文章写的不错请大家支持正版.豆瓣图书 我在之前的博客中讲解了搜索序时间戳,这次我们讲讲追溯值的概念. 追溯值: 设subtree( ...
- 图论之tarjan真乃神人也,强连通分量,割点,桥,双连通他都会
先来%一下Robert Tarjan前辈 %%%%%%%%%%%%%%%%%% 然后是热情感谢下列并不止这些大佬的博客: 图连通性(一):Tarjan算法求解有向图强连通分量 图连通性(二):Tarj ...
- 在无向图中找最短桥(tarjan)
题目:hdu 4738 题目意思: 曹操有N个岛,这些岛用M座桥连接起来 每座桥有士兵把守(也可能没有) 周瑜想让这N个岛不连通,但只能炸掉一座桥 并且炸掉一座桥需要派出不小于守桥士兵数的人去 解题 ...
- Tarjan找桥和割点与点连通分量与边连通分量【未成形】
之前只学了个强连通Tarjan算法,然后又摸了缩点操作: 然后今天在lightoj摸了一道模板题,是求所有桥的题: 然后发现,要把:割点,割点集合,双连通,最小割边集合(桥),点连通分量,边连通分量都 ...
随机推荐
- dockerfile简介及书写规则
Dockerfile 简介 Dockfile是一种被Docker程序解释的脚本, Dockerfile由一条一条的指令组成,每条指 ...
- AndroidStudio提高编译速度的建议
1.使用最新的Android gradle插件 Google tools team一直致力于提高android studio的编译速度,使用最新的gradle插件可以搞编译速度 在Android Gr ...
- SAP WM TO Print Control设置里,Movement Type 的优先级更高
SAP WM TO Print Control设置里,Movement Type 的优先级更高 存储类型的配置: 从storage type GRM 搬到任何地方,都不需要打印TO单. 移动类型的配置 ...
- NodeJS实现websocket代理机制
使用的模块 ws http http-proxy 主要通过htt-proxy实现中转 启动websocket服务 var WebSocketServer = require('ws').Server; ...
- 数学--数论---P4718 Pollard-Rho算法 大数分解
P4718 [模板]Pollard-Rho算法 题目描述 MillerRabin算法是一种高效的质数判断方法.虽然是一种不确定的质数判断法,但是在选择多种底数的情况下,正确率是可以接受的.Pollar ...
- [USACO1.3]虫洞wormhole
题目描述 农夫约翰爱好在周末进行高能物理实验的结果却适得其反,导致N个虫洞在农场上(2<=N<=12,n是偶数),每个在农场二维地图的一个不同点. 根据他的计算,约翰知道他的虫洞将形成 N ...
- python selenium(定位方法)
一.定位方法 注意:元素属性必须唯一存在 #id定位 find_element_by_id() #name定位 find_element_by_name() #class_name定位 find_el ...
- VUE生命周期中的钩子函数及父子组件的执行顺序
先附一张官网上的vue实例的生命周期图,每个Vue实例在被创建的时候都需要经过一系列的初始化过程,例如需要设置数据监听,编译模板,将实例挂载到DOM并在数据变化时更新DOM等.同时在这个过程中也会运行 ...
- 开源 一套 Blazor Server 端精致套件
Blazor 作为一种 Web 开发的新技术已经发展有一段时间了,有些人标称 无 JS 无 TS,我觉得有点误导新人的意味,也有人文章大肆宣传 Blazor 是 JavaScript 的终结者,是为了 ...
- MySQL命令2
索引与外键 // 添加索引 ALTER TABLE orders ADD KEY order_ix_custid(cust_id); // 删除索引 ALTER TABLE orders DROP K ...