Sum of Mex

AtCoder
IDabc438_f
Time2000ms
Memory256MB
Difficulty
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}$ [samples]
Samples
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
API Response (JSON)
{
  "problem": {
    "name": "Sum of Mex",
    "description": {
      "content": "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 num",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc438_f"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 num...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments