問題一覧 > 通常問題

No.2638 Initial fare

レベル : / AI推定難易度 : 725 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 116
作問者 : ramdos / テスター : shobonvip noya2
お気に入りにしたユーザー ProblemId : 10662 / 自分の提出
問題文最終更新日: 2024-02-17 23:13:29
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
traP 作問ハッカソンコンテスト 002(day2) (順位表) の他の問題:

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もしくは右上の雲マークをクリックしてアカウントを作成してください。