题目链接: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. [Leetcode] Decode Ways

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  2. MVC 缓存实践(一)

    为什么要讲缓存.缓存到底有什么作用? 下面我们来说一个场景我们有一个首页菜单的布局基本是不会经常发生的变化,如果动态生成的 Web 页被频繁请求并且构建时需要耗用大量的系统资源,那么,如何才能改进这种 ...

  3. 立flag

    lixintong这半年来一直浪啊浪啊都不认真做题!!!!!!简直是太堕落啦!!lixintong非常讨厌这样的lixintong !!! 鉴于lixintong NOIP 完全爆炸啦! lixint ...

  4. svn bug

    Error:Can't find temporary directory:internal error 原因:服务器端,磁盘满了 repository and is not part of the c ...

  5. webservice总结

    webservice xml(DTD,Schema,Stax) SOAP jax-ws (java api xml webservice) 契约优先的开发模式 CXF Rest 异构平台之间的交互(. ...

  6. FastJson和AsyncHttpCLient

    Android的展示数据,除了上章所讲的本地存储外,大部分数据都来自于网络.首先介绍一下Android APP开发常见的网络操作方式.从网络层面上有底层的tcp/ip,也就是我们常见的socket套接 ...

  7. 给备战NOIP 2014 的战友们的10条建议

    应老胡要求,要写10条建议= = begin 1. 注意文件关联 比如 halt 前要close(input); close(output); 还有就是一定要打这两句话= = 2. 快排,大家都懂得. ...

  8. 手机版本高于xcode,xcode的快速升级

    iPhone手机更新版本,xcode未更新时,不能真机测试 在xcode show in finder里面添加最新iPhone 版本 重启xcode即可 真机测试

  9. webrtc初识

    最近由于项目的需求,开始接触了webrtc这个东西.没想到这东西的门槛还是蛮高的,接下来分享一下我所踩过的坑,希望对以后初次接触这个东西的人有所帮助. webrtc官网 第一步当然是看官方主页了(ww ...

  10. git 常见问题

    RPC failed; error: RPC failed; curl 56 SSL read: error:00000000:lib(0):func(0):reason(0), er rno 100 ...