AtCoder Beginner Contest 438
F
Sum of Mex
AtCoder
⌀ 1852
原题
←G →
翻译中…

Problem Statement

翻译中…

You are given a tree $T$ with $N$ vertices. The vertices are numbered from $0$ to $N-1$, and the $i$-th edge $(1\le i\le N-1)$ bidirectionally connects vertices $u_i$ and $v_i$. (Note that vertex numbers are 0-indexed and edge numbers are 1-indexed.)

翻译中…

For a pair of integers $(i,j)$ where $0 \leq i,j < N$, define $f(i,j)$ as follows:

翻译中…
  • The vertex number of the vertex with the smallest number among the vertices not included in the path from vertex $i$ to vertex $j$ in tree $T$.
  • Here, if the path from vertex $i$ to vertex $j$ includes all vertices from vertex $0$ to vertex $N-1$, let $f(i,j)=N$.
翻译中…

Note that the path from vertex $i$ to vertex $j$ in tree $T$ includes vertices $i$ and $j$.

翻译中…

Find the value of $\displaystyle \sum_{0\le i \le j < N} f(i,j)$.

翻译中…

Constraints

翻译中…
  • $2\le N\le 2\times 10^5$
  • $0\le u_i < v_i < N$
  • The graph given in the input is a tree.
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

翻译中…

Output

翻译中…

Output the value of $\displaystyle \sum_{0\le i \le j < N} f(i,j)$.

翻译中…

样例

Input #1
2
0 1
Output #1
3
We have $f(0,0)=1,f(0,1)=2,f(1,1)=0$. Thus, output $1+2+0=3$.
Input #2
5
0 1
0 2
2 3
2 4
Output #2
16
Input #3
7
1 4
2 6
0 5
0 3
2 5
1 5
Output #3
16
题目信息
题号
abc438_f
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。