E. Triangle Tree

E. Triangle Tree

You are given a rooted tree$^{\text{∗}}$ containing $n$ vertices rooted at vertex $1$. A pair of vertices $(u,v)$ is called a good pair if $u$ is not an ancestor$^{\text{†}}$ of $v$ and $v$ is not an ancestor of $u$. For any two vertices, $\text{dist}(u,v)$ is defined as the number of edges on the unique simple path from $u$ to $v$, and $\text{lca}(u,v)$ is defined as their lowest common ancestor.

A function $f(u,v)$ is defined as follows.

  • If $(u,v)$ is a good pair, $f(u,v)$ is the number of distinct integer values $x$ such that there exists a non-degenerate triangle$^{\text{‡}}$ formed by side lengths $\text{dist}(u,\text{lca}(u,v))$, $\text{dist}(v,\text{lca}(u,v))$, and $x$.
  • Otherwise, $f(u,v)$ is $0$.

You need to find the following value: $$\sum_{i = 1}^{n-1} \sum_{j = i+1}^n f(i,j).$$

$^{\text{∗}}$A tree is a connected graph without cycles. A rooted tree is a tree where one vertex is special and called the root.

$^{\text{†}}$An ancestor of vertex $v$ is any vertex on the simple path from $v$ to the root, including the root, but not including $v$. The root has no ancestors.

$^{\text{‡}}$A triangle with side lengths $a$, $b$, $c$ is non-degenerate when $a+b > c$, $a+c > b$, $b+c > a$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 3 \cdot 10^5$).

Each of the next $n-1$ lines contains two integers $u_i$ and $v_i$, denoting the two vertices connected by an edge ($1 \le u_i,v_i \le n$, $u_i \neq v_i$).

It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.

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 $(i,j)$ satisfying $i<j$ is $(2,3)$. Here, $\text{lca}(2,3)$ is $1$, and the two distances are $1$ and $1$.

There is only one value of $x$ for two side lengths $1$ and $1$, which is $1$. Therefore, the answer for the first test case is $1$.

On the second test case, there is no good pair. Therefore, the answer for the second test case is $0$.

On the third test case, the good pairs $(i,j)$ satisfying $i<j$ are as follows.

  • $(2,5)$: $\text{lca}(2,5)$ is $1$, distances are $1$ and $1$. There is only one possible value of $x$, which is $1$.
  • $(3,4)$: $\text{lca}(3,4)$ is $2$, distances are $1$ and $1$. There is only one possible value of $x$, which is $1$.
  • $(3,5)$: $\text{lca}(3,5)$ is $1$, distances are $2$ and $1$. There is only one possible value of $x$, which is $2$.
  • $(4,5)$: $\text{lca}(4,5)$ is $1$, distances are $2$ and $1$. There is only one possible value of $x$, which is $2$.

Therefore, the answer for the third test case is $1+1+1+1=4$.

 

解题思路

  定义 $d_u$ 表示节点 $u$ 到根节点的距离。如果 $(u,v)$ 合法,记 $\mathrm{lca}(u,v)$ 为 $p$,$a = d_u - d_p$ 和 $b = d_v - d_p$ 为三角形的两条边(不失一般性假设 $a \leq b$),那么另一条边需要满足 $\displaylines{\begin{cases} a+b \geq x+1 \\ a+x \geq b+1 \end{cases}}$,解得 $b-a+1 \leq x \leq a+b-1$,因此 $x$ 的取值有 $2a-1$ 种。所以有

\begin{align*}
f(u,v) &= 2 \cdot \min\{d_u-d_p, d_v-d_p\} - 1 \\
&= 2 \cdot \min\{d_u, d_v\} - (2d_p + 1)
\end{align*}

  可以分别处理 $\sum\limits_{u}{\sum\limits_{v}{2 \cdot \min\{d_u, d_v\}}}$ 与 $\sum\limits_{u}{\sum\limits_{v}{2d_p + 1}}$ 这两部分。在求解前先进行一次 dfs,求出 $d_u$,子树 $u$ 的大小 $\mathrm{sz}_u$,深度为 $i$ 的节点数量 $c_i$。定义后缀和 $s_i = \sum\limits_{j=i}^{n}{c_j}$。

  对于 $\sum\limits_{u}{\sum\limits_{v}{2 \cdot \min\{d_u, d_v\}}}$,枚举每个节点 $u$,然后求出满足 $d_u \leq d_v$ 的节点 $v$ 的数量 $s_{d_u}$。需要注意的是这些节点包括子树 $u$ 的所有节点(子树的每个节点深度必然比 $u$ 大),因此需要减去 $\mathrm{sz}_u$,否则会统计不合法的 $(u,v)$。因此有 $2d_u(s_{d_u} - \mathrm{sz}_u)$。还有一个小问题,当 $d_u = d_v$ 时 $f(u,v)$ 会被统计两次,因此需要进行容斥,对于每个深度 $i$ 只需减去 $2 i \cdot C_{c_i}^2$ 即可。所以 $\sum\limits_{u}{\sum\limits_{v}{2 \cdot \min\{d_u, d_v\}}} = \sum\limits_{u}{2 d_u(s_{d_u} - \mathrm{sz}_u)} - \sum_{i=1}^{n}{2 i \cdot C_{c_i}^{2}}$。

  对于 $\sum\limits_{u}{\sum\limits_{v}{2d_p + 1}}$,只需枚举每个节点 $u$ 作为 lca,求出 lca 为 $u$ 的合法数对数量,乘以 $2d_p+1$ 即可。假设 $u$ 的子节点为 $v_1, v_2, \ldots, v_m$,那么 $\sum\limits_{i=1}^{m}{\mathrm{sz}_{v_i} \cdot \sum\limits_{j=1}^{i-1}{\mathrm{sz}_{v_j}}}$ 即为所求数量,记为 $t_u$。因此 $\sum\limits_{u}{\sum\limits_{v}{2d_p + 1}} = \sum\limits_{u}{(2d_u+1)t_u}$。

  AC 代码如下,时间复杂度为 $O(n)$:

#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

posted @ 2025-01-28 18:42  onlyblues  阅读(57)  评论(0)    收藏  举报
Web Analytics