CF375E.Red and Black Tree
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You have a weighted tree, consisting of n vertices. Each vertex is either painted black or is painted red. A red and black tree is called beautiful, if for any its vertex we can find a black vertex at distance at most x.
The distance between two nodes is the shortest path between them.
You have a red and black tree. Your task is to make it beautiful in the minimum number of color swap operations. In one color swap operation, you can choose two vertices of different colors and paint each of them the other color. In other words, if you choose a red vertex p and a black vertex q, then in one operation you are allowed to paint p black and paint q red.
Print the minimum number of required actions.
你有一棵带权树,包含 n 个顶点。每个顶点被涂成黑色或红色。若一棵红黑树满足:对其中任意一个顶点,均存在一个距离不超过 x 的黑色顶点,则称其为优美的。
两点之间的距离定义为它们之间的最短路径长度。
你拥有一棵红黑树。你的任务是通过最少次数的颜色交换操作,使其变为优美的。一次颜色交换操作指:选择两个颜色不同的顶点,并将它们的颜色互换。换言之,若你选择一个红色顶点 p 和一个黑色顶点 q,则在一次操作中,可将 p 涂为黑色、同时将 q 涂为红色。
请输出所需的最少操作次数。
输入格式
The first line contains two integers n and x (2 ≤ n ≤ 500; 1 ≤ x ≤ 109). The next line contains n integers, each of them is either a zero or one. If the i-th number equals 1, then vertex i of the tree is black, otherwise vertex i is red. Next n - 1 lines contain the tree edges. The j-th line contains integers u__j v__j w__j (1 ≤ u__j, v__j ≤ n; u__j ≠ v__j; 1 ≤ w__j ≤ 109) which means that the tree has an edge of weight w__j between vertices v__j and u__j.
Assume that the tree vertices are numbered from 1 to n.
第一行包含两个整数 n 和 x(2≤n≤500;1≤x≤109)。下一行包含 n 个整数,每个数为 0 或 1。若第 i 个数等于 1,则树的第 i 个顶点为黑色,否则为红色。接下来的 n−1 行描述树的边。第 j 行包含三个整数 uj、vj、wj(1≤uj,vj≤n;uj=vj;1≤wj≤109),表示树在顶点 uj 与 vj 之间存在一条权值为 wj 的边。
假设树的顶点编号从 1 到 n。
输出格式
Print a single integer — the minimum number of required swap operations.
If it is impossible to get a beautiful tree at any number of operations, print -1.
输出一个整数——所需的最少交换操作次数。
如果无论进行多少次操作都无法得到一棵优美的树,则输出 -1。
输入输出样例
输入#1
3 2 1 0 0 1 2 2 2 3 2
输出#1
1
输入#2
4 2 0 1 0 0 1 2 2 2 3 2 3 4 2
输出#2
-1
输入解题思路,AI测评打分。不知道怎么写?