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. 贪心 UVALive 6834 Shopping

    题目传送门 /* 题意:有n个商店排成一条直线,有一些商店有先后顺序,问从0出发走到n+1最少的步数 贪心:对于区间被覆盖的点只进行一次计算,还有那些要往回走的区间步数*2,再加上原来最少要走n+1步 ...

  2. Activity状态图、生命周期图(超详细),onSaveInstanceState只保存、恢复基本ui数据,持久数据不在这里保存。

    1.Activity状态图 2.Activity生命周期简图 启动Activity: onCreate()—>onStart()—>onResume(),Activity进入running ...

  3. Plugging an Unplugged Pluggable Database issue 3

    Multitenant Unplug/Plug Best Practices (文档 ID 1935365.1) 1.source 从0419 升级到1019 ,但是datapatch 没有回退041 ...

  4. jmeter(十五)Jmeter默认报告优化

    一.本文目的: 之前写了两篇文章搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)和ANT批量执行Jmeter脚本,功能实现上都没有什么问题,但是最后生成的报告有一点小问题,虽然不影响使 ...

  5. Python 设计模式--简单工厂模式

    简单工厂模式(Factory Pattern)是一种创建型的设计模式,像工厂一样根据要求生产对象实例. 特点:根据不同的条件,工厂实例化出合适的对象. <大话设计模式>中实例:四则运算计算 ...

  6. Elasticsearch--集群&吞吐量

    目录 高查询和高吞吐量 过滤器缓存 字段数据缓存和断路器 断路器 存储模块 索引缓冲和刷新率 索引刷新率 线程池的配置 一些通用的建议来配置高索引和查询吞吐量的集群 高查询和高吞吐量 过滤器缓存 过滤 ...

  7. php(三)使用PDO链接数据库

    1.启动 mysql数据库,打开图形化控制界面 2.新建一个数据库 3.创建一个数据表 4.给数据表添加数据 id是数字类型的  类型选择int长度 11 username 等其他数据  会是字符串形 ...

  8. java 线程池第一篇 之 ThreadPoolExcutor

    一:什么是线程池? java 线程池是将大量的线程集中管理的类,包括对线程的创建,资源的管理,线程生命周期的管理.当系统中存在大量的异步任务的时候就考虑使用java线程池管理所有的线程.减少系统资源的 ...

  9. preg_replace_callback使用方法

    官网解释: 执行一个正则表达式搜索并且使用一个回调进行替换 (PHP 4 >= 4.0.5, PHP 5) preg_replace_callback — 执行一个正则表达式搜索并且使用一个回调 ...

  10. photoshop cs6安装和破解步骤

    http://tieba.baidu.com/p/4791130877 http://www.frontopen.com/1181.html