题意:给你一个无向图,有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. Linux上Oracle 11g安装步骤图解

    Oracle 11g下载地址: http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html 选 ...

  2. 2018/03/31 每日一个Linux命令 之 date

    date 命令主要用于查看和修改时间和时区 -- 这里主要学习基本的查看和设置时间和时区的方法. 直接显示日期 date '+%D' 效果 vagrant@hong:~$ date '+%D' 03/ ...

  3. Elegant Construction---hdu5813(构造图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5813 题意是:有n个点,每个点都能到达num个点,让我们构造任意一个有向图满足条件,即:使得 i 能到 ...

  4. 原生js模仿下拉刷新功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. POJ3468 a simple problem with integers 分块

    题解:分块 解题报告: 是个板子题呢qwq 没什么可说的,加深了对分块的理解趴还是 毕竟这么简单的板子题我居然死去活来WA了半天才调出来,,,哭了QAQ 还是说下我错在了哪几个地方(...是的,有好几 ...

  6. opencv-Python---动态人脸捕捉

    本章重点内容: 1.python写人脸识别 2.选择OpenCv框架 案例1 导入图片并打开显示 思路:1.导入库 2.加载图片 3.创建窗口 4.显示图片 5.暂停窗口 6.关闭窗口 #1.导入库 ...

  7. Socket通信的Python实现

    Python中实现socket通信,socket通信的服务端比较复杂,而客户端非常简单,所以客户端基本上都是用sockct模块实现,而服务 端用有很多模块可以使用.下面就说一下服务端可使用的模块. 模 ...

  8. AspNetPager.dll 分页控件使用

    今天在用.net 做网站的时候,用到了DATALIST,但是datalist 没有自带的分页控件,后来在网上找了好长时间,看了aspnetpager.dll这个控件,这个控件挺好用的.我把使用方法写出 ...

  9. 自定义WordPress文件上传路径

    自WordPress 3.5版本开始,隐藏了后台媒体设置页面的“默认上传路径和文件的完整URL地址”选项,可以通过下面的代码将该选项调出来. 将下面的代码添加到当前主题functions.php文件中 ...

  10. (转)Elasticsearch分析聚合

    Elasticsearch不仅仅适合做全文检索,分析聚合功能也很好用.下面通过实例来学习. 一.准备数据 {"index":{ "_index": " ...