- 题目提供者 FarmerJohn2
- 评测方式 云端评测
- 标签 Link-Cut Tree,LCT 最近公共祖先,LCA 连通块 USACO 2018
- 难度 省选/NOI-
- 时空限制 1000ms / 128MB
Farmer John注意到他的奶牛们如果被关得太紧就容易吵架，所以他想开放一些新的牛棚来分散她们。 每当FJ建造一个新牛棚的时候，他会将这个牛棚用至多一条双向道路与一个现有的牛棚连接起来。为了确保他的奶牛们足够分散，他有时想要确定从某个特定的牛棚出发，到它能够到达的最远的牛棚的距离（两个牛棚之间的距离等于从一个牛棚出发到另一个之间必须经过的道路条数）。
FJ总共会给出$Q$（$1 \leq Q \leq 10^5$）次请求，每个请求都是“建造”和“距离”之一。对于一个建造请求，FJ建造一个牛棚，并将其与至多一个现有的牛棚连接起来。对于一个距离请求，FJ向你询问从某个特定的牛棚通过一些道路到离它最远的牛棚的距离。保证询问的牛棚已经被建造。请帮助FJ响应所有的请求。
输入格式（文件名：newbarn.in）： 第一行包含一个整数$Q$。以下$Q$行，每行包含一个请求。每个请求的格式都是“B p”或是“Q k”，分别告诉你建造一个牛棚并与牛棚$p$连接，或是根据定义求从牛棚$k$出发最远的距离。如果$p = -1$，则新的牛棚不会与其他牛棚连接。否则，$p$是一个已经建造的牛棚的编号。牛棚编号从$1$开始，所以第一个被建造的谷仓是$1$号谷仓，第二个是$2$号谷仓，以此类推。 输出格式（文件名：newbarn.out）： 对于每个距离请求输出一行。注意一个没有连接到其他牛棚的牛棚的最远距离为$0$。
Farmer John notices that his cows tend to get into arguments if they are packed too closely together, so he wants to open a series of new barns to help spread them out. Whenever FJ constructs a new barn, he connects it with at most one bidirectional pathway to an existing barn. In order to make sure his cows are spread sufficiently far apart, he sometimes wants to determine the distance from a certain barn to the farthest possible barn reachable from it (the distance between two barns is the number of paths one must traverse to go from one barn to the other).
FJ will give a total of $Q$ ($1 \leq Q \leq 10^5$) queries, each either of the form "build" or "distance". For a build query, FJ builds a barn and links it with at most one previously built barn. For a distance query, FJ asks you the distance from a certain barn to the farthest barn reachable from it via a series of pathways. It is guaranteed that the queried barn has already been built. Please help FJ answer all of these queries.
The first line contains the integer $Q$. Each of the next $Q$ lines contains a query. Each query is of the form "B p" or "Q k", respectively telling you to build a barn and connect it with barn $p$, or give the farthest distance, as defined, from barn $k$. If $p = -1$, then the new barn will be connected to no other barn. Otherwise, $p$ is the index of a barn that has already been built. The barn indices start from $1$, so the first barn built is barn $1$, the second is barn $2$, and so on.输出格式：
Please write one line of output for each distance query. Note that a barn which is connected to no other barns has farthest distance $0$.
The example input corresponds to this network of barns:
(1) \ (2)---(4) / (3)
In query 1, we build barn number 1. In query 2, we ask for the distance of 1 to the farthest connected barn. Since barn 1 is connected to no other barns, the answer is 0. In query 3, we build barn number 2 and connect it to barn 1. In query 4, we build barn number 3 and connect it to barn 2. In query 5, we ask for the distance of 3 to the farthest connected barn. In this case, the farthest is barn 1, which is 2 units away. In query 6, we build barn number 4 and connect it to barn 2. In query 7, we ask for the distance of 2 to the farthest connected barn. All three barns 1, 3, 4 are the same distance away, which is 1, so this is our answer.
Problem credits: Anson Hu