hdu5296(2015多校1)--Annoying problem(lca+一个公式)
Annoying problem
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 483 Accepted Submission(s): 148
Now there are two kinds of operation:
1 x: If the node x is not in the set S, add node x to the set S
2 x: If the node x is in the set S,delete node x from the set S
Now there is a annoying problem: In order to select a set of edges from tree after each operation which makes any two nodes in set S connected. What is the minimum of the sum of the selected edges’ weight ?
For each test:
The first line has 2 integer number n,q(0<n,q<=100000) describe the number of nodes and the number of operations.
The following n-1 lines each line has 3 integer number u,v,w describe that between node u and node v has an edge weight w.(1<=u,v<=n,1<=w<=100)
The following q lines each line has 2 integer number x,y describe one operation.(x=1 or 2,1<=y<=n)
The next q line represents the answer to each operation.
1
6 5
1 2 2
1 5 2
5 6 2
2 4 2
2 3 2
1 5
1 3
1 4
1 2
2 5
Case #1:
0
6
8
8
4
题目大意:给出一棵树,每一个边都有一个权值,如今有一个空的集合,两种操作,1 x吧x节点放到集合中(假设还没放入),2 x把x节点从集合中拿出来(已放入)。如今要求将集合中的点之间的边权之和
dfn[u] - dfn[ lca(x,u) ] - dfn[ lca(y,u) ] + dfn[ lca(x,y) ]
神一样的公式呀,表示比赛时根本就没想过要推公式,,。,,
先说这个公式怎么用,首先dfs一个顺序,加一个节点u。假设u节点的dfs序,在集合中节点的dfs序之间,那么找到最接近的(u的dfs序)的两个数为x和y;假设u节点的dfs序在集合中节点的dfs序的一側,那么x和y为集合中dfs序的最大值和最小值,,,,,这样带入公式中求的就是加入这个节点所带来的须要加入的距离。删除一个节点和加入时一样的。
如果节点要连接到一个链中,链的定点(x,y),那么u连接到x的距离是dfn[u] + dfn[x] - 2dfn[ lca(u,x) ] ;
u连接到y的距离dfn[u] + dfn[y] - 2dfn[ lca(u,x) ] :
x连接到y的距离dfn[x] + dfn[y] - 2dfn[ lca(x,y) ] :
u连接到x-y这个链的距离 = (u到y+u到x-x到y)/2
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
using namespace std ;
#define maxn 100050
struct E{
int v , w ;
int next ;
}edge[maxn<<1];
int head[maxn] , cnt ;
int rmq[maxn][20] ;
int dep[maxn] , p[maxn] , belong[maxn] , cid ;
int vis[maxn] , dfn[maxn] ;
set<int> s ;
set<int>::iterator iter ;
void add(int u,int v,int w) {
edge[cnt].v = v ; edge[cnt].w = w ;
edge[cnt].next = head[u] ; head[u] = cnt++ ;
edge[cnt].v = u ; edge[cnt].w = w ;
edge[cnt].next = head[v] ; head[v] = cnt++ ;
}
void dfs(int fa,int u) {
int i , j , v ;
p[u] = ++cid ;
belong[cid] = u ;
for(i = head[u] ; i != -1 ; i = edge[i].next ) {
v = edge[i].v ;
if( v == fa ) continue ;
dfn[v] = dfn[u] + edge[i].w ;
rmq[v][0] = u ;
for(j = 1 ; j < 19 ; j++)
rmq[v][j] = rmq[ rmq[v][j-1] ][j-1] ;
dep[v] = dep[u] + 1 ;
dfs(u,v) ;
}
}
int lca(int u,int v) {
if( dep[u] < dep[v] ) swap(u,v) ;
int i ;
for(i = 19 ; i >= 0 ; i--) {
if( dep[ rmq[u][i] ] >= dep[v] )
u = rmq[u][i] ;
if( u == v ) return u ;
}
for(i = 19 ; i >= 0 ; i--) {
if( rmq[u][i] != rmq[v][i] ) {
u = rmq[u][i] ;
v = rmq[v][i] ;
}
}
return rmq[u][0] ;
}
int solve(int u) {
if( s.empty() ) return 0 ;
int x , y ;
iter = s.upper_bound(u) ;
if( iter == s.end() || iter == s.begin() ) {
x = belong[ *s.begin() ] ;
y = belong[ *s.rbegin() ] ;
}
else {
x = belong[*iter] ;
iter-- ;
y = belong[*iter] ;
}
u = belong[u] ;
return dfn[u] - dfn[ lca(x,u) ] - dfn[ lca(y,u) ] + dfn[ lca(x,y) ] ;
}
int main() {
int Step = 0 , t ;
int n , m ;
int i , j , u , v , w , k ;
int ans ;
scanf("%d", &t) ;
while( t-- ) {
memset(head,-1,sizeof(head)) ;
memset(rmq,0,sizeof(rmq)) ;
memset(dfn,0,sizeof(dfn)) ;
memset(vis,0,sizeof(vis)) ;
cnt = cid = ans = 0 ;
s.clear() ;
scanf("%d %d", &n, &m) ;
for(i = 1 ; i < n ; i++) {
scanf("%d %d %d", &u, &v, &w) ;
add(u,v,w) ;
}
dep[1] = 1 ;
dfs(-1,1) ;
printf("Case #%d:\n", ++Step) ;
while( m-- ) {
scanf("%d %d", &k, &u) ;
u = p[u] ;
if( k == 1 ) {
if( !vis[u] ) {
vis[u] = 1 ;
ans += solve(u) ;
s.insert(u) ;
}
}
else {
if( vis[u] ) {
vis[u] = 0 ;
s.erase(u) ;
ans -= solve(u) ;
}
}
printf("%d\n", ans) ;
}
}
return 0 ;
}
hdu5296(2015多校1)--Annoying problem(lca+一个公式)的更多相关文章
- HDOJ 5296 Annoying problem LCA+数据结构
dfs一遍得到每一个节点的dfs序,对于要插入的节点x分两种情况考虑: 1,假设x能够在集合中的某些点之间,找到左边和右边距离x近期的两个点,即DFS序小于x的DFS序最大点,和大于x的DFS序最小的 ...
- HDU 5296 Annoying problem LCA+树状数组
题解链接 Annoying problem Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- HDU5296 Annoying problem(LCA)
//#pragma comment(linker, "/STACK:1677721600") #include <map> #include <set> # ...
- HDU 5296 Annoying problem (LCA,变形)
题意: 给一棵n个节点的树,再给q个操作,初始集合S为空,每个操作要在一个集合S中删除或增加某些点,输出每次操作后:要使得集合中任意两点互可达所耗最小需要多少权值.(记住只能利用原来给的树边.给的树边 ...
- 2015 Multi-University Training Contest 1 hdu 5296 Annoying problem
Annoying problem Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 2015 Multi-University Training Contest 1 - 1009 Annoying problem
Annoying problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5296 Mean: 给你一个有根树和一个节点集合 ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- HDU 5296 Annoying problem dfs序 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5296 Description Coco has a tree, w ...
- HDU 5296 Annoying problem dfs序 lca set
Annoying problem Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,…, ...
随机推荐
- [LOJ#114]k 大异或和
[LOJ#114]k 大异或和 试题描述 这是一道模板题. 给由 n 个数组成的一个可重集 S,每次给定一个数 k,求一个集合 T⊆S,使得集合 T 在 S 的所有非空子集的不同的异或和中,其异或和 ...
- BZOJ 3786 星系探索 ——Splay
子树可以移动,唔. 还是用Splay维护DFS序即可. 子树的话直接截取出来就好了. 然后求前驱后继可能麻烦一些. 添加两个虚拟节点会比较好写. #include <map> #inclu ...
- 刷题总结——(一道很妙的题)Resistance(ssoj 欧几里得 )
题解: 题目背景 151006 T1 题目描述 Picks 喜欢电路.这天他在研究元电路的时候,需要一个阻值为 (p/q)Ω 的电阻,然而他家中只有一大堆电阻为 1Ω 电阻.由于技术问题,Picks ...
- [暑假集训--数论]poj2657 Comfort
Description A game-board consists of N fields placed around a circle. Fields are successively number ...
- 部署 DevStack
本节按照以下步骤部署 DevStack 实验环境,包括控制节点和计算节点 创建虚拟机 按照物理资源需求创建 devstack-controller 和 devstak-compute 虚拟机 安装操作 ...
- 13深入理解C指针之---内存管理
该系列文章源于<深入理解C指针>的阅读与理解,由于本人的见识和知识的欠缺可能有误,还望大家批评指教. 内存管理对所有程序都很重要,主要包括显式内存管理和隐式内存管理.其中隐式内存管理主要是 ...
- 使用 PHPMailer 发邮件
/** * 发邮件 * * @param array $receiver 接收人信息 * @param array $attachment_info 附件信息 * @param string $is_ ...
- c#FileStream文件读写
//C#文件流写文件,默认追加FileMode.Append string msg = "okffffffffffffffff"; b ...
- 字蛛(font-spider)-单独压缩字体(解决页面少有的特殊字体的字体包引用)
特别想独立的把这个问题写成一篇内容,分享给大家. 反正我是这个字体压缩使用的受益者,不是打广告. 很久以前,设计师总是爱用一些奇奇怪怪的字体放在页面上,而作为前端我们很容易的就能直接使用TA们用到的字 ...
- codeforces gym 100825 D Rings
这题果然就是个暴力题.... 看每个T的四个方向,有'.',或者在边界上就填1 不然就填四个方向上最小的那个数再加1 然而写wa了几发,有点蠢... #include <bits/stdc++. ...