传送门

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. 1
    5 7
    1 2 3
    1 3 4
    3 4 9
    3 5 7
    1 3 4 9 8 7 10
Output
  1. 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将所有的点到原点的最大权值记录下来,之后将机器人按从大到小排序,开始边跑边删

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<sstream>
  6. #include<cmath>
  7. #include<stack>
  8. #include<map>
  9. #include<cstdlib>
  10. #include<vector>
  11. #include<string>
  12. #include<queue>
  13. using namespace std;
  14.  
  15. #define ll long long
  16. #define llu unsigned long long
  17. #define INF 0x3f3f3f3f
  18. const double PI = acos(-1.0);
  19. const int maxn = 1e5+;
  20. const int mod = 1e9+;
  21.  
  22. struct edge
  23. {
  24. int v,w;
  25. friend bool operator < (edge a,edge b)
  26. {
  27. return a.w > b.w;
  28. }
  29. };
  30. vector<edge>V[maxn];
  31. void addedeg(int u,int v,int w)
  32. {
  33. V[u].push_back(edge{v,w});
  34. }
  35. int maxx[maxn];
  36. int vis[maxn];
  37. void cal(int x,int fa)
  38. {
  39. if(V[x].size() == || x == -)
  40. return;
  41. for(int i=;i<V[x].size();i++)
  42. {
  43. int to = V[x][i].v;
  44. if(to == fa)
  45. continue;
  46. maxx[to] = max(maxx[x],V[x][i].w);
  47. cal(to,x);
  48. }
  49. }
  50. priority_queue<int,vector<int>,less<int>> pq;
  51. ll ans = ;
  52. void dfs(int x)
  53. {
  54. vis[x] = ;
  55. if(pq.empty() || pq.top() < maxx[x])
  56. return;
  57. for(int i=;i<V[x].size();i++)
  58. {
  59. int to = V[x][i].v;
  60. if(vis[to])
  61. continue;
  62. if(pq.top() > V[x][i].w)
  63. dfs(to);
  64. }
  65. while(pq.size() && pq.top() > maxx[x])
  66. {
  67. ans += x;
  68. pq.pop();
  69. }
  70. }
  71. int main()
  72. {
  73. int t;
  74. scanf("%d",&t);
  75. while(t--)
  76. {
  77. int n,q;
  78. memset(vis,,sizeof vis);
  79. memset(maxx,,sizeof maxx);
  80. scanf("%d%d",&n,&q);
  81. for(int i=;i<=n;i++)
  82. V[i].clear();
  83. for(int i=;i<n;i++)
  84. {
  85. int u,v,w;
  86. scanf("%d%d%d",&u,&v,&w);
  87. addedeg(u,v,w);
  88. addedeg(v,u,w);
  89. }
  90. cal(,-);
  91. for(int i=;i<=n;i++)
  92. sort(V[i].begin(),V[i].end());
  93. while(q--)
  94. {
  95. int x;
  96. scanf("%d",&x);
  97. pq.push(x);
  98. }
  99. ans = ;
  100. dfs();
  101. printf("%lld\n",ans);
  102. }
  103. }

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. SSM环境下配置log4j输出sql和异常到控制台和本地日志文件中

    1.引入日志依赖包 <!--解决Spring使用slf4j输出日志与log4j冲突的问题--> <dependency> <groupId>org.slf4j< ...

  2. 阿里前端笔试总结--H5面试题

    转载网址 https://blog.csdn.net/qq_20913021/article/details/51351801 1.有一个长度未知的数组a,如果它的长度为0就把数字1添加到数组里面,否 ...

  3. form中action属性后面?传递参数 获取不到

    $p_id = $_REQUEST['p_id']; echo "<h1>您将更新商品编号为<span>$p_id</span>的商品信息 <a h ...

  4. FRM-40654 Record has been updated

    该错误是由于界面上对数据进行了修改后(比如为A),将修改前的数据(B)与数据库中的数据(B)进行配不成功时报错: 首先确认Form界面是否有其他人对数据进行修改: 如果无人修改: 将数据库的数据查询出 ...

  5. 使用C语言来实现模块化

    除了C语言以及C++编程语言之外,在其它现在非常流行的开发语言中,比如说:java,php,jsp等等.我们很难想象到缺少标准化的模块管理机制是一件多么可怕的事情.但是这往往也是由C语言本身的设计哲学 ...

  6. 笨办法学Python(十八)

    习题 18: 命名.变量.代码.函数 标题包含的内容够多的吧?接下来我要教你“函数(function)”了!咚咚锵!说到函数,不一样的人会对它有不一样的理解和使用方法,不过我只会教你现在能用到的最简单 ...

  7. MHA故障切换方案

    mha故障切换当master 挂掉后会自动切换到slave01上去,可以看 tailf /var/mha_log/manager.log4个问题:1,切换后且原master01mysql服务启动OK, ...

  8. first 和firstordefault的用法 简介

    https://www.cnblogs.com/1312mn/p/9212325.html

  9. R 语言爬虫 之 cnblog博文爬取

    Cnbolg Crawl a). 加载用到的R包 ##library packages needed in this case library(proto) library(gsubfn) ## Wa ...

  10. 高亮代码 SyntaxHighlighter

    SyntaxHighlighter: http://alexgorbatchev.com/SyntaxHighlighter/download/ demo <!DOCTYPE html PUBL ...