CF1223E.Paint the Tree

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a weighted tree consisting of nn vertices. Recall that a tree is a connected graph without cycles. Vertices uiu_i and viv_i are connected by an edge with weight wiw_i.

Let's define the kk-coloring of the tree as an assignment of exactly kk colors to each vertex, so that each color is used no more than two times. You can assume that you have infinitely many colors available. We say that an edge is saturated in the given kk-coloring if its endpoints share at least one color (i.e. there exists a color that is assigned to both endpoints).

Let's also define the value of a kk-coloring as the sum of weights of saturated edges.

Please calculate the maximum possible value of a kk-coloring of the given tree.

You have to answer qq independent queries.

你将得到一棵包含 nn 个顶点的带权树。回忆一下,树是一种无环的连通图。顶点 uiu_i 和 viv_i 之间由一条权重为 wiw_i 的边相连。

我们定义树的一个 kk-着色 为:为每个顶点恰好分配 kk 种颜色,且每种颜色至多被使用两次(即至多出现在两个顶点上)。你可以假设有无穷多种颜色可供使用。若某条边的两个端点至少共享一种颜色(即存在一种颜色同时被分配给了这两个端点),则称该边在给定的 kk-着色下是饱和的。

此外,定义一个 kk-着色的值为所有饱和边的权重之和。

请计算给定树的所有 kk-着色中可能达到的最大值。

你需要回答 qq 个相互独立的询问。

输入格式

The first line contains one integer qq (1≤q≤5⋅1051 \le q \le 5 \cdot 10^5) – the number of queries.

The first line of each query contains two integers nn and kk (1≤n,k≤5⋅1051 \le n, k \le 5 \cdot 10^5) — the number of vertices in the tree and the number of colors to assign to each vertex, respectively.

Each of the next n−1n - 1 lines describes an edge of the tree. Edge ii is denoted by three integers uiu_i, viv_i and wiw_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i, 1≤wi≤1051 \le w_i \le 10^5) — the labels of vertices it connects and the weight of the edge. It is guaranteed that the given edges form a tree.

It is guaranteed that sum of all nn over all queries does not exceed 5⋅1055 \cdot 10^5.

第一行包含一个整数 qq(1≤q≤5⋅1051 \le q \le 5 \cdot 10^5),表示查询次数。

每次查询的第一行包含两个整数 nn 和 kk(1≤n,k≤5⋅1051 \le n, k \le 5 \cdot 10^5),分别表示树的顶点数以及需为每个顶点分配的颜色数。

接下来的 n−1n - 1 行每行描述树的一条边。第 ii 条边由三个整数 uiu_i、viv_i 和 wiw_i(1≤ui,vi≤n1 \le u_i, v_i \le n,ui≠viu_i \ne v_i,1≤wi≤1051 \le w_i \le 10^5)表示,分别代表该边所连接的两个顶点的编号以及该边的权重。保证所给边构成一棵树。

保证所有查询中 nn 的总和不超过 5⋅1055 \cdot 10^5。

输出格式

For each query print one integer — the maximum value of a kk-coloring of the given tree.

对于每个查询,输出一个整数——给定树的 kk-着色的最大值。

输入输出样例

  • 输入#1

    2
    4 1
    1 2 5
    3 1 2
    3 4 3
    7 2
    1 2 5
    1 3 4
    1 4 2
    2 5 1
    2 6 2
    4 7 3

    输出#1

    8
    14

说明/提示

The tree corresponding to the first query in the example:

One of the possible kk-colorings in the first example: (1),(1),(2),(2)(1), (1), (2), (2), then the 11-st and the 33-rd edges are saturated and the sum of their weights is 88.

The tree corresponding to the second query in the example:

One of the possible kk-colorings in the second example: (1,2),(1,3),(2,4),(5,6),(7,8),(3,4),(5,6)(1, 2), (1, 3), (2, 4), (5, 6), (7, 8), (3, 4), (5, 6), then the 11-st, 22-nd, 55-th and 66-th edges are saturated and the sum of their weights is 1414.

示例中第一个查询对应的树:

第一个示例中一种可能的 kk-着色方案:(1),(1),(2),(2)(1), (1), (2), (2),此时第 11 条和第 33 条边被饱和,其权重之和为 88。

示例中第二个查询对应的树:

第二个示例中一种可能的 kk-着色方案:(1,2),(1,3),(2,4),(5,6),(7,8),(3,4),(5,6)(1, 2), (1, 3), (2, 4), (5, 6), (7, 8), (3, 4), (5, 6),此时第 11、22、55 和 66 条边被饱和,其权重之和为 1414。

输入解题思路,AI测评打分。不知道怎么写?

首页