QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 512 MB Total points: 100 Hackable ✓

#17536. 距離の総和の最大化

Statistics

$N$ 個の頂点からなる木(サイクルを持たない無向連結グラフ)がある。頂点には $1$ から $N$ までの番号が、辺には $1$ から $(N-1)$ までの番号が付けられている。

以下のクエリを処理するプログラムを作成せよ。

  • $u$ $v$ : 頂点 $x$ ($1 \le x \le N$) について、$\operatorname{dist}(x, u) + \operatorname{dist}(x, v)$ の最大値を出力する。 ($1 \le u, v \le N$)

ここで、$\operatorname{dist}(x, y)$ は頂点 $x$ から頂点 $y$ への最短経路上の辺の数として定義される。木のすべての頂点 $x$ について $\operatorname{dist}(x, x) = 0$ である。

入力

1行目に木の頂点数 $N$ が与えられる。 ($2 \le N \le 300000$)

続く $(N-1)$ 行には木の情報が与えられる。そのうち $i$ 行目には、$i$ 番目の辺が接続する2つの頂点番号が空白区切りで与えられる。

次の行にクエリの数 $Q$ が与えられる。 ($2 \le Q \le 300000$)

続く $Q$ 行には、クエリの情報が1行ずつ与えられる。

出力

$Q$ 行にわたり、各クエリの答えを順に出力せよ。

入出力例

入力 1

5
1 2
2 3
2 4
4 5
3
1 3
1 5
2 3

出力 1

6
5
5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.