CF2167F.Tree, TREE!!!
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Roots change, but the tree stands strong — so should your logic.
Behruzbek received a tree∗ with n nodes. For a chosen root† r, Behruzbek wants to find cuteness of the tree.
Consider every set of k distinct nodes of the tree. For each such set, compute its lowest common ancestor (LCA) in the tree when it is rooted at r. Let Sr be the set of all distinct nodes obtained this way; then cuteness of the tree is ∣Sr∣, where ∣S∣ means the number of distinct elements.
After discovering the cuteness of trees, Behruzbek became interested in finding the kawaiiness of the tree! Kawaiiness is defined as:
sum_r=1n∣S_r∣=∣S_1∣+∣S_2∣+dots+∣S_n∣
Unfortunately, Behruzbek is feeling sleepy now. Please help Behruzbek by finding the kawaiiness of the tree!
∗A tree is a connected graph without cycles.
†A rooted tree is a tree where one vertex is special and called the root.
根会变化,但树依然挺立——你的逻辑亦应如此。
贝赫鲁泽克得到了一棵含 n 个节点的树∗。对于选定的根† r,贝赫鲁泽克希望计算该树的“可爱度”(cuteness)。
考虑树中所有大小为 k 的互异节点集合。对每个这样的集合,计算其在以 r 为根的树中的最低公共祖先(LCA)。令 Sr 表示通过此方式得到的所有互异节点构成的集合;则该树的可爱度定义为 ∣Sr∣,其中 ∣S∣ 表示集合 S 中互异元素的个数。
在发现树的可爱度后,贝赫鲁泽克开始对树的“卡哇伊度”(kawaiiness)产生了兴趣!卡哇伊度定义为:
r=1∑n∣Sr∣=∣S1∣+∣S2∣+⋯+∣Sn∣
不幸的是,贝赫鲁泽克现在感到困倦了。请帮助他计算这棵树的卡哇伊度!
∗ 树是一个无环的连通图。
† 有根树是一棵指定某一顶点为特殊顶点(称为根)的树。
输入格式
The first line contains the number of test cases t (1≤t≤104).
The first line of each test case contains two integers n and k (2≤k≤n≤2⋅105) — the number of vertices in the tree and the number of distinct integers to be chosen.
The following n−1 lines of each test case describe the tree. Each of the lines contains two integers u and v (1≤u,v≤n, u=v) that indicate an edge between vertex u and v. It is guaranteed that these edges form a tree.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
第一行包含测试用例的数量 t(1≤t≤104)。
每个测试用例的第一行包含两个整数 n 和 k(2≤k≤n≤2⋅105),分别表示树中顶点的数量以及需选择的互不相同整数的个数。
每个测试用例接下来的 n−1 行描述该树。每行包含两个整数 u 和 v(1≤u,v≤n,u=v),表示顶点 u 与 v 之间存在一条边。保证这些边构成一棵树。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, output one integer — the value of r=1∑n∣Sr∣.
对于每个测试用例,输出一个整数——即 r=1∑n∣Sr∣ 的值。
输入输出样例
输入#1
4 2 2 1 2 5 3 1 2 1 3 1 4 1 5 6 3 1 2 1 3 2 4 2 5 3 6 10 5 5 6 4 9 3 9 2 6 2 8 8 9 6 10 1 6 4 7
输出#1
2 9 17 35
说明/提示
Let f(i)=∣Si∣
For the third example:
-
Root is 1, only 1 and 2 nodes can be obtained. For example, we can choose: LCA(4,5,6)=1 and LCA(2,4,5)=2. As a result, f(1)=2.
-
Root is 2, only 1 and 2 nodes can be obtained. For example, we can choose: LCA(1,3,6)=1 and LCA(1,4,5)=2. As a result, f(2)=2.
-
Root is 3, f(3)=3. For example, node 3 can be obtained by choosing: LCA(2,4,6)=3.
-
Root is 4, f(4)=3. For example, node 2 can be obtained by choosing: LCA(1,3,5)=2.
-
Root is 5, f(5)=3. For example, node 2 can be obtained by choosing: LCA(3,4,6)=2.
-
Root is 6, f(6)=4. For example, node 3 can be obtained by choosing: LCA(3,4,5)=2.
Overall, 2+2+3+3+3+4=17.
令 f(i)=∣Si∣
对于第三个样例:
-
根节点为 1 时,仅能获得节点 1 和 2。例如,可选择:LCA(4,5,6)=1 和 LCA(2,4,5)=2。因此,f(1)=2。
-
根节点为 2 时,仅能获得节点 1 和 2。例如,可选择:LCA(1,3,6)=1 和 LCA(1,4,5)=2。因此,f(2)=2。
-
根节点为 3 时,f(3)=3。例如,节点 3 可通过选择:LCA(2,4,6)=3 得到。
-
根节点为 4 时,f(4)=3。例如,节点 2 可通过选择:LCA(1,3,5)=2 得到。
-
根节点为 5 时,f(5)=3。例如,节点 2 可通过选择:LCA(3,4,6)=2 得到。
-
根节点为 6 时,f(6)=4。例如,节点 3 可通过选择:LCA(3,4,5)=2 得到。
综上,2+2+3+3+3+4=17。
输入解题思路,AI测评打分。不知道怎么写?