欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1213G.Path Queries
Path Queries
CF1213G · Path Queries
- 难度:1800
- 标签:divide and conquer、dsu、graphs、sortings、trees
- 链接:https://codeforces.com/problemset/problem/1213/G
- 时间限制:3 seconds 内存限制:256 megabytes
- 出现位置:Day12-Kruskal-离线排序-路径贡献
英文原题面
Statement
You are given a weighted tree consisting of vertices. Recall that a tree is a connected graph without cycles. Vertices and are connected by an edge with weight . You are given queries. The -th query is given as an integer . In this query you need to calculate the number of pairs of vertices () such that the maximum weight of an edge on a simple path between and doesn't exceed .
Input
The first line of the input contains two integers and () — the number of vertices in the tree and the number of queries. Each of the next lines describes an edge of the tree. Edge is denoted by three integers , and — the labels of vertices it connects (, ) and the weight of the edge (). It is guaranteed that the given edges form a tree. The last line of the input contains integers (), where is the maximum weight of an edge in the -th query.
Output
Print integers — the answers to the queries. The -th value should be equal to the number of pairs of vertices () such that the maximum weight of an edge on a simple path between and doesn't exceed . Queries are numbered from to in the order of the input.
样例
样例 1
输入:
7 5
1 2 1
3 2 3
2 4 1
4 5 2
5 7 4
3 6 2
5 2 3 4 1
输出:
21 7 15 21 3
样例 2
输入:
1 2
1 2
输出:
0 0
样例 3
输入:
3 3
1 2 1
2 3 2
1 3 2
输出:
1 3 3
样例解释(英文原文)
The picture shows the tree from the first example:
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |