Under construction

Still being built. Things may change

Triangle Tree

Time limit2sMemory limit2048 MB

Summary
Given a rooted tree, sum the number of integer side lengths x that form a non-degenerate triangle over all pairs of vertices where neither is an ancestor of the other.
Level

Medium6 of 10

Topics
Tree, DFS, Sorting, Prefix sum
Solved
No attempts yet

Statement

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^1 with 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^2 of vv and vv is not an ancestor of uu. For any two vertices, dist(u,v)\text{dist}(u,v) is the number of edges on the unique simple path from uu to vv, and lca(u,v)\text{lca}(u,v) is their lowest common ancestor.

The 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 integers xx for which a non-degenerate triangle3^3 exists with 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)=0f(u,v)=0.

Compute ∑i=1n−1∑j=i+1nf(i,j)\sum_{i=1}^{n-1}\sum_{j=i+1}^{n} f(i,j).

1 A tree is a connected graph without cycles. A rooted tree is a tree in which one vertex is designated as the root.

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

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

Input

The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). Each test case is given as 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 uiu_i and viv_i (1≤ui,vi≤n1 \le u_i,v_i \le n, ui≠viu_i \neq v_i), the two vertices joined by an edge. The given edges form a tree.

The sum of nn over all test cases does not exceed 3⋅1053 \cdot 10^5.

Output

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

Hints

In the first test case, the only good pair with i<ji<j is (2,3)(2,3). Here lca(2,3)\text{lca}(2,3) is 11, and both distances are 11. For side lengths 11 and 11, the only valid xx is 11, so the answer for the first test case is 11.

The second test case has no good pair, so the answer is 00.

In the third test case, the good pairs with i<ji<j are the following.

  • (2,5)(2,5): lca(2,5)\text{lca}(2,5) is 11, and the distances are 11 and 11. The only possible xx is 11.
  • (3,4)(3,4): lca(3,4)\text{lca}(3,4) is 22, and the distances are 11 and 11. The only possible xx is 11.
  • (3,5)(3,5): lca(3,5)\text{lca}(3,5) is 11, and the distances are 22 and 11. The only possible xx is 22.
  • (4,5)(4,5): lca(4,5)\text{lca}(4,5) is 11, and the distances are 22 and 11. The only possible xx is 22.

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

Examples1

  1. Example 1

    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
    
    Expected output
    1
    0
    4
    29