CF1453E.Dog Snacks

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Gildong is playing with his dog, Badugi. They're at a park that has nn intersections and n−1n-1 bidirectional roads, each 11 meter in length and connecting two intersections with each other. The intersections are numbered from 11 to nn, and for every aa and bb (1≤a,b≤n1 \le a, b \le n), it is possible to get to the bb-th intersection from the aa-th intersection using some set of roads.

Gildong has put one snack at every intersection of the park. Now Gildong will give Badugi a mission to eat all of the snacks. Badugi starts at the 11-st intersection, and he will move by the following rules:

  • Badugi looks for snacks that are as close to him as possible. Here, the distance is the length of the shortest path from Badugi's current location to the intersection with the snack. However, Badugi's sense of smell is limited to kk meters, so he can only find snacks that are less than or equal to kk meters away from himself. If he cannot find any such snack, he fails the mission.
  • Among all the snacks that Badugi can smell from his current location, he chooses a snack that minimizes the distance he needs to travel from his current intersection. If there are multiple such snacks, Badugi will choose one arbitrarily.
  • He repeats this process until he eats all nn snacks. After that, he has to find the 11-st intersection again which also must be less than or equal to kk meters away from the last snack he just ate. If he manages to find it, he completes the mission. Otherwise, he fails the mission.

Unfortunately, Gildong doesn't know the value of kk. So, he wants you to find the minimum value of kk that makes it possible for Badugi to complete his mission, if Badugi moves optimally.

吉尔东正在和他的狗巴杜吉玩耍。他们身处一个拥有 nn 个路口和 n−1n-1 条双向道路的公园中,每条道路长度均为 11 米,并连接两个不同的路口。路口编号为 11 至 nn,且对任意 aa 和 bb(1≤a,b≤n1 \le a, b \le n),总存在若干条道路使得可以从第 aa 个路口到达第 bb 个路口。

吉尔东已在公园的每个路口放置了一份零食。现在,吉尔东将给巴杜吉一项任务:吃掉所有零食。巴杜吉从第 11 个路口出发,并遵循以下规则移动:

  • 巴杜吉寻找距离自己最近的零食。此处“距离”定义为从巴杜吉当前所在路口到含有零食的路口之间的最短路径长度。然而,巴杜吉的嗅觉范围限制为 kk 米,因此他仅能发现距离自身不超过 kk 米的零食。若他无法发现任何此类零食,则任务失败。
  • 在巴杜吉当前可嗅探到的所有零食中,他选择一个使其从当前位置出发所需行走距离最小的零食。若存在多个满足该条件的零食,巴杜吉可任选其一。
  • 他不断重复此过程,直至吃掉全部 nn 份零食。此后,他必须再次找到第 11 个路口,且该路口与他刚吃完的最后一份零食所在的路口之间的距离也必须不超过 kk 米。若他成功找到,则任务完成;否则任务失败。

不幸的是,吉尔东并不知道 kk 的值。因此,他希望你找出能使巴杜吉在最优移动策略下成功完成任务的最小 kk 值。

输入格式

Each test contains one or more test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4).

The first line of each test case contains one integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — the number of intersections of the park.

The next n−1n-1 lines contain two integers uu and vv (1≤u,v≤n1 \le u,v \le n, u≠vu \ne v) each, which means there is a road between intersection uu and vv. All roads are bidirectional and distinct.

It is guaranteed that:

  • For each test case, for every aa and bb (1≤a,b≤n1 \le a, b \le n), it is possible to get to the bb-th intersection from the aa-th intersection.
  • The sum of nn in all test cases doesn't exceed 2⋅1052 \cdot 10^5.

每个测试包含一个或多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 表示公园中交叉路口的数量。

接下来的 n−1n-1 行每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u,v \le n,且 u≠vu \ne v),表示交叉路口 uu 与 vv 之间存在一条道路。所有道路均为双向且互不相同。

保证满足以下条件:

  • 对于每个测试用例,任意 aa 和 bb(1≤a,b≤n1 \le a, b \le n),均可从第 aa 个交叉路口到达第 bb 个交叉路口;
  • 所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print one integer — the minimum possible value of kk such that Badugi can complete the mission.

对于每个测试用例,输出一个整数——使得 Badugi 能够完成任务的最小可能的 kk 值。

输入输出样例

  • 输入#1

    3
    3
    1 2
    1 3
    4
    1 2
    2 3
    3 4
    8
    1 2
    2 3
    3 4
    1 5
    5 6
    6 7
    5 8

    输出#1

    2
    3
    3

说明/提示

In the first case, Badugi can complete his mission with k=2k=2 by moving as follows:

  1. Initially, Badugi is at the 11-st intersection. The closest snack is obviously at the 11-st intersection, so he just eats it.
  2. Next, he looks for the closest snack, which can be either the one at the 22-nd or the one at the 33-rd intersection. Assume that he chooses the 22-nd intersection. He moves to the 22-nd intersection, which is 11 meter away, and eats the snack.
  3. Now the only remaining snack is on the 33-rd intersection, and he needs to move along 22 paths to get to it.
  4. After eating the snack at the 33-rd intersection, he needs to find the 11-st intersection again, which is only 11 meter away. As he gets back to it, he completes the mission.

In the second case, the only possible sequence of moves he can make is 11 – 22 – 33 – 44 – 11. Since the distance between the 44-th intersection and the 11-st intersection is 33, kk needs to be at least 33 for Badugi to complete his mission.

In the third case, Badugi can make his moves as follows: 11 – 55 – 66 – 77 – 88 – 22 – 33 – 44 – 11. It can be shown that this is the only possible sequence of moves for Badugi to complete his mission with k=3k=3.

在第一种情况下,Badugi 可以通过 k=2k=2 完成任务,具体移动过程如下:

  1. 最初,Badugi 位于第 11 个路口。显然,距离最近的零食就在第 11 个路口,因此他直接吃掉它。
  2. 接着,他寻找距离最近的零食,该零食可能位于第 22 个路口,也可能位于第 33 个路口。假设他选择了第 22 个路口。他移动到第 22 个路口(距离为 11 米),并吃掉那里的零食。
  3. 此时,唯一剩余的零食位于第 33 个路口,他需要沿 22 条路径前往该处。
  4. 吃掉第 33 个路口的零食后,他需要再次找到第 11 个路口,而该路口仅距他 11 米。当他返回第 11 个路口时,任务即告完成。

在第二种情况下,他唯一可能的移动序列为 11 – 22 – 33 – 44 – 11。由于第 44 个路口与第 11 个路口之间的距离为 33,因此 kk 至少需为 33,Badugi 才能完成任务。

在第三种情况下,Badugi 的移动可如下进行:11 – 55 – 66 – 77 – 88 – 22 – 33 – 44 – 11。可以证明,这是 Badugi 在 k=3k=3 条件下完成任务的唯一可能移动序列。

输入解题思路,AI测评打分。不知道怎么写?

首页