CF1822F.Gardening Friends
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Two friends, Alisa and Yuki, planted a tree with n vertices in their garden. A tree is an undirected graph without cycles, loops, or multiple edges. Each edge in this tree has a length of k. Initially, vertex 1 is the root of the tree.
Alisa and Yuki are growing the tree not just for fun, they want to sell it. The cost of the tree is defined as the maximum distance from the root to a vertex among all vertices of the tree. The distance between two vertices u and v is the sum of the lengths of the edges on the path from u to v.
The girls took a course in gardening, so they know how to modify the tree. Alisa and Yuki can spend c coins to shift the root of the tree to one of the neighbors of the current root. This operation can be performed any number of times (possibly zero). Note that the structure of the tree is left unchanged; the only change is which vertex is the root.
The friends want to sell the tree with the maximum profit. The profit is defined as the difference between the cost of the tree and the total cost of operations. The profit is cost of the tree minus the total cost of operations.
Help the girls and find the maximum profit they can get by applying operations to the tree any number of times (possibly zero).
两位朋友,阿丽莎(Alisa)和由纪(Yuki),在自家花园里种下了一棵含有 n 个顶点的树。树是一种无环、无自环、无重边的无向图。该树的每条边长度均为 k。初始时,顶点 1 是这棵树的根。
阿丽莎和由纪种树并不仅仅为了娱乐,她们还打算出售这棵树。这棵树的“成本”定义为:树中所有顶点到根节点的最大距离。其中,两个顶点 u 和 v 之间的距离等于连接 u 与 v 的唯一路径上所有边的长度之和。
两位女孩修习过园艺课程,因此懂得如何改造这棵树。阿丽莎和由纪可以花费 c 枚金币,将当前根节点更改为它的一个邻接顶点(即执行一次“换根”操作)。该操作可执行任意多次(包括零次)。注意:树的结构保持不变,唯一改变的是哪一顶点被指定为根。
这对朋友希望以最大利润出售这棵树。利润定义为:树的成本减去所有操作的总花费。即:
利润=树的成本−操作总花费
请帮助她们求出:通过任意次数(包括零次)的换根操作,所能获得的最大利润。
输入格式
The first line of the input contains one integer t (1≤t≤104) — the number of test cases.
The description of the test cases follows.
The first line of each test case contains integers n, k, c (2≤n≤2⋅105; 1≤k,c≤109) — the number of vertices in the tree, the length of each edge, and the cost of the operation.
The next n−1 lines of the test case contain pairs of integers ui, vi (1≤ui,vi≤n) — the edges of the graph. These edges form a tree.
The sum of the values of n over all test cases does not exceed 2⋅105.
输入的第一行包含一个整数 t(1≤t≤104)—— 表示测试用例的数量。
接下来是各测试用例的描述。
每个测试用例的第一行包含三个整数 n、k、c(2≤n≤2⋅105;1≤k,c≤109)—— 分别表示树中顶点的数量、每条边的长度以及一次操作的代价。
每个测试用例的接下来 n−1 行,每行包含一对整数 ui、vi(1≤ui,vi≤n)—— 表示图中的边。这些边构成一棵树。
所有测试用例中 n 的总和不超过 2⋅105。
输出格式
For each test case, output a single integer — the maximum profit that Yuki and Alisa can get.
对于每个测试用例,输出一个整数——Yuki 和 Alisa 能获得的最大利润。
输入输出样例
输入#1
4 3 2 3 2 1 3 1 5 4 1 2 1 4 2 5 4 3 4 6 5 3 4 1 6 1 2 6 5 1 3 2 10 6 4 1 3 1 9 9 7 7 6 6 4 9 2 2 8 8 5 5 10
输出#1
2 12 17 32
输入解题思路,AI测评打分。不知道怎么写?