QOJ.ac

QOJ

Límite de tiempo: 10 s Límite de memoria: 512 MB Puntuación total: 100

#7338. 教育噩梦

Estadísticas

你正在做一场噩梦!在梦中,你已经完成了学业,现在要开始教书育人了。今天是你的第一天!你(稍微有点迟到地)进入了教学楼,然后……当然,你忘记了你的课在哪个教室上。

教学楼由 $n$ 个房间组成,编号为 $1, 2, \dots, n$。一些房间对之间由通道相连。你可以在相连的房间之间移动,每次移动恰好耗时一秒。教学楼内共有 $n-1$ 条通道,且只要时间足够,每个房间都能到达。换句话说,房间和通道构成了一棵树。

你从房间 $s$ 开始。如果你进入了正确的房间,你会立即认出它并结束你的寻找。即便如此,搜索所有的房间可能需要很长时间……幸运的是,你还有一个技巧可以使用:在房间 $m$ 里有一份完整的课程表。如果你进入了房间 $m$,你就会立即知道教室在哪里,并且可以立即前往那里,走最短路径(请注意:房间 $m$ 不提供打印、传真、扫描或复印服务。你不应该向他们询问这些)。

求在最坏情况下找到教室所需的最短时间 $t$,即存在一种策略,保证能在 $t$ 秒内找到正确房间的最小 $t$ 值。

输入格式

输入的第一行包含测试用例的数量 $z$ ($1 \le z \le 10^9$)。接下来是各测试用例的描述。

每个测试用例的第一行包含三个整数:房间总数 $n$、起始房间 $s$ 以及存放课程表的房间 $m$ ($1 \le n \le 200\,000, 1 \le s, m \le n$)。接下来有 $n-1$ 行,每行包含两个整数 $a, b$ ($1 \le a, b \le n, a \neq b$),表示房间 $a$ 和 $b$ 之间的一条通道。保证任意两个房间之间都是连通的。

课程可能在房间 $s$ 或房间 $m$ 进行。也有可能从房间 $m$ 开始。所有测试用例的房间总数不超过 $10^7$。

输出格式

对于每个测试用例,输出一个整数:在采取最优策略的情况下,找到教室所需的最短总时间(秒)。

样例

样例输入 1

1
4 2 3
1 2
1 3
1 4

样例输出 1

4

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.