atcoder#ABC173F. [ABC173F] Intervals on Tree

[ABC173F] Intervals on Tree

Score : 600600 points

Problem Statement

We have a tree with NN vertices and N1N-1 edges, respectively numbered 1,2,,N1, 2,\cdots, N and 1,2,,N11, 2, \cdots, N-1. Edge ii connects Vertex uiu_i and viv_i.

For integers L,RL, R (1LRN1 \leq L \leq R \leq N), let us define a function f(L,R)f(L, R) as follows:

  • Let SS be the set of the vertices numbered LL through RR. f(L,R)f(L, R) represents the number of connected components in the subgraph formed only from the vertex set SS and the edges whose endpoints both belong to SS.

Compute L=1NR=LNf(L,R)\sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1ui,viN1 \leq u_i, v_i \leq N
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

u1u_1 v1v_1

u2u_2 v2v_2

::

uN1u_{N-1} vN1v_{N-1}

Output

Print L=1NR=LNf(L,R)\sum_{L=1}^{N} \sum_{R=L}^{N} f(L, R).

3
1 3
2 3
7

We have six possible pairs (L,R)(L, R) as follows:

  • For L=1,R=1L = 1, R = 1, S={1}S = \{1\} and we have 11 connected component.
  • For L=1,R=2L = 1, R = 2, S={1,2}S = \{1, 2\} and we have 22 connected components.
  • For L=1,R=3L = 1, R = 3, S={1,2,3}S = \{1, 2, 3\} and we have 11 connected component, since SS contains both endpoints of each of the edges 1,21, 2.
  • For L=2,R=2L = 2, R = 2, S={2}S = \{2\} and we have 11 connected component.
  • For L=2,R=3L = 2, R = 3, S={2,3}S = \{2, 3\} and we have 11 connected component, since SS contains both endpoints of Edge 22.
  • For L=3,R=3L = 3, R = 3, S={3}S = \{3\} and we have 11 connected component.

The sum of these is 77.

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