题意:给你一个无向图,有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. 2018/04/03 每日一个Linux命令 之 lastb/last

    今天还在想暴力破解一个服务器是怎么完成的....... -- lastb功能说明:列出登录系统失败的用户相关信息. -- 单独执行 lastb 时候,它会读取/var/log 下的 btmp 文件,输 ...

  2. 【mlflow】执行import mlflow 报错:ImportError: No module named 'pkg_resources'

    命令行运行 python -c “import mlflow” 的时候报错: ImportError: No module named 'pkg_resources' 结果发现是因为本地有一个文件夹叫 ...

  3. 5839Special Tetrahedron---hdu5839(计算几何,求特殊四面体个数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5839 给你n个三维的点,然后求这n各点可以构成多少个特殊四面体,特殊四面体满足一下两点: 1.至少有四 ...

  4. CF750F New Year and Finding Roots 构造+树论

    正解:构造 解题报告: 传送门! 交互题交互题!哇好新鲜啊QwQ 首先考虑最傻逼的做法,应该是每个人都能想到的 首先看一下它给的条件,考虑到完全二叉树的性质,就可以发现,如果给的邻居只有一个,说明是叶 ...

  5. 洛谷P4035 球形空间产生器 [JSOI2008] 高斯消元

    正解:高斯消元 解题报告: 链接! 昂开始看到以为是,高斯消元板子题? 开始很容易想到的是,虽然是多维但是可以类比二维三维列出式子嘛 但是高斯消元是只能处理一元问题的啊,,,辣怎么处理呢 对的这就是这 ...

  6. 抽象语法符号ASN.1(Abstract Syntax Notation One)

      一.ASN.1 (Abstract Syntax Notation One) ASN.1包括两部分:数据描述语言(ISO 8824)和数据编码规则(ISO 8825).ASN.1的数据描述语言允许 ...

  7. Java学习之路-Spring的HttpInvoker学习

    Hessian和Burlap都是基于HTTP的,他们都解决了RMI所头疼的防火墙渗透问题.但当传递过来的RPC消息中包含序列化对象时,RMI就完胜Hessian和Burlap了. 因为Hessian和 ...

  8. cube-ui的用法

    .安装:npm install cube-ui -S .修改 .babelrc:(添加到plugins中去) { "plugins": [ ["transform-mod ...

  9. iOS 新浪微博-1.0框架搭建

    项目搭建 1.新建一个微博的项目,去掉屏幕旋转 2.设置屏幕方向-->只有竖向 3.使用代码构建UI,不使用storyboard 4.配置图标AppIcon和LaunchImage 将微博资料的 ...

  10. svg绘图工具raphael.js的使用

    1.raphael.js svg画图的开源库,支持IE8+ 官方api: http://dmitrybaranovskiy.github.io/raphael/reference.html Githu ...