题意:给你一个无向图,有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. virtualenvwrapper 的安装和使用(转)

    原文:http://www.cnblogs.com/asmer-stone/p/5470144.html virtualenvwrapper是用来管理virtualenv的扩展包,用着很方便. 1. ...

  2. chown 命令

    root@localhost ~]# .txt # 同时修改文件属主和属组 !chown 执行上一个chown命令

  3. dedecms如何快速删除跳转的文章(记得清空内容回收站)

    网站内容更新多了,有些页面修改了,这时其他相关页面也要做相应的调整,不然可能会出现404错误,那么dedecms如何快速删除跳转的文章呢?下面就随ytkah一起操作一下吧 如上图所示,在“核心”(标示 ...

  4. 011-jdk1.8版本新特性三-Date API

    1.7.Date API Java 8 在包java.time下包含了一组全新的时间日期API.新的日期API和开源的Joda-Time库差不多,但又不完全一样,下面的例子展示了这组新API里最重要的 ...

  5. Iterator源码解读

    //继承关系 public interface Inteator { boolean hasNext(); Object next(); } public interface Iterable { I ...

  6. [py][mx]django form验证-给db减压

    django form认证-解压db压力 一般系统都需要前后端都验证 前端验证容器逃逸破解,如通过js console口去发 试想如果后端只有db验证,那么前端无论发什么后端都查询一次db,对db压力 ...

  7. Jmeter接口自动化测试 (四)(持续构建)

    本文转载至http://www.cnblogs.com/chengtch/p/6145867.html  Jmeter是压力测试.接口测试工具,Ant是基于Java的构建工具,具有跨平台的作用,jen ...

  8. 网站nginx负载下因程序错误导致多节点重复处理请求的解决过程

    目录 前言 问题来了 问题又来了 问题分析 困惑 转机 后续 前言: 这是我上周工作过程中的一次解决问题的过程.解决的是nginx负载环境下,因为应用程序异常导致某一请求被多节点站点重复处理的问题. ...

  9. Lintcode: Insert Node in a Binary Search Tree

    Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...

  10. Git—使用方法

    1.:插件的安装(eclipse LUNA版本之后已经自动集成,不需要安装插件). * 先打开该网页提供了对应版本的EGit,自己选择相应的版本.(http://wiki.eclipse.org/EG ...