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 ...
随机推荐
- 【Linux学习】python脚本直接运行与nohup运行结果不同
之前遇到问题,在云主机上运行python脚本直接运行与nohup运行结果不同,甚至nohup根本运行不出来. 后来参考下别人的博客,终于知道问题了. nohup 使用的python版本问题. 而且no ...
- python基础===string模块常量
In [8]: import string In [9]: dir(string) In [10]: string.ascii_letters Out[10]: 'abcdefghijklmnopqr ...
- [MySQL] AUTO_INCREMENT lock Handing in InnoDB
MySQL AUTO_INCREMENT lock Handing in InnoDB 在MySQL的表设计中很普遍的使用自增长字段作为表主键, 实际生产中我们也是这样约束业务开发同学的, 其中的优势 ...
- 【BubbleCup X】G:Bathroom terminal
一个hash的题 对?出现位置直接暴力枚举,然后hash判断下,扔进map里 cf的评测机跑的针tm块 #include<bits/stdc++.h> ; ; typedef long l ...
- HDFS RAID实现方案(转)
原文链接:http://blog.chinaunix.net/uid-20196318-id-3213700.html 分布式文件系统主要用于解决海量数据存储的问题,如Goolge.Facebook等 ...
- .htaccess教程:简介、访问控制、验证、目录浏览控制
一..htaccess简介 1.什么是.htaccess .htaccess是一个纯文本文件,里面存放着Apache服务器配置相关的一些指令,它类似于Apache的站点配置文件,如httpd.conf ...
- MySQL JDBC驱动下载
下载地址:https://pan.baidu.com/s/1VLNaV_rz2P1jMtYrjJydiQ
- java并发编程实战笔记---(第三章)对象的共享
3.1 可见性 synchronized 不仅实现了原子性操作或者确定了临界区,而且确保内存可见性. *****必须在同步中才能保证:当一个线程修改了对象状态之后,另一个线程可以看到发生的状态变化. ...
- office 文档转pdf
本地先安装 金山wps,并确保可用 工程目录 1.使用前,先执行install.bat 安装jacob 到maven本地仓库 2.复制 jacob-1.18-M2-x64.dlljacob-1.18- ...
- 木块问题(UVa101)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...