Stability

Time Limit: 2000ms
Memory Limit: 102400KB

This problem will be judged on HDU. Original ID: 5458
64-bit integer IO format: %I64d      Java class name: Main

 

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.

Sample Input
1
10 12 14
1 2
1 3
2 4
2 5
3 6
4 7
4 8
5 8
6 10
7 9
8 9
8 10
2 7 9
2 7 10
2 10 6
2 10 5
1 10 6
2 10 1
2 10 6
2 3 10
1 8 5
2 5 10
2 4 5
1 7 9
2 7 9
2 10 5

Sample Output
Case #1:
0
0
0
0
2
4
3
3
2
3
4

Source
2015 ACM/ICPC Asia Regional Shenyang Online

解题:树链剖分

逆向操作,把删边视为加边,除去删除的那些边,玩树链剖分,首先,你得有棵树,没树,什么都是扯淡。

我们把要删的那些边从图上统统删除,剩下的残图不一定就是树,所以用并查集在残图上找棵树,树的边权都设为1,然后把残图中不在树上的边都加到树上,这样形成环了,那么把环上的树边全部置0.

现在开始删边(实际是加边,因为逆序操作)和查询,是的,逆序的,每次加边,就把形成的环,环上的树边都置成0,查询就查询这条路径上的边权和即可

 #include <iostream>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstring>
using namespace std;
typedef pair<int,int> PII;
const int maxn = ;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
} e[maxn<<];
struct node {
int lt,rt,sum,lazy;
} tree[maxn<<];
int head[maxn],tot;
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
inline void pushup(int v) {
tree[v].sum = tree[v<<].sum + tree[v<<|].sum;
}
inline void pushdown(int v) {
if(~tree[v].lazy) {
tree[v<<].sum = (tree[v<<].rt - tree[v<<].lt + )*tree[v].lazy;
tree[v<<].lazy = tree[v].lazy;
tree[v<<|].sum = (tree[v<<|].rt - tree[v<<|].lt + )*tree[v].lazy;
tree[v<<|].lazy = tree[v].lazy;
tree[v].lazy = -;
}
}
void build(int lt,int rt,int v) {
tree[v].lt = lt;
tree[v].rt = rt;
tree[v].lazy = -;
if(lt == rt) {
tree[v].sum = ;
return;
}
int mid = (lt + rt)>>;
build(lt,mid,v<<);
build(mid + ,rt,v<<|);
pushup(v);
}
void update(int lt,int rt,int val,int v) {
if(lt <= tree[v].lt && rt >= tree[v].rt) {
tree[v].sum = (tree[v].rt - tree[v].lt + )*val;
tree[v].lazy = val;
return;
}
pushdown(v);
if(lt <= tree[v<<].rt) update(lt,rt,val,v<<);
if(rt >= tree[v<<|].lt) update(lt,rt,val,v<<|);
pushup(v);
}
int query(int lt,int rt,int v) {
if(lt == tree[v].lt && rt == tree[v].rt) return tree[v].sum;
pushdown(v);
int mid = (tree[v].lt + tree[v].rt)>>;
if(rt <= mid) return query(lt,rt,v<<);
if(lt > mid) return query(lt,rt,v<<|);
return query(lt,mid,v<<) + query(mid + ,rt,v<<|);
}
int fa[maxn],dep[maxn],top[maxn],siz[maxn],son[maxn],loc[maxn],cnt;
void FindHeavyEdge(int u,int father,int depth) {
fa[u] = father;
dep[u] = depth;
siz[u] = ;
son[u] = -;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to == father) continue;
FindHeavyEdge(e[i].to,u,depth + );
siz[u] += siz[e[i].to];
if(son[u] == - || siz[son[u]] < siz[e[i].to])
son[u] = e[i].to;
}
}
void ConnectHeavyEdge(int u,int ancestor) {
top[u] = ancestor;
loc[u] = ++cnt;
if(son[u] != -) ConnectHeavyEdge(son[u],ancestor);
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to == fa[u] || e[i].to == son[u]) continue;
ConnectHeavyEdge(e[i].to,e[i].to);
}
}
void UPDATE(int u,int v,int val = ) {
while(top[u] != top[v]) {
if(dep[top[u]] < dep[top[v]]) swap(u,v);
update(loc[top[u]],loc[u],val,);
u = fa[top[u]];
}
if(u == v) return;
if(dep[u] > dep[v]) swap(u,v);
update(loc[son[u]],loc[v],val,);
}
int QUERY(int u,int v,int ret = ) {
while(top[u] != top[v]) {
if(dep[top[u]] < dep[top[v]]) swap(u,v);
ret += query(loc[top[u]],loc[u],);
u = fa[top[u]];
}
if(u == v) return ret;
if(dep[u] > dep[v]) swap(u,v);
return ret + query(loc[son[u]],loc[v],);
}
int u,v,ans[maxn],uf[maxn],op[maxn],x[maxn],y[maxn];
bool used[maxn];
int Find(int x) {
if(x != uf[x]) uf[x] = Find(uf[x]);
return uf[x];
}
multiset<PII>S,V;
int main() {
int kase,n,m,q,cs = ;
scanf("%d",&kase);
while(kase--) {
S.clear();
V.clear();
scanf("%d%d%d",&n,&m,&q);
for(int i = tot = cnt = ; i < m; ++i) {
scanf("%d%d",&u,&v);
if(u > v) swap(u,v);
S.insert(PII(u,v));
}
for(int i = ; i <= n; ++i) {
head[i] = -;
used[i] = false;
uf[i] = i;
}
for(int i = ; i < q; ++i) {
scanf("%d%d%d",op + i,x + i,y + i);
if(x[i] > y[i]) swap(x[i],y[i]);
if(op[i] == ) S.erase(S.find(PII(x[i],y[i])));
}
for(auto &it:S) {
int a = Find(it.first),b = Find(it.second);
if(a != b) {
V.insert(it);
add(it.first,it.second);
add(it.second,it.first);
uf[a] = b;
}
}
FindHeavyEdge(,,);
ConnectHeavyEdge(,);
build(,cnt,);
for(auto &it:S)
if(V.find(it) == V.end()) UPDATE(it.first,it.second);
for(int i = q-; i >= ; --i)
if(op[i] == ) UPDATE(x[i],y[i]);
else if(op[i] == ) ans[i] = QUERY(x[i],y[i]);
printf("Case #%d:\n",cs++);
for(int i = ; i < q; ++i)
if(op[i] == ) printf("%d\n",ans[i]);
}
return ;
}

HDU 5458 Stability的更多相关文章

  1. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...

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

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

  3. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  4. HDU 5458 Stability (树链剖分+并查集+set)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 给你n个点,m条边,q个操作,操作1是删边,操作2是问u到v之间的割边有多少条. 这题要倒着做才 ...

  5. 并查集+树链剖分+线段树 HDOJ 5458 Stability(稳定性)

    题目链接 题意: 有n个点m条边的无向图,有环还有重边,a到b的稳定性的定义是有多少条边,单独删去会使a和b不连通.有两种操作: 1. 删去a到b的一条边 2. 询问a到b的稳定性 思路: 首先删边考 ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  9. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

随机推荐

  1. 用.NetReactor保护您的源码[转][修改]

    原文链接 前言 VS开发的源代码安全性,是很多开发者头痛的事情.于是保护好源代码便成了开发者们最关心的事情之一了. 在网上搜一搜,很多有不少的第三方工具可以为源代码加密.加密方式不外乎就是混淆,加壳. ...

  2. python 使用 Pyscript 调试 报错

    UnicodeEncodeError: 'ascii' codec can't encode characters in position 13-16: ordinal not in range(12 ...

  3. 掌握Spark机器学习库-05-spark中矩阵与向量的使用

    1)介绍 矩阵: Matrix,看做二维表,基本运算(+,-,*,T) 向量: Vectors,方向和大小,基本运算,范数 2)spark中向量的使用(主要使用breeze.linalg) 3)spa ...

  4. iOS Programming UIWebView 2

    iOS Programming  UIWebView 1 Instances of UIWebView render web content. UIWebView可以显示web content. In ...

  5. R in action读书笔记(8)-第八章:回归(上)

    8.1回归的多面性 8.2 OLS回归 OLS回归拟合模型形式: 为了能够恰当地解释oLs模型的系数,数据必须满足以下统计假设. 口正态性对于固定的自变量值,因变量值成正态分布. 口独立性Yi值之间相 ...

  6. 提高SQL查询效率 的10大方法

    一.查询条件精确,针对有参数传入情况 二.SQL逻辑执行顺序 FROM–>JOIN–>WHERE–>GROUP–>HAVING–>DISTINCT–>ORDER–& ...

  7. mvc框架 与vuex的介绍

    应用级的状态集中放在store中: 改变状态的方式是提交mutations,这是个同步的事物: 异步逻辑应该封装在action中. const vuex_store = new Vuex.store( ...

  8. 慎将MBTI测试用于招聘或就业:4星|《人格魅力修炼指南》

    人格魅力修炼指南:成为理想中的自己,就靠它了!(<哈佛商业评论>增刊) <哈佛商业评论>的11篇领导者人格魅力相关的文章.比较专业. 一些重要的信息:慎将MBTI测试用于“招聘 ...

  9. webstorm里直接调用命令行

    写代码写到一半要切换窗口出去敲命令行?webstorm的external tools可以帮你省下一点时间 举例说明,比如我要直接使用npm: ctrl+alt+s打开setting菜单,找到exter ...

  10. UI开发复杂度度量

    1)要素的个数: 2)要素布局和渲染的复杂度: 3)交互的复杂度. 本质上分为两种:要素的复杂度和联系的复杂度. 联系包含要素间布局的联系与交互的联系,已经和外部上下文的联系.