#P10842. 【MX-J2-T3】Piggy and Trees

【MX-J2-T3】Piggy and Trees

Background

Original problem link: https://oier.team/problems/J2D.

Problem Description

You are given a tree with nn nodes.

Define f(u,v,i)f(u, v, i) as the minimum value of dis(x,i)\text{dis}(x, i) among all nodes xx that satisfy $^\dagger\text{dis}(u, x) + \text{dis}(v, x) = \text{dis}(u, v)$.

Compute $\sum\limits_{u = 1}^n \sum\limits_{v = u + 1}^n \sum\limits_{i = 1}^n f(u, v, i)$ modulo 109+710^9 + 7.

†dis(u,v)^\dagger\text{dis}(u, v) is the length of the path between nodes uu and vv in the tree. In particular, dis(u,u)=0\text{dis}(u, u) = 0.

Input Format

The first line contains an integer nn, representing the number of nodes in the tree.

Each of the following n−1n - 1 lines contains two integers ui,viu_i, v_i, representing an edge in the tree.

Output Format

Output one line containing one integer, representing the answer.

4
1 2
1 3
1 4

9

6
1 2
2 3
3 4
4 5
5 6

70

10
1 2
1 3
1 4
2 5
3 6
2 7
4 8
8 9
9 10

536

Hint

Sample Explanation

In sample 11, all non-zero values of f(u,v,i)f(u, v, i) are:

  • f(1,2,3)=1f(1, 2, 3) = 1;
  • f(1,2,4)=1f(1, 2, 4) = 1;
  • f(1,3,2)=1f(1, 3, 2) = 1;
  • f(1,3,4)=1f(1, 3, 4) = 1;
  • f(1,4,2)=1f(1, 4, 2) = 1;
  • f(1,4,3)=1f(1, 4, 3) = 1;
  • f(2,3,4)=1f(2, 3, 4) = 1;
  • f(2,4,3)=1f(2, 4, 3) = 1;
  • f(3,4,2)=1f(3, 4, 2) = 1.

Constraints

This problem uses bundled testdata and subtask dependencies are enabled.

Subtask ID Score n≤n \le Special Property Dependencies
11 88 5050 None None
22 1515 400400 11
33 2424 30003000 1,21, 2
44 1717 2⋅1052 \cdot 10^5 ui=i,vi=i+1u_i = i, v_i = i + 1 None
55 3636 None 1,2,3,41, 2, 3, 4

For all testdata, 2≤n≤2⋅1052 \le n \le 2 \cdot 10^5, and the input graph is a tree.

Translated by ChatGPT 5