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摸了一道模板题,是求所有桥的题: 然后发现,要把:割点,割点集合,双连通,最小割边集合(桥),点连通分量,边连通分量都 ...
随机推荐
- Spring Boot JPA中关联表的使用
文章目录 添加依赖 构建Entity 构建Repository 构建初始数据 测试 Spring Boot JPA中关联表的使用 本文中,我们会将会通过一个Book和Category的关联关系,来讲解 ...
- 计算5的n次幂html代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- RxJava--Buffer,GroupBy 对比
Buffer 设定收集n个元素为一组,以下方代码为例,三个为一组,则当组满三个元素时,返回一次List数据 没组满三个元素时,如果调用onComplete,直接发送剩余元素,没调用onComplete ...
- WeChatSampleBuilder V2.0 使用教程(网页版+桌面版)
为了方便开发者可以快速搭建一个最小化所需模块的 Senparc.Weixin SDK Sample 项目,我们于 2018 年 11 月发布了首个 WeChatSampleBuilder 的版本,受到 ...
- DeepWalk论文精读:(3)实验
模块三 1 实验设计 1.1 数据集 BLOGCATALOG[39]:博客作者网络.标签为作者感兴趣的主题. FLICKR[39]:照片分享网站的用户网络.标签为用户的兴趣群组,如"黑白照片 ...
- 升级vue项目中的element-ui的版本
首先卸载项目中的element-ui 命令为: npm uninstall element-ui / cnpm uninstall element-ui 安装更新最新的element-ui 命令为 n ...
- RF(ride 工具使用)
1.新建项目 project,工程 suite,用例 testcase 新建 project:file -> new project,输入工程名,Type 选择 directory,选择工程存放 ...
- 【Java8新特性】一张图带你领略Java8有哪些新特性
写在前面 很多小伙伴留言说,冰河你能不能写一些关于Java8的文章呢,看书看不下去,看视频进度太慢.好吧,看到不少读者对Java8还是比较陌生的,那我就写一些关于Java8的文章吧,希望对大家有所帮助 ...
- MongoDB 部署以及操作
目录 1.MongoDB简介 2.MongoDB优势 3.MongoDB安装 3.MongoDB用户管理 3.1.Mongodb创建超级管理员 3.2.MongoDB创建读写用户 3.3.Moongo ...
- C语言程序设计实验报告四
C程序设计实验报告 姓 名:赖瑾 实验地点:家 实验时间:2020年4月9日 实验项目:5.3.1练习2 求数列的前n项和 5.3.2练习2 求水仙花数 5.3.4 十进制转换 5.3.5练习1 百马 ...