HDU 5296 Annoying problem dfs序 lca
Annoying problem
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5296
Description
Coco has a tree, whose nodes are conveniently labeled by 1,2,…,n, which has n-1 edge,each edge has a weight. An existing set S is initially empty.
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 ?
Input
one integer number T is described in the first line represents the group number of testcases.( T<=10 )
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)
Output
Each testcase outputs a line of "Case #x:" , x starts from 1.
The next q line represents the answer to each operation.
Sample Input
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
Sample Output
Case #1:
0
6
8
8
4
Hint
题意
给你一棵树,现在有两个操作
1.把某个点染成黑色
2.把某个点染成白色
然后每次操作结束后,问你黑色点构成的树的所有边权和是多少。
一开始全是白色的点。
题解:
首先黑色点构成的树一定是唯一的。
然后我们加入一个点,就只用考虑以前那堆点中dfs序比他小和比他大的点u,v。
如果找不到的话,找字典序最大和最小的点就好了。
然后这棵树增加的距离就是dis(x,u)+dis(x,v)+dis(u,v)
删除点也是一样的。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int flag[maxn],idx[maxn],cnt=1,n,q,LCA[maxn][21],deep[maxn],G[maxn][21];
vector<pair<int,int> >E[maxn];
set<pair<int,int> >S;
long long ans = 0;
void init()
{
ans = 0;
S.clear();cnt=1;
memset(idx,0,sizeof(idx));
memset(flag,0,sizeof(flag));
memset(LCA,0,sizeof(LCA));
memset(G,0,sizeof(G));
memset(deep,0,sizeof(deep));
for(int i=0;i<maxn;i++)E[i].clear();
}
void dfs(int x,int fa)
{
idx[x]=cnt++;
for(int i=0;i<E[x].size();i++)
{
int v = E[x][i].first;
if(v==fa)continue;
LCA[v][0]=x,deep[v]=deep[x]+1,G[v][0]=E[x][i].second;
dfs(v,x);
}
}
long long QueryDis(int u , int v){
long long ans = 0;
if(deep[u] < deep[v]) swap( u , v );
for(int i = 20 ; i >= 0 ; -- i ) if( deep[u] - (1 << i) >= deep[v] ) ans += G[u][i],u = LCA[u][i];
if( u == v ) return ans;
for(int i = 20 ; i >= 0 ; -- i ) if( LCA[u][i] != LCA[v][i] ) ans += (G[u][i] + G[v][i]) , u = LCA[u][i] , v = LCA[v][i];
return ans + G[u][0] + G[v][0];
}
void Lca_init()
{
for(int j = 1 ; j <= 20 ; ++ j)
for(int i = 1 ; i <= n ; ++ i)
if(LCA[i][j-1]){
LCA[i][j]=LCA[LCA[i][j-1]][j-1];
G[i][j] = G[i][j-1] + G[LCA[i][j-1]][j-1];
}
}
long long solve(int x)
{
if(S.size()==0)return 0;
set<pair<int,int> >::iterator it;
it=S.lower_bound(make_pair(idx[x],x));
int d1,d2;
if(it==S.begin()||it==S.end())
{
d1=S.begin()->second;
d2=S.rbegin()->second;
}
else
{
d1=it->second;
it--;
d2=it->second;
}
return QueryDis(d1,x)+QueryDis(d2,x)-QueryDis(d1,d2);
}
void solve()
{
init();
scanf("%d%d",&n,&q);
for(int i=1;i<n;i++)
{
int x,y,z;scanf("%d%d%d",&x,&y,&z);
E[x].push_back(make_pair(y,z));
E[y].push_back(make_pair(x,z));
}
dfs(1,-1);
Lca_init();
while(q--)
{
int op,x;scanf("%d%d",&op,&x);
if(op==1&&flag[x]==1);
else if(op==2&&flag[x]==0);
else if(op==1)flag[x]=1,ans+=solve(x),S.insert(make_pair(idx[x],x));
else if(op==2)flag[x]=0,S.erase(make_pair(idx[x],x)),ans-=solve(x);
printf("%lld\n",ans/2);
}
}
int main()
{
int t;scanf("%d",&t);
for(int i=1;i<=t;i++)
{
printf("Case #%d:\n",i);
solve();
}
return 0;
}
HDU 5296 Annoying problem dfs序 lca的更多相关文章
- HDU 5296 Annoying problem dfs序 lca set
Annoying problem Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,…, ...
- HDU 5296 Annoying problem (LCA,变形)
题意: 给一棵n个节点的树,再给q个操作,初始集合S为空,每个操作要在一个集合S中删除或增加某些点,输出每次操作后:要使得集合中任意两点互可达所耗最小需要多少权值.(记住只能利用原来给的树边.给的树边 ...
- HDU 5296 Annoying problem LCA+树状数组
题解链接 Annoying problem Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- 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 hdu 5296 Annoying problem
Annoying problem Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu_5293_Tree chain problem(DFS序+树形DP+LCA)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5293 被这题打蹦了,看着题解写的,很是爆炸,确实想不到,我用的DFS序+LCA+树形DP,当然也可以写 ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- HDOJ 5296 Annoying problem LCA+数据结构
dfs一遍得到每一个节点的dfs序,对于要插入的节点x分两种情况考虑: 1,假设x能够在集合中的某些点之间,找到左边和右边距离x近期的两个点,即DFS序小于x的DFS序最大点,和大于x的DFS序最小的 ...
- HDU 3966 dfs序+LCA+树状数组
题目意思很明白: 给你一棵有n个节点的树,对树有下列操作: I c1 c2 k 意思是把从c1节点到c2节点路径上的点权值加上k D c1 c2 k 意思是把从c1节点到c2节点路径上的点权值减去k ...
随机推荐
- 3-Python内置结构-列表
目录 1 Python内置数据结构 1.1 数值型 1.2 math模块 1.3 round圆整 1.4 常用的其他函数 1.5 类型判断 2 列表 2.1 索引访问 2.2 列表和链表的区别 2.3 ...
- SQLserver连接本地服务器
1.打开SQLserver “连接到服务器” 2.服务器类型:数据库引擎 3.服务器名称:浏览更多->本地服务器->数据库引擎->选择本地服务器 4.身份验证:windows验证 5 ...
- JSOI 2017 Round 1滚粗记
day0 到常州一中报道,吃了午饭,好像这次有小火锅. 然后下午听JYY讲线性规划...好神啊. 晚上去试机,机子上没有npp,只有linux下的codeblocks,敲起来一顿一顿的...后来被迫使 ...
- WEB-INF下资源访问问题
原文链接:http://blog.csdn.net/u010921701/article/details/67637527 问题描述:建了一个Web工程,将js.css等放到了WEB-INF文件下,发 ...
- FAQ1: 列表索引和切片问题
问题1. 超过列表成员个数的索引访问列表会出现IndexError错误,但是如果用切片去访问就不会报错,而是返回一个空列表.同样元组也是. >>> a=[1,2,3,4] >& ...
- nginx 查看当前的连接数
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a,S[a]}' https://www.cnblogs.com/lianzhil ...
- java版云笔记(九)之动态sql
SQL 首先,所谓SQL的动态和静态,是指SQL语句在何时被编译和执行,二者都是用在SQL嵌入式编程中的,这里所说的嵌入式是指将SQL语句嵌入在高级语言中,而不是针对于单片机的那种嵌入式编程. 静态S ...
- springboot1.5.4 idea 自动保存编译更新
maven dependencies增加 <dependency> <groupId>org.springframework.boot</groupId> < ...
- [翻译]HLS实践
最近公司项目没事做,课余实践研究一下技术,算是积累,也可以用到项目里,从零开始记录 HLS:Http Live Streaming 官方文档 https://developer.apple.com/s ...
- 查看loadrunner运行日志
查看loadrunner运行日志 日志分两种 1.在VUGEN中运行后的日志 2.在controller中运行后的日志日志设置分两步: 1.首先,在VUGEN或controller中run-tim ...