题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458

Problem Description
Given an undirected connected graph G with n nodes and m edges, with possibly repeated edges and/or loops. The stability of connectedness between node u and node v is defined by the number of edges in this graph which determines the connectedness between them (once we delete this edge, node u and v would be disconnected).

You need to maintain the graph G, support the deletions of edges (though we guarantee the graph would always be connected), and answer the query of stability for two given nodes.

 
Input
There are multiple test cases(no more than 3 cases), and the first line contains an integer t, meaning the totally number of test cases.

For each test case, the first line contains three integers n, m and q, where 1≤n≤3×104,1≤m≤105 and 1≤q≤105. The nodes in graph G are labelled from 1 to n.

Each of the following m lines contains two integers u and v describing an undirected edge between node u and node v.

Following q lines - each line describes an operation or a query in the formats:
⋅ 1 a b: delete one edge between a and b. We guarantee the existence of such edge.
⋅ 2 a b: query the stability between a and b.

 
Output
For each test case, you should print first the identifier of the test case.

Then for each query, print one line containing the stability between corresponding pair of nodes.

题目大意:给一个N个点M条边的无向图,有Q个询问:1、删掉a、b之间所存在的边;2、询问有多少条边,单独删掉之后a与b不再连通。

思路:脑洞大开。

对于询问,首先想到的就是a与b之间有多少桥(割边),然后想到双连通分量,然而删边是个坑爹的问题,于是我们离线倒着来,把删边变成加边。

双连通分量这种东西呢,其实缩点连起来之后,就是一棵树辣。

然后询问两个点的时候,设根到点x的距离为dep[x],a、b的最近公共祖先为lca(a, b),那么询问query(a, b) = dep[a] + dep[b] - 2 * dep[lca(a, b)]

加一条边的时候呢,比如加edge(a, b),那么原来的a到b的路径就形成了一个环,那么这个环就应该缩成一个双连通分量(每个点只会被缩一次,平摊的复杂度肯定是没问题的)。

这里可以用并查集来维护同一个双连通分量,每次都是儿子合并到父亲,然后每一次点u合并到父亲的时候,u和u的所有子孙的高度都会减一。

因为要处理一棵子树的所有值,这里使用DFS序+树状数组的方法来维护每个点的深度dep。

然后求LCA这里用的是树上倍增。整个题目要做的就是这些了。

然后整个流程就是:

1、初始化边表并读入所有数据并给边表排序用于查找(我这里用了vector)。(复杂度O(n+m+q+mlog(m)))

2、然后给1操作涉及的边打个删除标记。(复杂度O(qlog(m)))

3、DFS随便建一棵树,给DFS到的边打个删除标记(我直接把父边从vector移除了),顺便建立好DFS序和树状数组、dep和fa数组(用于LCA倍增)。(复杂度O(n+m+nlog(n)))

4、初始化LCA倍增。(复杂度O(nlog(n)))

5、对于每条没打标记的边(即树里的横向边?) edge(a, b),合并路径path(a, b)上的所有点,并维护好树状数组。(复杂度为O(m+nlog(n)))

6、逆序跑询问,第一个操作就加边,加边方法同流程4,第二个操作便是求出LCA,然后用树状数组扒出每个点的深度,然后加加减减得到结果并存起来。(复杂度O(qlog(n)+nlog(n))))

7、输出结果。(复杂度O(q))

代码(889MS):

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cctype>
using namespace std;
typedef long long LL; const int MAXV = ;
const int MAXQ = ;
const int MAX_LOG = ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ; int readint() {
char c = getchar();
while(!isdigit(c)) c = getchar();
int res = ;
while(isdigit(c)) res = res * + c - '', c = getchar();
return res;
} struct Node {
int to, del;
Node(int to): to(to), del() {}
bool operator < (const Node &rhs) const {
if(to != rhs.to) return to < rhs.to;
return del > rhs.del;
}
};
vector<Node> adjs[MAXV];
int n, m, q, T; void init() {
for(int i = ; i <= n; ++i) {
adjs[i].clear();
}
} void add_edge(int u, int v) {
adjs[u].push_back(Node(v));
adjs[v].push_back(Node(u));
} struct Query {
int op, a, b, apos, bpos;
void read() {
scanf("%d%d%d", &op, &a, &b);
if(op == ) {
lower_bound(adjs[a].begin(), adjs[a].end(), Node(b))->del = true;
lower_bound(adjs[b].begin(), adjs[b].end(), Node(a))->del = true;
}
}
} query[MAXQ];
int ans[MAXQ], acnt; struct BIT {
int tree[MAXV];
void init() {
memset(tree + , , n * sizeof(int));
}
int lowbit(int x) {
return x & -x;
}
void modify(int x, int val) {
while(x <= n) {
tree[x] += val;
x += lowbit(x);
}
}
void modify(int a, int b, int val) {
modify(a, val);
modify(b + , -val);
}
int get_val(int x) {
int res = ;
while(x) {
res += tree[x];
x -= lowbit(x);
}
return res;
}
} bitree; int bid[MAXV], eid[MAXV], dep[MAXV];
int fa[MAX_LOG][MAXV];
int dfs_clock; void dfs_id(int u, int f, int depth) {
if(f > ) adjs[u].erase(lower_bound(adjs[u].begin(), adjs[u].end(), Node(f)));
fa[][u] = f;
dep[u] = depth;
bid[u] = ++dfs_clock;
for(Node& p : adjs[u]) if(!p.del && !bid[p.to]) {
p.del = ;
dfs_id(p.to, u, depth + );
}
eid[u] = dfs_clock;
bitree.modify(bid[u], eid[u], );
}
void bit_init() {
memset(bid + , , n * sizeof(int));
bitree.init();
dfs_clock = ;
dfs_id(, , );
} struct LCA {
void init_lca() {
for(int k = ; k + < MAX_LOG; ++k) {
for(int u = ; u <= n; ++u) {
if(fa[k][u] == -) fa[k + ][u] = -;
else fa[k + ][u] = fa[k][fa[k][u]];
}
}
} int ask(int u, int v) {
if(dep[u] < dep[v]) swap(u, v);
for(int k = ; k < MAX_LOG; ++k) {
if((dep[u] - dep[v]) & ( << k)) u = fa[k][u];
}
if(u == v) return u;
for(int k = MAX_LOG - ; k >= ; --k) {
if(fa[k][u] != fa[k][v])
u = fa[k][u], v = fa[k][v];
}
return fa[][u];
}
} lca; int dsu[MAXV]; int find_set(int x) {
return dsu[x] == x ? x : dsu[x] = find_set(dsu[x]);
} void mergeFa(int u, int gold) {
u = find_set(u);
while(u != gold) {
int t = find_set(fa[][u]);
dsu[u] = t;
bitree.modify(bid[u], eid[u], -);
u = t;
}
} void merge(int u, int v) {
int l = find_set(lca.ask(u, v));
mergeFa(u, l);
mergeFa(v, l);
} void init_tree() {
for(int i = ; i <= n; ++i)
dsu[i] = i;
for(int u = ; u <= n; ++u)
for(Node p : adjs[u]) if(!p.del) {
merge(u, p.to);
}
} void solve() {
bit_init();
lca.init_lca();
init_tree();
for(int i = q - ; i >= ; --i) {
if(query[i].op == ) {
merge(query[i].a, query[i].b);
} else {
int l = lca.ask(query[i].a, query[i].b);
ans[acnt++] = bitree.get_val(bid[query[i].a]) + bitree.get_val(bid[query[i].b]) - * bitree.get_val(bid[l]);
}
}
for(int i = acnt - ; i >= ; --i)
printf("%d\n", ans[i]);
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d%d", &n, &m, &q);
init();
for(int i = , u, v; i < m; ++i) {
u = readint(), v = readint();
add_edge(u, v);
}
for(int i = ; i <= n; ++i)
sort(adjs[i].begin(), adjs[i].end()); acnt = ;
for(int i = ; i < q; ++i)
query[i].read(); printf("Case #%d:\n", t);
solve();
}
}

HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)的更多相关文章

  1. Hdu 5458 Stability (LCA + 并查集 + 树状数组 + 缩点)

    题目链接: Hdu 5458 Stability 题目描述: 给出一个还有环和重边的图G,对图G有两种操作: 1 u v, 删除u与v之间的一天边 (保证这个边一定存在) 2 u v, 查询u到v的路 ...

  2. HDU 4750 Count The Pairs ★(图+并查集+树状数组)

    题意 给定一个无向图(N<=10000, E<=500000),定义f[s,t]表示从s到t经过的每条路径中最长的边的最小值.Q个询问,每个询问一个t,问有多少对(s, t)使得f[s, ...

  3. Hdu 5452 Minimum Cut (2015 ACM/ICPC Asia Regional Shenyang Online) dfs + LCA

    题目链接: Hdu 5452 Minimum Cut 题目描述: 有一棵生成树,有n个点,给出m-n+1条边,截断一条生成树上的边后,再截断至少多少条边才能使图不连通, 问截断总边数? 解题思路: 因 ...

  4. (字符串处理)Fang Fang -- hdu -- 5455 (2015 ACM/ICPC Asia Regional Shenyang Online)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=5455 Fang Fang Time Limit: 1500/1000 MS (Java/Others)  ...

  5. HDU 4729 An Easy Problem for Elfness(主席树)(2013 ACM/ICPC Asia Regional Chengdu Online)

    Problem Description Pfctgeorge is totally a tall rich and handsome guy. He plans to build a huge wat ...

  6. Hdu 5451 Best Solver (2015 ACM/ICPC Asia Regional Shenyang Online) 暴力找循环节 + 递推

    题目链接: Hdu  5451  Best Solver 题目描述: 对于,给出x和mod,求y向下取整后取余mod的值为多少? 解题思路: x的取值为[1, 232],看到这个指数,我的心情是异常崩 ...

  7. Hdu 5459 Jesus Is Here (2015 ACM/ICPC Asia Regional Shenyang Online) 递推

    题目链接: Hdu 5459 Jesus Is Here 题目描述: s1 = 'c', s2 = 'ff', s3 = s1 + s2; 问sn里面所有的字符c的距离是多少? 解题思路: 直觉告诉我 ...

  8. HDU 4719 Oh My Holy FFF(DP+线段树)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description N soldiers from the famous "*FFF* army" is standing in a line, from left to ri ...

  9. hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)

    hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...

随机推荐

  1. gulp自动化构建

    最近正在使用gulp去帮我自动化构建一些技术块,感觉很爽,所以把gulp操作步骤给写笔记,记录下来... 首先了解什么是gulp? 我的理解是一个工具并且自动化的,能帮你把一些前端技术的语法转换成当前 ...

  2. [转] 前端中的MVC

    MVC是一种设计模式,它将应用划分为3个部分:数据(模型).展现层(视图)和用户交互(控制器).其中: M - MODEL(模型) V - VIEW(视图) C - CONTROLLER(控制器) 一 ...

  3. 搭把手教美工妹妹如何通过升级SSD提升电脑性能

    -----by LinHan 不单单适用于妹子,我这名的意思的妹子也能看懂. 以下教程依据实践和部分互联网资料总结得出,向博客园, CSDN的前辈们致谢:同时,如有说的不正确或有不到位的地方,麻烦指出 ...

  4. css 选择器优先级

    优先级自上而下逐渐递减 1. 在属性后面使用 !important 会覆盖页面内任何位置定义的元素样式. 2.作为style属性写在元素内的样式 3.id选择器 4.类选择器 5.标签选择器 6.通配 ...

  5. 【DP】POJ 2385

    题意:又是Bessie 这头牛在折腾,这回他喜欢吃苹果,于是在两棵苹果树下等着接苹果,但苹果不能落地后再接,吃的时间不算,假设他能拿得下所有苹果,但是这头牛太懒了[POJ另一道题目说它是头勤奋的奶牛, ...

  6. SPX Instant Screen Capture

    Today I will recommend a NICE screen capture tool, which name is SPA Instant Screen Capture. http:// ...

  7. Android SDK下载和更新失败的解决方法

    解决国内访问Google服务器的困难启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Se ...

  8. 下载php扩展笔记

    查找相关php的扩展网址https://pecl.php.net/index.php PECL 的全称是 The PHP Extension Community Library ,即PHP 扩展库.是 ...

  9. 撑100s小游戏

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <met ...

  10. convert与int.parse int

    1,convert :适合将object 转换 int:简单数据转换 int.parse:将string类型转换为int 2,convert:对于空值返回0 不会报异常 int.parse:将会抛出异 ...