E. Triangle Tree
E. Triangle Tree
You are given a rooted tree containing vertices rooted at vertex . A pair of vertices is called a good pair if is not an ancestor of and is not an ancestor of . For any two vertices, is defined as the number of edges on the unique simple path from to , and is defined as their lowest common ancestor.
A function is defined as follows.
- If is a good pair, is the number of distinct integer values such that there exists a non-degenerate triangle formed by side lengths , , and .
- Otherwise, is .
You need to find the following value:
A tree is a connected graph without cycles. A rooted tree is a tree where one vertex is special and called the root.
An ancestor of vertex is any vertex on the simple path from to the root, including the root, but not including . The root has no ancestors.
A triangle with side lengths , , is non-degenerate when , , .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains a single integer ().
Each of the next lines contains two integers and , denoting the two vertices connected by an edge (, ).
It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output the answer on a separate line.
Example
Input
4
3
1 2
1 3
3
1 2
3 2
5
2 3
1 5
4 2
1 2
11
2 1
2 3
2 4
4 5
6 5
5 7
4 8
8 9
7 10
10 11
Output
1
0
4
29
Note
On the first test case, the only good pair satisfying is . Here, is , and the two distances are and .
There is only one value of for two side lengths and , which is . Therefore, the answer for the first test case is .
On the second test case, there is no good pair. Therefore, the answer for the second test case is .
On the third test case, the good pairs satisfying are as follows.
- : is , distances are and . There is only one possible value of , which is .
- : is , distances are and . There is only one possible value of , which is .
- : is , distances are and . There is only one possible value of , which is .
- : is , distances are and . There is only one possible value of , which is .
Therefore, the answer for the third test case is .
解题思路
定义 表示节点 到根节点的距离。如果 合法,记 为 , 和 为三角形的两条边(不失一般性假设 ),那么另一条边需要满足 ,解得 ,因此 的取值有 种。所以有
可以分别处理 与 这两部分。在求解前先进行一次 dfs,求出 ,子树 的大小 ,深度为 的节点数量 。定义后缀和 。
对于 ,枚举每个节点 ,然后求出满足 的节点 的数量 。需要注意的是这些节点包括子树 的所有节点(子树的每个节点深度必然比 大),因此需要减去 ,否则会统计不合法的 。因此有 。还有一个小问题,当 时 会被统计两次,因此需要进行容斥,对于每个深度 只需减去 即可。所以 。
对于 ,只需枚举每个节点 作为 lca,求出 lca 为 的合法数对数量,乘以 即可。假设 的子节点为 ,那么 即为所求数量,记为 。因此 。
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 3e5 + 5, M = N * 2;
int h[N], e[M], ne[M], idx;
int d[N], sz[N], s[N];
LL ans;
void add(int u, int v) {
e[idx] = v, ne[idx] = h[u], h[u] = idx++;
}
void dfs(int u, int p) {
d[u] = d[p] + 1;
s[d[u]]++;
for (int i = h[u]; i != -1; i = ne[i]) {
int v = e[i];
if (v == p) continue;
dfs(v, u);
ans -= (2ll * d[u] + 1) * sz[u] * sz[v];
sz[u] += sz[v];
}
sz[u]++;
}
void solve() {
int n;
cin >> n;
idx = 0;
memset(h, -1, n + 1 << 2);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
add(u, v), add(v, u);
}
ans = 0;
memset(sz, 0, n + 1 << 2);
memset(s, 0, n + 2 << 2);
dfs(1, 0);
for (int i = n; i; i--) {
ans -= s[i] * (s[i] - 1ll) * i;
s[i] += s[i + 1];
}
for (int i = 1; i <= n; i++) {
ans += 2ll * d[i] * (s[d[i]] - sz[i]);
}
cout << ans << '\n';
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
参考资料
Codeforces Round 1000 (Div. 2) — Editorial:https://codeforces.com/blog/entry/138593
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/18693313
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2023-01-28 斐波那契前 n 项和
2023-01-28 最幸运的数字
2022-01-28 圆形牛棚