题目: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. phpcms v9 打开网站特别慢 增加数据库缓存方法

    SET GLOBAL QUERY_CACHE_SIZE=80000000; 设置好查询缓存的大小就行了.比如设置个20MB.SET GLOBAL QUERY_CACHE_SIZE=20000000; ...

  2. mySQL时间

    " and day='".date('Y-m-d',strtotime($day_g)). "'";  时间如: 2014-09-09 and day>= ...

  3. MySQL创建数据库[保存mojo格式的数据库]已经常用的utf8格式数据库

    一.创建最新编码utf8mb4格式的库: CREATE DATABASE IF NOT EXISTS yourdbname CHARACTER SET utf8mb4 COLLATE utf8mb4_ ...

  4. oracle中的隐式提交(auto commit)

    通常我们执行sql或pl/sql时,需要我们手工提交.这样才能使所做的更改永久保存到数据库. 但有时即使我们没有在sql或pl/sql中发出commit命令,所做的更改也会被提交.这种提交是在某些特定 ...

  5. Fedora 17下安装Oracle 10g详细图文教程

    一.硬件要求——内存 & swap & 硬盘 最小内存与swap: 1 GB of RAM & swap 建议内存与swap: 2 GB of RAM & swap [ ...

  6. MySQL日期和时间函数

    WEEKDAY( date ) 返回date的星期索引( = 星期天 ) . mysql> select WEEKDAY( '1997-10-04 22:23:00' ) ; mysql> ...

  7. ios 8和iOS 9 适用的uidatepicker

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n ...

  8. shell复习笔记----命令与参数

    shell最基本的工作就是执行命令. 每键入一道命令, shell 就会执行. $cd work;ls -l whizprog.c 首先:格式很简单,以空白(Space 键或者 Tab键)隔开命令行中 ...

  9. spring mvc中的@PathVariable(转)

    鸣谢:http://jackyrong.iteye.com/blog/2059307 ------------------------------------------------ spring m ...

  10. org.hibernate.LazyInitializationException

    1.org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.c ...