ACM-ICPC2018 青岛赛区网络预赛-B- Red Black Tree
题目描述
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 u is the ancestor of vertex v if it lies on the shortest path between vertex v 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 vi,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 vertex among all the n 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.
输入
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 weight wi 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, . . . , vi,ki follow (1≤vi,1 < vi,2 < ... < vi,ki≤n), indicating the vertices whose maximum cost BaoBao has to minimize.
It’s guaranteed that the sum of n in all test cases will not exceed 106, and the sum of ki in all test cases will not exceed 2×106.
输出
样例输入
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
样例输出
4
5
3
8
0
0
0
提示
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.
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=+;
int T,n,m,q,cnt,tot;
bool red[N];
int last[N],pos[N],f[N],rmq[N],mm[N],dp[N][],a[N];
ll cost[N],cost_t[N];
struct tree{
int v,w,nex;
}t[N];
bool cmp(int a,int b)
{
return cost_t[a]>cost_t[b];
}
void add(int x,int y,int z)
{
cnt++;
t[cnt].v=y;
t[cnt].nex=last[x];
last[x]=cnt;
t[cnt].w=z;
}
void dfs(int x,int fa,int deep,ll dis,ll dis1)
{
if (red[x]) dis1=;
cost[x]=dis; cost_t[x]=dis1;
pos[x]=tot; f[tot]=x; rmq[tot++]=deep;
for (int i=last[x];i;i=t[i].nex)
{
if (t[i].v==fa) continue;
dfs(t[i].v,x,deep+,dis+t[i].w,dis1+t[i].w);
f[tot]=x;
rmq[tot++]=deep;
}
}
void ST(int n)
{
mm[]=-;
for (int i=;i<=n;i++)
{
mm[i]=((i&(i-))==) ? mm[i-]+:mm[i-];
dp[i][]=i;
}
for (int j=;j<=mm[n];j++)
for (int i=;i+(<<j)-<=n;i++)
dp[i][j]=rmq[dp[i][j-]]<rmq[dp[i+(<<(j-))][j-]] ? dp[i][j-] : dp[i+(<<(j-))][j-];
}
int query(int a,int b)
{
a=pos[a]; b=pos[b];
if (a>b) swap(a,b);
int k=mm[b-a+];
int ret=rmq[dp[a][k]]<=rmq[dp[b-(<<k)+][k]] ? dp[a][k] : dp[b-(<<k)+][k];
return f[ret];
}
int main()
{
scanf("%d",&T);
while (T--)
{
int x,y,z,k;
cnt=; tot=;
memset(red,,sizeof(red));
memset(last,, sizeof(last)); scanf("%d%d%d",&n,&m,&q);
for (int i=;i<=m;i++)
{
scanf("%d",&x);
red[x]=true;
}
for (int i=;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z); add(y,x,z);
} cost[]=cost_t[]=cost_t[n+]=;
dfs(,-,,,);
ST(tot-); while (q--)
{
scanf("%d",&k);
for (int i=;i<=k;i++) scanf("%d",&a[i]); sort(a+,a++k,cmp); a[k+]=n+; ll ans=cost_t[a[]],lon,maxx=;
int fa=a[];
for (int i=;i<=k;i++)
{
int new_fa=query(fa,a[i]);
int dep1=rmq[pos[fa]],dep2=rmq[pos[new_fa]];
if (dep2<dep1) maxx+=cost[fa]-cost[new_fa]; lon=min(cost_t[a[i]],cost[a[i]]-cost[new_fa]);
maxx=max(maxx,lon); fa=new_fa;
ans=min(ans,max(maxx,cost_t[a[i+]]));
}
printf("%lld\n",ans);
}
}
return ;
}
ACM-ICPC2018 青岛赛区网络预赛-B- Red Black Tree的更多相关文章
- ACM-ICPC 2018 青岛赛区网络预赛 J. Press the Button(数学)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4056 题意:有一个按钮,时间倒计器和计数器,在时间[0,t]内, ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)
ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...
- ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer (最大生成树+LCA求节点距离)
ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer J. Maze Designer After the long vacation, the maze designer ...
- ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)
https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...
- ACM-ICPC 2018 南京赛区网络预赛 J.sum
A square-free integer is an integer which is indivisible by any square number except 11. For example ...
- ACM-ICPC 2018 南京赛区网络预赛 E题
ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest wi ...
- ACM-ICPC 2018 南京赛区网络预赛B
题目链接:https://nanti.jisuanke.com/t/30991 Feeling hungry, a cute hamster decides to order some take-aw ...
- ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)
There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...
- ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
随机推荐
- 20181009-3 Scrum立会报告+燃尽图02
此作业要求:[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2190] 一.小组介绍 组长:王一可 组员:范靖旋,王硕,赵佳璐,范洪达,祁 ...
- 软件工程第九周psp
1.PSP表格 2.进度条 3.饼状图 4.折线图
- 第二次作业——个人项目实战(sudoku)
第二次作业--个人项目实战(sudoku) 一.作业要求地址 第二次作业--个人项目实战 二.Github项目地址 softengineering1--sudoku 三.PSP表格估计耗时 PSP2. ...
- Navicat for mysql导入.sql数据库大小受限制
把导入单个表的最大限制调一下就行(在my.ini里面就算改了max_allowed_packet也不一定行,因为Navicat貌似并不调用,实际他有自己的一套默认配置,所以需要在Navicat上调整) ...
- sql nolock是什么
百度:SQL Server 中的 NOLOCK 到底是什么意思? 文章地址:http://blog.sina.com.cn/s/blog_7d3b18a50100rfwg.html 查询语句加上 no ...
- 人生的第一篇blog
开始写博客了,人生第一篇博客啊,要写些什么呢?想想也没有什么头绪,随便写写吧. 这学期要使用代码管理工具了,要写团队项目了.一直以来都是自己一个人在默默编程,没有过合作经历.对于代码的管理也只是一直在 ...
- Java 线程安全问题
线程安全问题产生原因: 1.多个线程操作共享的数据: 2.操作共享数据的线程代码有多条. 当一个线程正在执行操作共享数据的多条代码过程中,其它线程也参与了运算, 就会导致线程安全问题的发生. cl ...
- [hook.js]通用Javascript函数钩子及其他
2013.02.16<:article id=post_content> 最近看Dom Xss检测相关的Paper,涉及到Hook Javascript函数,网上翻了一下,貌似没有什么通用 ...
- 使用ssh公钥登陆
记录一下使用的具体命令,具体参考: Centos设置禁止密码登录而只使用密钥登录SSH方法 优先参考这个. ssh使用公钥授权不通过的问题解决 Xshell配置ssh免密码登录-密钥公钥(Publi ...
- Maven编译打包出错:找不到符号
项目中,使用的是maven管理,但是有几个jar不是通过maven引入的,是通过IDEA导入的,在使用maven插件编译的时候,会出现如下的一些错误: 解决方法: 在项目中创建一个目录lib,然后将j ...