CF1249F.Maximum Weight Subset
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a tree, which consists of n vertices. Recall that a tree is a connected undirected graph without cycles.
Example of a tree.
Vertices are numbered from 1 to n. All vertices have weights, the weight of the vertex v is av.
Recall that the distance between two vertices in the tree is the number of edges on a simple path between them.
Your task is to find the subset of vertices with the maximum total weight (the weight of the subset is the sum of weights of all vertices in it) such that there is no pair of vertices with the distance k or less between them in this subset.
给你一棵包含 n 个顶点的树。注意:树是一种无环的连通无向图。
树的一个示例。
顶点编号为 1 到 n。每个顶点均具有一个权值,顶点 v 的权值为 av。
注意:树中两个顶点之间的距离定义为它们之间简单路径上的边数。
你的任务是找出一个顶点子集,使得该子集的总权值(即子集中所有顶点权值之和)最大,且子集中任意两个顶点之间的距离均严格大于 k。
输入格式
The first line of the input contains two integers n and k (1≤n,k≤200) — the number of vertices in the tree and the distance restriction, respectively.
The second line of the input contains n integers a1,a2,…,an (1≤ai≤105), where ai is the weight of the vertex i.
The next n−1 lines contain edges of the tree. Edge i is denoted by two integers ui and vi — the labels of vertices it connects (1≤ui,vi≤n, ui=vi).
It is guaranteed that the given edges form a tree.
输入的第一行包含两个整数 n 和 k(1≤n,k≤200),分别表示树中顶点的数量和距离限制。
输入的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤105),其中 ai 表示顶点 i 的权值。
接下来的 n−1 行描述树的边。第 i 条边由两个整数 ui 和 vi 表示——即该边所连接的两个顶点的编号(1≤ui,vi≤n,且 ui=vi)。
保证所给的边构成一棵树。
输出格式
Print one integer — the maximum total weight of the subset in which all pairs of vertices have distance more than k.
输出一个整数——满足其中任意两个顶点间距离均大于 k 的子集的最大总权重。
输入输出样例
输入#1
5 1 1 2 3 4 5 1 2 2 3 3 4 3 5
输出#1
11
输入#2
7 2 2 1 2 1 2 1 1 6 4 1 5 3 1 2 3 7 5 7 4
输出#2
4
输入解题思路,AI测评打分。不知道怎么写?