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 ...
随机推荐
- 2进制,16进制,BCD,ascii,序列化对象相互转换
public final static char[] BToA = "0123456789abcdef".toCharArray() ; 1.16进制字符串转为字节数组 /** * ...
- C#,MVC视图中把枚举转成DropdownList
1.拓展EnumHelper public static class EnumHelper { // Get the value of the description attribute if the ...
- JS实现页面跳转重定向的几种方式
1.重定向 <script language="javascript"type="text/javascript"> window.locatio ...
- 【工具篇】xshell
SSH.telnet.串口登录等,类似Secure CRT,蛮好用的. 中文显示乱码的解决方法,file->properties,在Encoding那里修改为UTF-8 修改颜色,点Edit修改 ...
- 封装Timer
System.Timers.Timer,System.Timers.Timer在使用的过程中需要: 1.构造函数不同,构造函数可以什么事情也不做,也可以传入响应间隔时间:System.Timers.T ...
- visualSVN server库迁移(转)
转自:http://blog.csdn.net/yuhuijun_1/article/details/9762683 首先,VisualSVN Server Manager,包含两个路径,一个是安装路 ...
- 神秘链接__proto__是什么鬼
_proto_实际上是某个实例对象的隐藏属性,而prototype是其构造器函数(或者说‘类’)的原型属性; function Mine() {} var hi = new Function(), ...
- actionscript sendToURL请求url,传递http_referer分浏览器统计
IE全版本都不传递referer,但会在header中传递X_FLASH_VERSION,例如:"HTTP_X_FLASH_VERSION":"13,0,0,182&qu ...
- python 图片压缩存储
python(PIL)图像处理(等比例压缩.裁剪压缩) 缩略(水印)图 http://outofmemory.cn/code-snippet/12264/python-PIL-image-proces ...
- RabbitMQ启动出错:- unable to connect to epmd on xxxx: timeout (timed out)
yum install后启动rabbitmq报错: [root@www ~]# /etc/init.d/rabbitmq-server start Starting rabbitmq-server: ...