传送门

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);
}
}

最新文章

  1. python redis使用心得
  2. input lable水平对齐
  3. transform原点
  4. httpclient调用https
  5. OpenGL学习进程(11)第八课:颜色绘制的详解
  6. WRONGTYPE Operation against a key holding the wrong kind of value
  7. js正则验证手机号
  8. Interaction with the camera or the photo library
  9. javascript常用的内置对象实用操作
  10. HDU 1361 Parencodings(栈)
  11. css初始化值
  12. [iOS]Objective-C 第一节课
  13. Python 基础三 文件 函数
  14. React学习(一)父子组件通讯
  15. Github终于连上了hexo
  16. echarts中饼图显示百分比
  17. 【译】Optaplanner开发手册本地化: (0) - 前言及概念
  18. Mac OS X 恢复 VMware Fusion 虚拟机中的 vmdk 文件
  19. strongSwan配置、运行及测试
  20. 转:CMake 使用方法

热门文章

  1. hibernate课程 初探一对多映射2-4 Mysql创建数据库表
  2. 安全漏洞 : XSS CSRF
  3. 转:Windows任务计划实现自动执行ArcGIS相关功能
  4. java IO流——字符流
  5. hdu-2844&amp;&amp;POJ-1742 Coins---多重背包
  6. unixbench安装及使用
  7. vuejs给组件绑定原生事件
  8. react里面 react-router4 跳转
  9. mysql 全连接 报错1051的原因
  10. ajax(form)图片上传(spring)