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

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN=;
int bin[MAXN],n,m;
int low[MAXN],dfn[MAXN],father[MAXN];
int q,cut,index;
vector<int>g[MAXN]; int findx(int x)
{
return bin[x]==x?x:(bin[x]=findx(bin[x]));
}
int merge(int x,int y)
{
int fx,fy;
fx=findx(x);
fy=findx(y);
if(fx!=fy)
{
bin[fx]=fy;
return ;
}
else
return ;
} void tarjan(int i,int pre)
{
int j,k;
low[i]=dfn[i]=++index;
for (k=; k<g[i].size(); k++)
{
j = g[i][k];
if (!dfn[j])
{
tarjan(j,i);
low[i]=min(low[i],low[j]);
father[j]=i;
if (low[j] > dfn[i])
cut++;
else
merge(i,j);
}
else if (j!=pre)
{
low[i]=min(low[i],dfn[j]);
}
}
}
void lca(int u,int v)
{
while (u!=v)
{
while (dfn[u]>=dfn[v] && u!=v)
{
if (merge(u,father[u]))
cut--;
u = father[u];
}
while (dfn[v]>=dfn[u]&&u!=v)
{
if (merge(v,father[v]))
cut--;
v=father[v];
}
}
} int main()
{
int i,x,y,T=;
while(~scanf("%d%d",&n,&m)&&n||m)
{
for(i=; i<=n+; i++)
{
g[i].clear();
low[i]=dfn[i]=father[i]=;
bin[i]=i;
}
index=cut=; for(i=; i<m; i++)
{
scanf("%d%d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
tarjan(,-);
scanf("%d",&q); printf("Case %d:\n",T++);
while(q--)
{
scanf("%d%d",&x,&y);
lca(x,y);
printf("%d\n",cut);
}
cout<<endl;
}
return ;
}

poj 3694 Network(双连通分量)的更多相关文章

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

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

  2. poj 3694 Network 边双连通+LCA

    题目链接:http://poj.org/problem?id=3694 题意:n个点,m条边,给你一个连通图,然后有Q次操作,每次加入一条边(A,B),加入边后,问当前还有多少桥,输出桥的个数. 解题 ...

  3. poj 3694 Network(割边+lca)

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

  4. POJ 3352 (边双连通分量)

    题目链接: http://poj.org/problem?id=3352 题目大意:一个连通图中,至少添加多少条边,使得删除任意一条边之后,图还是连通的. 解题思路: 首先来看下边双连通分量的定义: ...

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

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

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

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

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

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

  8. POJ 3694 Network (tarjan + LCA)

    题目链接:http://poj.org/problem?id=3694 题意是给你一个无向图n个点,m条边,将m条边连接起来之后形成一个图,有Q个询问,问将u和v连接起来后图中还有多少个桥. 首先用t ...

  9. poj 1523 SPF(双连通分量割点模板)

    题目链接:http://poj.org/problem?id=1523 题意:给出无向图的若干条边,求割点以及各个删掉其中一个割点后将图分为几块. 题目分析:割点用tarjan算法求出来,对于每个割点 ...

随机推荐

  1. 《C和指针》 读书笔记 -- 第7章 函数

    1.当程序调用一个无法见到原型的函数时,编译器便认为该函数返回一个整型值.如果这个值实际上是非整型值时,还得执行类型转换,所以函数原型声明有时很重要. 2.值的类型并不是值的内在本质,而是取决于它被使 ...

  2. 《WPF程序设计指南》读书笔记——第5章 Stack与Wrap

    1.StackPanel面板 using System; using System.Windows; using System.Windows.Input; using System.Windows. ...

  3. oracle 表空间、用户名 相关语句

    一.oracle查询表空间文件所在路径 select * from dba_data_files t  where t.tablespace_name='FLW' 二.计算出表空间各相关数据 SELE ...

  4. 【MongoDB】 安装为windows services

    参考  http://stackoverflow.com/questions/2404742/how-to-install-mongodb-on-windows # mongodb.conf # da ...

  5. [译] ASP.NET 生命周期 – ASP.NET 上下文对象(七)

    使用 HttpRequest 对象 HttpRequest 对象描述的是一个正在被处理的 HTTP 请求.下表列举了 HttpRequest 中的属性,它们提供了当前请求的相关信息(HttpReque ...

  6. EXTJS 4.2 资料 控件之Window窗体相关属性的用法

    最大化,最小化,是否显示关闭按钮 var win_CommonPicLibMultiple = Ext.create("Ext.window.Window", { title: & ...

  7. tableView中不易被注意到的方法

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ } 这个方法 在 r ...

  8. php实现调用微信上传照片然后保存至服务器与数据库

    <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>    <s ...

  9. BeanFactory和FactoryBean

    BeanFactory和FactoryBean 1.BeanFactory BeanFactory定义了 IOC 容器的最基本形式,并提供了 IOC 容器应遵守的的最基本的接口,也就是Spring I ...

  10. A JSTL primer, Part 2: Getting down to the core

    In the initial article of this series, you got your first look at JSTL. We described the use of its  ...