传送门

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
1
5 7
1 2 3
1 3 4
3 4 9
3 5 7
1 3 4 9 8 7 10
Output
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的带权无向树,定点编号为1-n,有q个机器人,每个机器人带一个值,要求所有机器人从顶点1出发,机器人权值严格大于边权且只能向孩子节点反向才能移动。问所有机器人终点顶点编号和。

思路:先dfs将所有的点到原点的最大权值记录下来,之后将机器人按从大到小排序,开始边跑边删

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 1e5+;
const int mod = 1e9+; struct edge
{
int v,w;
friend bool operator < (edge a,edge b)
{
return a.w > b.w;
}
};
vector<edge>V[maxn];
void addedeg(int u,int v,int w)
{
V[u].push_back(edge{v,w});
}
int maxx[maxn];
int vis[maxn];
void cal(int x,int fa)
{
if(V[x].size() == || x == -)
return;
for(int i=;i<V[x].size();i++)
{
int to = V[x][i].v;
if(to == fa)
continue;
maxx[to] = max(maxx[x],V[x][i].w);
cal(to,x);
}
}
priority_queue<int,vector<int>,less<int>> pq;
ll ans = ;
void dfs(int x)
{
vis[x] = ;
if(pq.empty() || pq.top() < maxx[x])
return;
for(int i=;i<V[x].size();i++)
{
int to = V[x][i].v;
if(vis[to])
continue;
if(pq.top() > V[x][i].w)
dfs(to);
}
while(pq.size() && pq.top() > maxx[x])
{
ans += x;
pq.pop();
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,q;
memset(vis,,sizeof vis);
memset(maxx,,sizeof maxx);
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
V[i].clear();
for(int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedeg(u,v,w);
addedeg(v,u,w);
}
cal(,-);
for(int i=;i<=n;i++)
sort(V[i].begin(),V[i].end());
while(q--)
{
int x;
scanf("%d",&x);
pq.push(x);
}
ans = ;
dfs();
printf("%lld\n",ans);
}
}

Robots Gym - 101915G的更多相关文章

  1. Gym 101915G Robots

    G. Robots time limit per test 5.0 s memory limit per test 256 MB input standard input output standar ...

  2. Gym 101915

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

  3. 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 ...

  4. 【Gym 100971J】Robots at Warehouse

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

  5. 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 ...

  6. Gym 100971J-Robots at Warehouse

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

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

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

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

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

  9. Open AI Gym简介

    介绍 OpenAI Gym是一款用于研发和比较强化学习算法的工具包,它支持训练智能体(agent)做任何事——从行走到玩Pong或围棋之类的游戏都在范围中. OpenAI Gym 是一个用于开发和比较 ...

随机推荐

  1. Redis入门--(二)Redis的安装

    1.建议安装在Linux服务器上来运行测试的

  2. java控制某个字段当天递增

    ①思路 1.获取当前时间年月日 如:2018-01-15 00:00:00 2018-01-15 24:00:00 2.查询表中对应日期字段是否在当天时间段内 3.若是在,则从0开始递增 4.若是不在 ...

  3. CSS知识点梳理

  4. 基于Python的开源人脸识别库:离线识别率高达99.38%

    项目地址:https://github.com/ageitgey/face_recognition#face-recognition 本文的模型使用了C++工具箱dlib基于深度学习的最新人脸识别方法 ...

  5. ReactNative-JS 调用原生方法实例代码(转载)

    第一步首先创建ReactNative 模块类继承ReactContextBaseJavaModule package com.mixture;   import android.content.Con ...

  6. android studio gradle统一管理版本

    创建config.gradle ext { android = [ compileSdkVersion : 26, buildToolsVersion : "26.0.2", mi ...

  7. php的yii框架开发总结4

    用户验证的实现:/protected/components/UserIdentity.php 修改:function authenticate()函数中的语句 public function auth ...

  8. System Center Configuration Manager 2016 配置安装篇(Part1)

    SCCM 2016 配置管理系列(Part 1- 4) 介绍AD01上配置了Active Directory域服务(ADDS),然后将Configuration Manager服务器(CM16)加入到 ...

  9. ASP.NET与json对象互转

    这两天写这个xml跟json的读写,心累啊,也不是很理解,请大家多指教 首先来个热身菜做一个简单的解析json 在script里写一个简单的弹窗效果 <script> //script里简 ...

  10. windows服务器间文件同步搭建步骤搜集

    Rsync https://www.cnblogs.com/janas/p/3321087.html https://yq.aliyun.com/ziliao/110867 subersion协议 h ...