题意:给你一个无向图,有q次操作,每次连接两个点,问你每次操作后有几个桥

思路:我们先用tarjan求出所有的桥,同时我们可以用并查集缩点,fa表示缩点后的编号,还要记录每个节点父节点pre。我们知道,缩点后形成一棵树,所有边都是桥,连接两点必会成环,环上任意边都不是桥。所以连点后,我们把两个点一步一步往上走,如果往上走之后发现fa不一样,说明走过了一条桥,那么合并fa,桥数量-1。

代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
using namespace std;
int head[maxn], dfn[maxn], low[maxn], vis[maxn], fa[maxn], pre[maxn], tot, index, ans;
struct Edge{
int to, next;
}e[maxn << ];
void addEdge(int u, int v){
e[tot].to = v;
e[tot].next = head[u];
head[u] = tot++;
}
int find(int x){
return fa[x] == x? x : fa[x] = find(fa[x]);
}
void Union(int u, int v){
int fx = find(u);
int fy = find(v);
if(fx != fy){
fa[fx] = fy;
}
}
void tarjan(int u, int Fa){
vis[u] = ;
dfn[u] = low[u] = ++index;
pre[u] = Fa;
for(int i = head[u]; i != -; i = e[i].next){
int v = e[i].to;
if(!dfn[v]){
tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v] > dfn[u]){
ans++;
}
else{
Union(u, v);
}
}
else if(v != Fa){
low[u] = min(low[u], dfn[v]);
}
}
}
void Move(int x){
int fx = find(x);
int fy = find(pre[x]);
if(fx != fy){
fa[fx] = fy;
ans--;
}
}
void LCA(int u, int v){
while(dfn[u] > dfn[v]){
Move(u);
u = pre[u];
}
while(dfn[v] > dfn[u]){
Move(v);
v = pre[v];
}
while(u != v){
Move(u);
Move(v);
u = pre[u];
v = pre[v];
}
}
void init(){
tot = index = ans = ;
memset(head, -, sizeof(head));
memset(vis, , sizeof(vis));
memset(dfn, , sizeof(dfn));
}
int main(){
int n, m, Case = ;
while(scanf("%d%d", &n, &m) && n + m){
init();
int u, v;
for(int i = ; i < m; i++){
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
for(int i = ; i <= n; i++)
fa[i] = i;
tarjan(, );
int q;
scanf("%d", &q);
printf("Case %d:\n", Case++);
while(q--){
scanf("%d%d", &u, &v);
if(find(u) != find(v)){
LCA(u, v);
}
printf("%d\n", ans);
}
printf("\n");
}
return ;
}

POJ 3694 Network(并查集缩点 + 朴素的LCA + 无向图求桥)题解的更多相关文章

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

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

  2. 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D

    目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...

  3. POJ3694:Network(并查集+缩点+lca)

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13172   Accepted: 4774 题目链接:htt ...

  4. POJ 2236 Wireless Network (并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 761 ...

  5. poj 2236 Wireless Network (并查集)

    链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 ...

  6. POJ 3694 Network (求桥,边双连通分支缩点,lca)

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5619   Accepted: 1939 Descripti ...

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

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

  8. poj 1456 Supermarket - 并查集 - 贪心

    题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...

  9. poj 2236【并查集】

    poj 2236 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical t ...

随机推荐

  1. LightOJ 1027 - A Dangerous Maze(求期望)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1027 题意:又一个迷宫,有n个门,每个门又一个值num,如果num>0 说明在n ...

  2. python2.X编码

    1.Python文件的编码 在Python文件中,可以在第一或第二行指定文件的编码格式(以注释的形式加),这也是Python语法规定的,见http://www.python.org/peps/pep- ...

  3. 【Python】【Web.py】python调用html【问题:echart图标调用html上未显示】

    code调用123.html和echarts.min.js文件 code.py import web import execjs urls = ( '/hello', 'hello', ) app = ...

  4. mysql 权限管理 针对库 授权 db.*

    需求 只放行user表 db1库的select权限 mysql> grant select on db1.* to 'mike'@'localhost'; Query OK, rows affe ...

  5. java-JProfiler(三)-进行本地JVM的性能监控-监视本地java程序

    1.打开JProfiler 默认会启动快速窗口[或者使用菜单Session→Start Center]打开 这里监视本地java程序,故在 主界面 2.查看监视界面 这时就可以查看 Instance ...

  6. 【剑指offer】重建二叉树

    一.题目: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7 ...

  7. Redis的五种数据结构的内部编码

    type命令实际返回的就是当前键的数据结构类型,它们分别是:string(字符串).hash(哈希). list(列表).set(集合).zset(有序集合),但这些只是Redis对外的数据结构. 实 ...

  8. vue学习之npm

    任何一门计算机语言都包含了丰富的第三方库,npm就是JavaScript这门语言的第三方库管理工具,本文详细介绍了JavaScript的包管理工具,npm. 在计算机中安装好Node.js之后,默认已 ...

  9. Spark Shuffle Write阶段磁盘文件分析

    这篇文章会详细介绍,Sort Based Shuffle Write 阶段是如何进行落磁盘的 流程分析 入口处: org.apache.spark.scheduler.ShuffleMapTask.r ...

  10. 如何调用另一个python文件中的代码

    模块的搜索路径 模块的搜索路径都放在了sys.path列表中,如果缺省的sys.path中没有含有自己的模块或包的路径,可以动态的加入(sys.path.apend)即可.下面是sys.path在Wi ...