G. Robots
time limit per test

5.0 s

memory limit per test

256 MB

input

standard input

output

standard output

The Robotics Olympiad teams were competing in a contest.

There was a tree drawn on the floor, consisting of n nodes and n - 1 edges. The nodes are numbered from 1 to n, and each edge has a weight. The tree is rooted at the first node. q teams are participating, and each team is given an integer xi. Their robot should start at node 1, and move in the following way until there are no valid moves left: From all the edges between the current node and it's children, go through the edge with the maximum value less than xi. Note that the robot can't move to the parent, only to children.

However, the teams weren't able to program the robots to return to them after the contest, so they had to manually pick them up. Since the tree can be quite large, they need your help to determine where each robot ended it's movement.

Input

The first line contains a single integer T, the number of test cases.

The first line of each test case contains two space-separated integers n and q. (1 ≤ n, q ≤ 105).

The following n - 1 lines contain 3 integers ui, vi, wi. This means that there is an edge connecting nodes ui and vi, with weight wi. (1 ≤ ui, vi ≤ n) (1 ≤ wi ≤ 109). It's guaranteed that all wi are distinct.

The following line contains q integers xi. (1 ≤ xi ≤ 109).

Output

For each test case, print one line with a single number Si, the sum of numbers of nodes where each robot ends.

Example
input

Copy
1
5 7
1 2 3
1 3 4
3 4 9
3 5 7
1 3 4 9 8 7 10
output

Copy
21
Note

In the sample test case, the robots end in the following nodes: {1, 1, 2, 5, 5, 3, 4}.

Si = 1+1+2+5+5+3+4 = 21.

Large I/O files. Please consider using fast input/output methods.

【题意】

有n个点,n-1条边,每条边有权值。现在有M个机器人,他们也有自己的权值,他们会从点1开始顺着这颗树向下走,每个机器人能走的边是那些边的权值严格小于机器人权值的边。但是机器人是有选择的,它只会走那些能选择的边里面权值尽可能大的边走,并且它们是无法回头的。

现在询问给定的M个机器人最后停留在哪些点,求这些点的和


【分析】

把每个点连出去的边按照权值降序排序,这样保证在之后的遍历中优先选择边权大的(贪心)。每个机器人的最后停留位置是固定的,所以他们谁先走的顺序是无所谓的,只有权值才是我们需要的。所以按照机器人的权值从大到小也排序一遍,这里用优先队列来更新这个信息。

首先预处理出达到每个点所需要的最大的权值大小用mx[ ]记录。再按照DFS的顺序到达每个无法再向下的点的时候比较该点的mx[ i
]值与当前权值最大的机器人的权值,若机器人的权值大于mx[ i
]则表示这个机器人是可以到达这里的,此时更新答案并且把这个机器人从队列中取出。这样操作直到队头元素不满足权值大于mx[ i
],然后开始回溯,这样就可以保证每个机器人一定是达到了他们固定的位置。
 


【代码】

#include<queue>
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+5;
struct E{
int u,v,w;
E(int _u=0,int _v=0,int _w=0){u=_u;v=_v;w=_w;}
inline bool operator <(const E &a)const{
return w>a.w;
}
};
vector<E>e[N];
priority_queue<int>q;
int n,m,cas,mx[N];long long ans;
void dfs(int x,int fa){
int sz=e[x].size();
for(int i=0;i<sz;i++){
int v=e[x][i].v;
if(v!=fa){
mx[v]=max(mx[x],e[x][i].w);
dfs(v,x);
}
}
}
void DFS(int x,int fa){
if(q.empty()||q.top()<=mx[x]) return ;
int sz=e[x].size();
for(int i=0;i<sz;i++){
int v=e[x][i].v;
if(v!=fa&&q.top()>mx[v]&&!q.empty()){
DFS(v,x);
}
}
for(;q.top()>mx[x]&&!q.empty();q.pop()) ans+=x;
}
inline void Clear(){
ans=0;
for(;!q.empty();q.pop());
for(int i=1;i<=n;i++) e[i].clear(),mx[i]=0;
}
inline void Init(){
scanf("%d%d",&n,&m);
for(int i=1,x,y,z;i<n;i++) scanf("%d%d%d",&x,&y,&z),e[x].push_back(E(x,y,z)),e[y].push_back(E(y,x,z));
for(int i=1,x;i<=m;i++) scanf("%d",&x),q.push(x);
for(int i=1;i<=n;i++) sort(e[i].begin(),e[i].end());
}
inline void Solve(){
dfs(1,0);
DFS(1,0);
printf("%I64d\n",ans);
}
int main(){
for(scanf("%d",&cas);cas--;Clear()){
Init();
Solve();
}
return 0;
}

 

Gym 101915G Robots的更多相关文章

  1. Robots Gym - 101915G

    传送门 The Robotics Olympiad teams were competing in a contest. There was a tree drawn on the floor, co ...

  2. Gym 101915

    Gym - 101915A  Printing Books 题意:有一本书,从第X页开始,一共用了n位数字,求此书一共多少页.99就是两位数字,100就是三位数字. 思路:直接模拟即可,我用了一个hi ...

  3. 要back的题目 先立一个flag

    要back的题目 目标是全绿!back一题删一题! acmm7 1003 1004 acmm8 1003 1004 sysu20181013 Stat Origin Title Solved A Gy ...

  4. gym 100971 J Robots at Warehouse

    Vitaly works at the warehouse. The warehouse can be represented as a grid of n × m cells, each of wh ...

  5. 【Gym 100971J】Robots at Warehouse

    题意 链接给你一个n*m的地图,'#'代表墙,‘.’代表可走的,1代表1号机器人,2代表2号机器人,机器人可以上下左右移动到非墙的位置,但不能走到另一个机器人身上.问能否交换1和2的位置. 分析 如果 ...

  6. Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

    Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...

  7. Gym 100971J-Robots at Warehouse

    题目链接:http://codeforces.com/gym/100971/problem/J Vitaly works at the warehouse. The warehouse can be ...

  8. Gym - 100971J (思维+简单bfs)

    题目链接:http://codeforces.com/gym/100971/problem/J J. Robots at Warehouse time limit per test 2.0 s mem ...

  9. (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest

    layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest author: "luow ...

随机推荐

  1. Hive性能分析和优化方法

    Hive性能分析和优化方法 http://wenku.baidu.com/link?url=LVrnj-mD0OB69-eUH-0b2LGzc2SN76hjLVsGfCdYjV8ogyyN-BSja5 ...

  2. ★Wireshark基本介绍和学习TCP三次握手

    之前写过一篇博客:用 Fiddler 来调试HTTP,HTTPS. 这篇文章介绍另一个好用的抓包工具wireshark, 用来获取网络数据封包,包括http,TCP,UDP,等网络协议包. 记得大学的 ...

  3. 近阶段学习总结(EasyUI的使用)

    最近阶段正在学习Js框架的使用,目前正在详细了解JQuery EasyUI 的使用. jQuery EasyUI 框架帮助我们轻松建立站点: easyui是一个基于jquery的集成了各种用户界面的插 ...

  4. LinuxMint系统下Gate One的安装指南

    1. Gate One简介 前面有两个随笔介绍过开源软件tty.js和wetty在Linux的安装.Tty.js和wetty都是采用Node.js实现的开源Web-based ssh.今天来介绍另一个 ...

  5. Spring的JdbcTemplate与其事务

    JdbcTemplate:http://lehsyh.iteye.com/blog/1579737 JdbcTemplate with TransactionTemplate:http://www.c ...

  6. JSP求和计算

    已知两个数的值,如何求和并输出? <%@ page language="java" import="java.util.*,java.text.*" co ...

  7. Vim终极指南:所思即所得

    https://blog.csdn.net/dc_726/article/details/78981756

  8. Ajax请求全局配置

    摘要: jQuery已经成为项目中最常见的js库,也是前端开发最喜欢使用的库.下面是在项目中封装了jQuery的Ajax,分享给大家. 代码: // ajax 请求参数 var ajaxSetting ...

  9. 2018年7月份,python上传自己的包库到pypi官网的方法

    最近pypi官网进行了更新,老的上传网址作废了.记录下上传到pypi的方法 0.去pypi官网注册账号,没账号是不可能上传的,想想也是那不乱套了吗,注册后会收到一个邮件需要点击然后重新登录 1.目录就 ...

  10. iOS使用NSURLConnection发送同步和异步HTTP Request

    1. 同步发送 - (NSString *)sendRequestSync { // 初始化请求, 这里是变长的, 方便扩展 NSMutableURLRequest *request = [[NSMu ...