2018 ICPC青岛网络赛 B. Red Black Tree(倍增lca好题)
BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. Among the vertices, of them are red, while the others are black. The root of the tree is vertex 1 and it's a red vertex.
Let's define the cost of a red vertex to be 0, and the cost of a black vertex to be the distance between this vertex and its nearest red ancestor.
Recall that
The length of a path on the tree is the sum of the weights of the edges in this path.
The distance between two vertices is the length of the shortest path on the tree to go from one vertex to the other.
Vertex is the ancestor of vertex v if it lies on the shortest path between vertex and the root of the tree (which is vertex 1 in this problem).
As BaoBao is bored, he decides to play q games with the tree. For the i-th game, BaoBao will select ki vertices v1,1,vi,2,...,vi,ki on the tree and try to minimize the maximum cost of these ki vertices by changing at most one vertex on the tree to a red vertex.
Note that
BaoBao is free to change any n vertex among all the vertices to a red vertex, NOT necessary among the ki vertiecs whose maximum cost he tries to minimize.
All the q games are independent. That is to say, the tree BaoBao plays with in each game is always the initial given tree, NOT the tree modified during the last game by changing at most one vertex.
Please help BaoBao calculate the smallest possible maximum cost of the given ki vertices in each game after changing at most one vertex to a red vertex.
Input
There are multiple test cases. The first line of the input is an integer T, indicating the number of test cases. For each test case:
The first line contains three integers n,m and q (2≤m≤n≤105,1≤q≤2×105), indicating the size of the tree, the number of red vertices and the number of games.
The second line contains m integers r1,r2,...,rm (1=r1<r2<...<rm≤n), indicating the red vertices.
The following (n-1) lines each contains three integers ui,vi and wi (1≤ui,vi≤n,1≤wi≤109 ), indicating an edge with wi weight connecting vertex ui and vi in the tree.
For the following q lines, the i-th line will first contain an integer ki(1≤ki≤n). Then ki integers vi,1,vi,2,...viki follow (1≤vi,1<vi,2<...<viki≤n), indicating the vertices whose maximum cost BaoBao has to minimize.
It's guaranteed that the sum of in all test cases will not exceed , and the sum of in all test cases will not exceed .
Output
For each test case output q lines each containing one integer, indicating the smallest possible maximum cost of the ki vertices given in each game after changing at most one vertex in the tree to a red vertex.
Sample Input
2
12 2 4
1 9
1 2 1
2 3 4
3 4 3
3 5 2
2 6 2
6 7 1
6 8 2
2 9 5
9 10 2
9 11 3
1 12 10
3 3 7 8
4 4 5 7 8
4 7 8 10 11
3 4 5 12
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3
Sample Output
4
5
3
8
0
0
0
Hint
The first sample test case is shown above. Let's denote C(v) as the cost of vertex v .
For the 1st game, the best choice is to make vertex 2 red, so that C(3)=4,C(7)=3 and C(8)=4. So the answer is 4.
For the 2nd game, the best choice is to make vertex 3 red, so that C(4)=3,C(5)=2,C(7)=4 and C(8)=5. So the answer is 5.
For the 3rd game, the best choice is to make vertex 6 red, so that C(7)=1,C(8)=2,C(10)=2 and C(11)=3. So the answer is 3.
For the 4th game, the best choice is to make vertex 12 red, so that C(4)=8,C(5)=7 and C(12)=0. So the answer is 8.
Due to the design restrictions of ZOJ, we can only provide a subset of the testing data here (the original data is too large for ZOJ to store). We will update the testing data once we update ZOJ. Sorry for the inconvenience caused.
题意
给出一棵树,其中某些点是红色,其余点是黑色。定义一个点的花费为这个点到距其最近的红色祖先节点的距离。q次查询,每次查询给出k个节点,允许将最多一个黑色点变为红色, 求这k个点中最大花费的最小值。每次查询相互独立,不影响树的初始结构。
题解
dfs处理出每个点距离1点的距离D,每个点距离红色祖先的距离dis
倍增lca预处理,用于查询公共祖先
每个查询q
先将k个点按dis从大到小排序
每次处理第i个节点
1.找到和前一个点的公共祖先
2.如果深度<上一个深度,说明然红的点在上面,需要把前面的最大值+这一段距离
3.然后当前节点本身的dis和把公共祖先染红的新距离取最小
4.2,3操作得到的值取max
5.4操作得到的max和下一个点的dis取max再整体取min,因为有可能节点操作后值变小了,使得最大值为下一个节点
代码
#include<bits/stdc++.h>
using namespace std; #define fi first
#define se second
#define LL long long const int maxn=1e5+;
const int INF=0x3f3f3f3f; int R[maxn],cnt[maxn],fa[maxn][],deep[maxn],n,m;
LL D[maxn],dis[maxn];
bool red[maxn];
vector< pair<int,int> >G[maxn]; void dfs(int x,int f)
{
if(red[x])R[x]=x;
else R[x]=R[f];
dis[x]=D[x]-D[R[x]];
for(auto v:G[x])
{
if(v.fi==f)continue;
D[v.fi]=D[x]+v.se;
deep[v.fi]=deep[x]+;
fa[v.fi][]=x;
dfs(v.fi,x);
}
}
bool cmp(LL a,LL b)
{
return dis[a]>dis[b];
}
void RMQ()
{
for(int j=;(<<j)<=n;j++)
for(int i=;i<=n;i++)
fa[i][j]=fa[fa[i][j-]][j-];
}
int LCA(int u,int v)
{
if(deep[u]<deep[v])swap(u,v);
int de=log(deep[u])/log(2.0);
for(int i=de;i>=;i--)
if(deep[u]-(<<i)>=deep[v])
u=fa[u][i];
if(v==u)return u;
for(int i=de;i>=;i--)
if(fa[u][i]!=fa[v][i])
u=fa[u][i],v=fa[v][i];
return fa[v][];
}
int main()
{
int t,q,a,b,c,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<=n;i++)
red[i]=,G[i].clear();
for(int i=;i<=m;i++)
scanf("%d",&a),red[a]=;
for(int i=;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
G[a].push_back(make_pair(b,c));
G[b].push_back(make_pair(a,c));
}
D[]=dis[]=dis[n+]=;
dfs(,);
RMQ();
while(q--)
{
scanf("%d",&k);
LL ans=,maxx=,lon;
for(int i=;i<k;i++)
scanf("%d",&cnt[i]);
cnt[k]=n+;
sort(cnt,cnt+k,cmp);
int far=cnt[];
ans=min(dis[cnt[]],dis[cnt[]]);
for(int i=;i<k;i++)
{
int mom=LCA(far,cnt[i]);//找上一个和当前的公共祖先,染红mom节点
if(deep[mom]<deep[far])maxx+=D[far]-D[mom];//如果新祖先深度<上一个祖先深度,最大的距离需要+两个祖先之间的距离
lon=min(dis[cnt[i]],D[cnt[i]]-D[mom]);//当前节点本身距离红色祖先的距离和把到mom染红后的节点距离取min
maxx=max(lon,maxx);
far=mom;
ans=min(ans,max(maxx,dis[cnt[i+]]));//保证每次处理最大值
}
printf("%lld\n",ans);
}
}
return ;
}
2018 ICPC青岛网络赛 B. Red Black Tree(倍增lca好题)的更多相关文章
- 2018 icpc 青岛网络赛 J.Press the Button
Press the Button Time Limit: 1 Second Memory Limit: 131072 KB BaoBao and DreamGrid are playing ...
- 2018 ICPC 沈阳网络赛
2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...
- 2018 ICPC 徐州网络赛
2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...
- 2018 ICPC 焦作网络赛 E.Jiu Yuan Wants to Eat
题意:四个操作,区间加,区间每个数乘,区间的数变成 2^64-1-x,求区间和. 题解:2^64-1-x=(2^64-1)-x 因为模数为2^64,-x%2^64=-1*x%2^64 由负数取模的性质 ...
- 2018 ICPC南京网络赛 L Magical Girl Haze 题解
大致题意: 给定一个n个点m条边的图,在可以把路径上至多k条边的权值变为0的情况下,求S到T的最短路. 数据规模: N≤100000,M≤200000,K≤10 建一个立体的图,有k层,每一层是一份原 ...
- 2018 icpc 徐州网络赛 F Features Track
这个题,我也没想过我这样直接就过了 #include<bits/stdc++.h> using namespace std; ; typedef pair<int,int> p ...
- 2018 ACM-ICPC 青岛网络赛
最近打比赛不知道为什么总是怀疑自己 写完之后不敢交,一定跟学长说一遍自己的思路 然后发现"哦原来我是对的" 然后就A掉了…… 所以还是要有自信 Problem A 最大值直接输出m ...
- 【2018 ICPC焦作网络赛 K】Transport Ship(多重背包二进制优化)
There are N different kinds of transport ships on the port. The ith kind of ship can carry the weigh ...
- 【2018 ICPC焦作网络赛 G】Give Candies(费马小定理+快速幂取模)
There are N children in kindergarten. Miss Li bought them N candies. To make the process more intere ...
随机推荐
- Django项目在linux系统中虚拟环境部署
1.在linux系统下,安装virtualenv 命令:pip install virtualenv 2.项目部署前的准备 1. Django web project deployment 1.1. ...
- oracle-logminer
LogMiner工具实际上是由两个新的PL/SQL内建包((DBMS_LOGMNR 和 DBMS_ LOGMNR_D)和四个V$动态性能视图(视图是在利用过程DBMS_LOGMNR.START_LOG ...
- 整数的故事(4)——Karastuba算法
我们在小学就学过用竖式计算两个多位数的乘法: 这个过程简单而繁琐,没有最强大脑的普通大众通常是用计算器代替的.然而对于超大整数的乘法,计算器也未必靠得住,它还存在“溢出”一说.这就需要我们自行编写算法 ...
- [转]阿里巴巴十年Java架构师分享,会了这个知识点的人都去BAT了
1.源码分析专题 详细介绍源码中所用到的经典设计思想,看看大牛是如何写代码的,提升技术审美.提高核心竞争力. 帮助大家寻找分析源码的切入点,在思想上来一次巨大的升华.知其然,并知其所以然.把知识变成自 ...
- Spark性能优化指南——高级篇
本文转载自:https://tech.meituan.com/spark-tuning-pro.html 美团技术点评团队) Spark性能优化指南——高级篇 李雪蕤 ·2016-05-12 14:4 ...
- Xtrabackup的安装与使用
Xtrabackup的安装与使用 1. XtraBackup 简介 XtraBackup(PXB) 工具是 Percona 公司用 perl 语言开发的一个用于 MySQL 数据库物理热备的备份工具, ...
- 【python】如何将ipdb的python解释器路径切换至虚拟环境中
背景: 利用virtualenv构建一个python3.5的虚拟环境,在该虚拟环境中使用ipdb调试程序,结果报错找不到某一个模块. 程序的所有依赖模块都已经成功安装在虚拟环境中. 在虚拟环境中,te ...
- Substring方法(C#,JS,Java,SQL)的区别
C#: substring(第一参数,第二参数)// 第一参数:从第几位开始截,初始是从0位开始 第二参数:截取几位 substring(参数) 如果传入参数为一个长整, 且大于等于0,则以这个 ...
- Java性能调优(一):调优的流程和程序性能分析
https://blog.csdn.net/Oeljeklaus/article/details/80656732 Java性能调优 随着应用的数据量不断的增加,系统的反应一般会越来越慢,这个时候我 ...
- List转Json函数
public string ObjectToJson<T>(string jsonName, IList<T> IL) { StringBuilder Json = new S ...