QOJ.ac

QOJ

実行時間制限: 1.0 s メモリ制限: 2048 MB 満点: 100

#16833. Elevated Rails

統計

Brandon 和 Geoffry 喜欢火车!他们被任务要求建造一个连接 $n$ 个火车站的铁路网络。每个车站都位于三座岛屿之一,且每座岛屿上至少有一个车站。

Brandon 一直在努力建立同一岛屿上车站之间的连接。具体来说,Brandon 已经建立了足够多的连接,使得在同一岛屿上的任意两个车站之间,恰好只有一种乘火车的方法可以在不重复经过任何车站的情况下到达。然而,目前还无法在不同岛屿的两个车站之间乘坐火车。

Geoffry 看着 Brandon 目前建好的铁路网络,向他提出了几个问题。每个问题会选择两个当前处于不同岛屿的车站,并询问:如果 Brandon 恰好再添加两条连接,使得所有车站之间都可以互相到达,那么这两个车站之间的路径上最多可以包含多少个车站?

Brandon 忙于处理铁路信号,无暇思考如何连接不同岛屿上的车站,因此将 Geoffry 的所有问题都交给你来回答。

输入格式

输入的第一行包含两个整数 $n$ 和 $q$($3 \le n \le 10^5$,$1 \le q \le 2 \cdot 10^5$),分别表示火车站的数量和 Geoffry 提问的数量。

接下来的 $n - 3$ 行,每行包含两个整数 $x$ 和 $y$($1 \le x < y \le n$),表示车站 $x$ 和 $y$ 之间有一条双向铁路相连。

保证当前的铁路连接满足上述条件——这 $n$ 个车站可以被分为三座岛屿,使得两个车站能够互相到达当且仅当它们在同一座岛屿上,且它们之间存在唯一一条不重复经过车站的路径。

接下来的 $q$ 行,每行包含两个整数 $a$ 和 $b$,表示询问在车站 $a$ 和车站 $b$ 之间的路径上最多可以出现多少个车站。保证车站 $a$ 和车站 $b$ 位于不同的岛屿上。

每个询问之间是相互独立的。

输出格式

输出 $q$ 行,每行对应一个询问。每个询问的输出应为一个整数,表示车站 $a$ 和车站 $b$ 之间的路径上最多可以出现的车站数量。

样例

输入样例 1

12 3
1 2
2 3
2 4
5 6
8 9
9 10
9 11
7 8
11 12
2 5
11 4
7 6

输出样例 1

9
9
10

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.