#SRD0000123. Những chuồng bò mới (New Barns)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Những chuồng bò mới (New Barns)
New Barns
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
Initially, no barns exist. Process operations that construct a forest of barns and ask distance queries.
B p: build a new barn with index equal to the number of already built barns plus one. If , the new barn starts a new connected component. Otherwise, add one bidirectional path from the new barn to the already existing barn .Q k: find the maximum distance from barn to any other barn reachable from via the current paths. Distance is the number of edges along the path.
Every new barn connects to at most one old barn, so the graph remains a forest. Barns in different connected components do not contribute to each other's answers. An isolated barn has answer .
Input
- The first line contains integer .
- The next lines contain an operation
B porQ k. - For
B p, either or is an existing barn index. - For
Q k, is an existing barn index; queries are guaranteed to be valid.
Output
For each Q k operation, print one line with the maximum distance from to a barn in the same connected component.
Subtasks
General guarantees: ; barn indices start at ; all operations are valid.
- Subtask 1 (10 points): ; all general guarantees remain in force.
- Subtask 2 (20 points): ; all general guarantees remain in force.
- Subtask 3 (30 points): ; all general guarantees remain in force.
- Subtask 4 (40 points): ; all general guarantees remain in force (full original constraints).
Examples
Example 1
Input
7
B -1
Q 1
B 1
B 2
Q 3
B 2
Q 2
Output
0
2
1
Explanation
After constructing isolated barn , Q 1 is . Building barns attached to and then produces the path , so the farthest barn from is , at distance . After building barn attached to , every other barn in that component is one edge away from , giving the final answer .