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. ROS学习笔记四:用C++编写ROS发布与订阅

    1 创建并编译功能包 1.1 创建功能包 在工作空间的 src 目录下创建功能包: $ cd ~/dev/catkin_ws/src $ catkin_create_pkg chapter2_tuto ...

  2. Base64编码与解码 分类: 中文信息处理 2014-11-03 21:58 505人阅读 评论(0) 收藏

    Base64是一种将二进制转为可打印字符的编码方法,主要用于邮件传输.Base64将64个字符(A-Z,a-z,0-9,+,/)作为基本字符集,把所有符号转换为这个字符集中的字符. 编码: 编码每次将 ...

  3. 用C#操作word替换字符,用spire

    这两天想写个小程序,是用C#操作word文档的.许多人都对微软本身的解决方案COM组件十分不看好,比如需要本机安装office等等,总之吐槽很多,直接放弃. 搜到一个国产的npoi库,据说操作简单功能 ...

  4. 【原】无脑操作:Eclipse + Maven + jFinal + MariaDB 环境搭建

    一.开发环境 1.windows 7 企业版 2.Eclipse IDE for Enterprise Java Developers  Version: 2019-03 (4.11.0) 3.JDK ...

  5. java基础(六):RabbitMQ 入门

    建议先了解为什么项目要使用 MQ 消息队列,MQ 消息队列有什么优点,如果在业务逻辑上没有此种需求,建议不要使用中间件.中间件对系统的性能做优化的同时,同时增加了系统的复杂性也维护难易度:其次,需要了 ...

  6. Struts2------拦截器和标签库和注解开发

    一.解析Struts2源码中拦截器的执行 客户端请求Action,执行前端控制器,在前端控制器内部创建了Action的代理类,调用代理类的execute方法,在execute方法内部执行ActionI ...

  7. Android ImageView setImageBitmap 不显示图片

    从sd卡里读出图片后有时调用setImageBitmap(bitmap)方法会显示不出图片,仔细考虑过后原来是加载的图片过大导致的,解决办法为: BitmapFactory.Options op = ...

  8. PPTP的搭建

    一.准备 1.检查是否支持pptp modprobe ppp-compress-18 && echo yes yes支持 2.是否开启tun cat /dev/net/tun 返回ca ...

  9. updating error reports database解决方案

    Window--->Preferences--->General--->Startup and Shutdown--->取消勾选Eclipse Automated Error  ...

  10. C# GDI+ 画坐标(x,y)

    private void button1_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics(); g.Clear ...