POJ 3694 Network(无向图求桥+重边处理+LCA)
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <cstring>
usingnamespace std;
#define INF 0xfffffff
#define min(a,b) (a<b?a:b)
#define maxn 100005
int m, n, Time, ans;
int dfn[maxn], low[maxn], Father[maxn], bridge[maxn];
vector<int> G[maxn]; void init()
{
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(bridge, 0, sizeof(bridge));
memset(Father, 0, sizeof(Father));
Time = ans = 0; for(int i=0; i<=n; i++)
G[i].clear();
} void Tarjan(int u,int fa)
{
dfn[u] = low[u] = ++Time;
Father[u] = fa;
int len = G[u].size(), v; for(int i=0; i<len; i++)
{
v = G[u][i]; if( !low[v] )
{
Tarjan(v, u);
low[u] = min(low[u], low[v]); if(dfn[u] < low[v])
{
bridge[v] ++;
ans ++;
}
}
elseif(v != fa)
{
low[u] = min(low[u], dfn[v]); if(dfn[u] < low[v])
{
bridge[v] ++;
ans --;
}
} }
}
void Lca(int a,int b)
{
if(a == b)
return ; if(dfn[a] > dfn[b])
{
if( bridge[a] == 1)
{
bridge[a] = 0;
ans --;
}
Lca(Father[a], b);
}
else
{
if(bridge[b] == 1)
{
bridge[b] = 0;
ans --;
}
Lca(a, Father[b]);
}
} int main()
{
int cas = 1;
while(scanf("%d %d",&n, &m), m+n)
{
int Q, a, b;
init();
while(m --)
{
scanf("%d %d",&a, &b);
G[a].push_back(b);
G[b].push_back(a);
} scanf("%d", &Q);
Tarjan(1, 0);
// printf("%d\n", ans);
printf("Case %d:\n", cas ++);
while(Q --)
{
scanf("%d %d",&a, &b);
Lca(a, b);
printf("%d\n", ans);
}
}
return0;
}
/* 4 4
1 2
2 1
2 3
1 4
2
1 2
3 4 */
POJ 3694 Network(无向图求桥+重边处理+LCA)的更多相关文章
- POJ 3694 Network (求桥,边双连通分支缩点,lca)
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5619 Accepted: 1939 Descripti ...
- POJ 3694 Network 无向图双联通+LCA
一开始题目没看清楚,以为是增加那条边后还有多少桥,所以就当做是无向图tarjan缩点后建树,然后求u,v的最近公共祖先,一直wa. 后来再看题目后才发现边放上去后不会拿下来了,即增加i条边后桥的数量. ...
- poj 1144 Network 无向图求割点
Network Description A Telephone Line Company (TLC) is establishing a new telephone cable network. Th ...
- POJ 3694 Network(Tarjan求割边+LCA)
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10969 Accepted: 4096 Descript ...
- HDU 4738--Caocao's Bridges(重边无向图求桥)
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Poj 3694 Network (连通图缩点+LCA+并查集)
题目链接: Poj 3694 Network 题目描述: 给出一个无向连通图,加入一系列边指定的后,问还剩下多少个桥? 解题思路: 先求出图的双连通分支,然后缩点重新建图,加入一个指定的边后,求出这条 ...
- UVA 796 Critical Links(无向图求桥)
题目大意:给你一个网络要求这里面的桥. 输入数据: n 个点 点的编号 (与这个点相连的点的个数m) 依次是m个点的 输入到文件结束. 桥输出的时候需要排序 知识汇总: 桥: 无向连通 ...
- UVA 796 Critical Links(模板题)(无向图求桥)
<题目链接> 题目大意: 无向连通图求桥,并将桥按顺序输出. 解题分析: 无向图求桥的模板题,下面用了kuangbin的模板. #include <cstdio> #inclu ...
- 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D
目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...
随机推荐
- html_entity_decode() 函数
html_entity_decode() 函数 定义和用法 The html_entity_decode() function converts HTML entities to characte ...
- 关于es6的箭头函数使用与内部this指向
特型介绍:箭头函数是ES6新增的特性之一,它为JS这门语言提供了一种全新的书写函数的语法. 'use strcit'; let arr = [1,2,3]; //ES5 let es5 = arr.m ...
- js--小结⑦---格式转换
- [转帖]MATLAB曲线绘制及颜色类型
信号源产生的方法 来源:http://www.2cto.com/kf/201401/270494.html matlab的checkerboard说明,GOOD! 来源:http://www.chi ...
- angularjs 利用filter进行表单查询及分页查询
页面: <div> <input style="width:90%;margin-left:5px;margin-right:5px;" class=" ...
- 最简单的基于FFmpeg的移动端例子:IOS 视频解码器-保存
===================================================== 最简单的基于FFmpeg的移动端例子系列文章列表: 最简单的基于FFmpeg的移动端例子:A ...
- 文字排版--斜体(font-style)
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 【工具篇】xshell
SSH.telnet.串口登录等,类似Secure CRT,蛮好用的. 中文显示乱码的解决方法,file->properties,在Encoding那里修改为UTF-8 修改颜色,点Edit修改 ...
- 【POJ3580】【splay版】SuperMemo
Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...
- splice 操作符
几乎所有的数组操作都可用 splice 实现. 除了第一个参数,数组,为必须,其余的参数都不是必须的. splice ARRAY, OFFSET, LENGTH, LIST OFFSET 和 LENG ...