QOJ.ac

QOJ

Límite de tiempo: 3 s Límite de memoria: 512 MB Puntuación total: 100 Hackeable ✓

#17536. Cực đại hóa tổng khoảng cách

Estadísticas

Cho một cây (đồ thị vô hướng liên thông không có chu trình) gồm $N$ đỉnh. Các đỉnh được đánh số từ $1$ đến $N$, và các cạnh được đánh số từ $1$ đến $(N-1)$.

Hãy viết chương trình thực hiện các truy vấn sau:

  • $u$ $v$ : Với mỗi đỉnh $x$ ($1 \le x \le N$), hãy in ra giá trị lớn nhất của $\operatorname{dist}(x, u) + \operatorname{dist}(x, v)$. ($1 \le u, v \le N$)

Trong đó, $\operatorname{dist}(x, y)$ được định nghĩa là số lượng cạnh trên đường đi ngắn nhất từ đỉnh $x$ đến đỉnh $y$. Với mọi đỉnh $x$ trong cây, $\operatorname{dist}(x, x) = 0$.

Dữ liệu vào

Dòng đầu tiên chứa số lượng đỉnh $N$ của cây. ($2 \le N \le 300000$)

$(N-1)$ dòng tiếp theo chứa thông tin về cây. Dòng thứ $i$ chứa hai số nguyên cách nhau bởi dấu cách, biểu thị hai đỉnh mà cạnh thứ $i$ kết nối.

Dòng tiếp theo chứa số lượng truy vấn $Q$. ($2 \le Q \le 300000$)

$Q$ dòng tiếp theo, mỗi dòng chứa thông tin của một truy vấn.

Dữ liệu ra

In ra kết quả của các truy vấn theo thứ tự trên $Q$ dòng.

Ví dụ

Dữ liệu vào 1

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

Dữ liệu ra 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.