No.2638 Initial fare
Problem Statement
Railway company A has $N$ stations and $N−1$ tracks. The $i$-th track connects stations $u_i$ and $v_i$, and its length is $1$ km.
All stations can be reached from one another via the tracks. That is, the railway network is a tree.
In this railway company, you can ride at the initial fare if and only if the riding distance is at most $3$ km.
How many unordered pairs of $2$ distinct stations are there such that the fare between them is the initial fare?
Constraints
- $2 \leq N \leq 2 \times 10^5$
- $1 \leq u_i,v_i \leq N$ for each $i$ such that $1 \leq i \leq N-1$
- All inputs are integers
- The given graph is a tree
Input
$N$
$u_1\ v_1$
$\vdots$
$u_{N-1}\ v_{N-1}$
- $N$ is the number of stations
- For each $i$ such that $1 \leq i \leq N-1$, there is a track bidirectionally connecting station $u_i$ and station $v_i$
Output
Output the answer on one line.
Sample
Sample 1
Input
5 1 2 2 3 3 4 4 5
Output
9
All pairs except $(1,5)$ can be traveled at the initial fare.
Sample 2
Input
8 1 2 2 3 2 4 2 5 5 6 6 7 6 8
Output
22
All pairs except $(1,7),(1,8),(3,7),(3,8),(4,7),(4,8)$ can be traveled at the initial fare.
Sample 3
Input
2 1 2
Output
1
The distance between $(1,2)$ is $1$ km, so it can be traveled at the initial fare.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
noya2