题目链接:http://poj.org/problem?id=3694

题意:n个点,m条边,给你一个连通图,然后有Q次操作,每次加入一条边(A,B),加入边后,问当前还有多少桥,输出桥的个数。

解题思路:先将原连通图边双连通缩点成一颗树,Q次操作过程中对树进行LCA操作。具体看代码:

看网上也有不缩点的方法。

思路参考于:http://www.cnblogs.com/kuangbin/p/3184884.html

#include "stdio.h"   //poj 3177 边双连通问题 + LCA(最近公共祖先)
#include "string.h"
#include "vector"
#include "queue"
using namespace std; #define N 100100
#define M 400200 struct node
{
int x,y;
bool visit;
int next;
} edge[2*M];
int idx,head[N]; void Init()
{
idx = 0;
memset(head,-1,sizeof(head));
} void Add(int x,int y)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].visit = false;
edge[idx].next = head[x];
head[x] = idx++;
} int time;
int low[N],dfn[N];
inline int MIN(int a,int b)
{
return a<b?a:b;
} int st[M],num; //记录哪些点为桥
int stackk[2*M],top; //模拟栈(本题栈中存的是点,不是边) int n,m;
int countt; //记录有多少个双连通分量
int belong[N]; void lian_tong(int x)
{
int t;
countt++;
while(1)
{
t = stackk[top];
top--;
belong[t] = countt;
if(t==x) break;
}
} void DFS(int x)
{
int i,y;
stackk[++top] = x;
low[x] = dfn[x] = ++time;
for(i=head[x]; i!=-1; i=edge[i].next)
{
y = edge[i].y;
if(edge[i].visit) continue;
edge[i].visit = edge[i^1].visit = true;
if(!dfn[y])
{
DFS(y);
low[x] = MIN(low[x],low[y]);
if(low[y]>dfn[x])
st[num++] = i; //记录桥(两边双连通分量必定由桥相连)
}
else
low[x] = MIN(low[x],dfn[y]);
}
if(dfn[x]==low[x])
lian_tong(x); //标记当前边双连通分量
} int ans;
bool mark[N];
int deep[N];
int father[N];
vector<int> vec[N]; //存树 void LCA_bfs(int root)
{
int i,x,y;
memset(deep,-1,sizeof(deep));
deep[root] = 0;
mark[root] = false;
father[root] = -1;
queue<int> q;
q.push(root);
while(!q.empty())
{
x = q.front();
q.pop();
for(i=0; i<(int)vec[x].size(); ++i)
{
y = vec[x][i];
if(deep[y]!=-1) continue;
deep[y] = deep[x]+1;
mark[y] = true;
father[y] = x;
q.push(y);
}
}
} void swap(int &x,int &y)
{
int t = x;
x = y;
y = t;
} void LCA(int x,int y)
{
if(deep[x] > deep[y]) swap(x,y);
while(deep[x]<deep[y])
{
if(mark[y])
{
ans--;
mark[y] = false;
}
y = father[y];
}
while(x!=y)
{
if(mark[x])
{
ans--;
mark[x] = false;
}
if(mark[y])
{
ans--;
mark[y] = false;
}
x = father[x];
y = father[y];
}
} void Solve()
{
int i;
int x,y;
countt = 0; //统计边双连通分量的个数
num = 0; //统计桥的条数
top = 0; //栈
time = 0;
memset(dfn,0,sizeof(dfn));
DFS(1);
for(i=1; i<=countt; ++i) vec[i].clear();
for(i=0; i<num; ++i) //遍历桥
{
x = edge[st[i]].x;
y = edge[st[i]].y;
x = belong[x];
y = belong[y];
vec[x].push_back(y);
vec[y].push_back(x);
}
LCA_bfs(1);
ans = countt - 1;
int Q;
int u,v;
scanf("%d",&Q);
while(Q--)
{
scanf("%d %d",&u,&v);
LCA(belong[u],belong[v]);
printf("%d\n",ans);
}
printf("\n");
} int main()
{
int i;
int Case=0;
int x,y;
while(scanf("%d %d",&n,&m),n+m)
{
Init();
Case++;
for(i=0; i<m; ++i)
{
scanf("%d %d",&x,&y);
Add(x,y);
Add(y,x);
}
printf("Case %d:\n",Case);
Solve();
}
return 0;
}

poj 3694 Network 边双连通+LCA的更多相关文章

  1. POJ 3694 Network 无向图双联通+LCA

    一开始题目没看清楚,以为是增加那条边后还有多少桥,所以就当做是无向图tarjan缩点后建树,然后求u,v的最近公共祖先,一直wa. 后来再看题目后才发现边放上去后不会拿下来了,即增加i条边后桥的数量. ...

  2. Poj 3694 Network (连通图缩点+LCA+并查集)

    题目链接: Poj 3694 Network 题目描述: 给出一个无向连通图,加入一系列边指定的后,问还剩下多少个桥? 解题思路: 先求出图的双连通分支,然后缩点重新建图,加入一个指定的边后,求出这条 ...

  3. POJ 3694——Network——————【连通图,LCA求桥】

    Network Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  4. poj 3694 Network(双连通分量)

    题目:http://poj.org/problem?id=3694 #include <iostream> #include <cstring> #include <cs ...

  5. POJ 3694 Network(Tarjan求割边+LCA)

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10969   Accepted: 4096 Descript ...

  6. [双连通分量] POJ 3694 Network

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9434   Accepted: 3511 Descripti ...

  7. (中等) CF 555E Case of Computer Network,双连通+树。

    Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible a ...

  8. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  9. poj 3694 Network(割边+lca)

    题目链接:http://poj.org/problem?id=3694 题意:一个无向图中本来有若干条桥,有Q个操作,每次加一条边(u,v),每次操作后输出桥的数目. 分析:通常的做法是:先求出该无向 ...

随机推荐

  1. 重构第7天 重命名(Rename )

    理解:重命名就是把一些函数.字段.类.参数的名称 重命名为易于理解,最好是和自身的意义相同的名称.这样更易于理解,也可以减少大量的注释,名字即含义. 详解: 这个重构方法是我经常也是最常用的一种.我们 ...

  2. 可拖拽的ListBox

    之前在写播放器的时候,遇到了一个问题,现在播放器无论是千千,KuGoo还是比较原始的MediaPlayer,它们的播放表都是可以拖拽的,直接把文件拖到播放表实现歌曲的添加那个先暂且不说,光是播放表里面 ...

  3. C#检查标准图幅编号

    /// <summary> /// 检查是否为标准图幅编号 /// </summary> /// <param name="MapNumber"> ...

  4. TreeBuilder科学的树创建器

    public static class TreeBuilder { public static List<dynamic> Build(IEnumerable<dynamic> ...

  5. 【jQuery基础学习】06 jQuery表单验证插件-Validation

    jQuery的基础部分前面都讲完了,那么就看插件了. 关于jQuery表单验证插件-Validation validation特点: 内置验证规则:拥有必填.数字.E-Mail.URL和信用卡号码等1 ...

  6. mbps

    Mbps=Mbit/s即兆比特每秒.Million bits per second的缩写 传输速率是指设备的的数据交换能力,也叫“带宽”,单位是Mbps(兆位/秒),目前主流的集线器带宽主要有10Mb ...

  7. 后缀数组---New Distinct Substrings

    Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...

  8. 动态创建JS

    var element=document.createElement('script'); element.setAttribute('src', './js/move.js'); document. ...

  9. IOS6学习笔记(一)

    一.ARC 1.ARC环境下可以使用-(void)dealloc{};处理一些事情(比如移除KVO观察),但不要调用[super dealloc]; 2.ARC与非ARC混编要注意符合Cocoa命名约 ...

  10. javascript的封装实例

    StringBuffer方法的js自定义封装: <!doctype html><html lang="en"> <head> <meta ...