Triangle Tree

시간 제한2초메모리 제한2048 MB

요약
서로 조상 관계가 아닌 모든 정점 쌍에 대해, LCA 아래 두 거리와 삼각형을 이루는 정수 x의 개수를 모두 더한다.
난이도

보통10점 중 7점

유형
트리, DFS, 조합론, 수학
정답자
아직 제출이 없습니다

문제

One day, a giant tree grew in the countryside. Little John, with his childhood eagle, decided to make it his home. Little John will build a structure on the tree with galvanized square steel. However, little did he know, he could not build what is physically impossible.

You are given a rooted tree1 containing nn vertices rooted at vertex 11. A pair of vertices (u,v)(u,v) is called a good pair if uu is not an ancestor2 of vv and vv is not an ancestor of uu. For any two vertices, dist(u,v)\text{dist}(u,v) is defined as the number of edges on the unique simple path from uu to vv, and lca(u,v)\text{lca}(u,v) is defined as their lowest common ancestor.

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

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

You need to find the following value:

 ∑_i=1n−1∑_j=i+1nf(i,j).\sum\_{i = 1}^{n-1} \sum\_{j = i+1}^n f(i,j). 


1A tree is a connected graph without cycles. A rooted tree is a tree where one vertex is special and called the root.

2An ancestor of vertex vv is any vertex on the simple path from vv to the root, including the root, but not including vv. The root has no ancestors.

3A triangle with side lengths aa, bb, cc is non-degenerate when a+b>ca+b > c, a+c>ba+c > b, b+c>ab+c > a.

입력

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

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

Each of the next n−1n-1 lines contains two integers u_iu\_i and v_iv\_i, denoting the two vertices connected by an edge (1≤u_i,v_i≤n1 \le u\_i,v\_i \le n, u_i≠v_iu\_i \neq v\_i).

It is guaranteed that the given edges form a tree.

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

출력

For each test case, output the answer on a separate line.

힌트

On the first test case, the only good pair (i,j)(i,j) satisfying i\<ji\<j is (2,3)(2,3). Here, lca(2,3)\text{lca}(2,3) is 11, and the two distances are 11 and 11.

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

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

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

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

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

예제1

  1. 예제 1

    입력
    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
    
    예상 출력
    1
    0
    4
    29