问题 A: LCT

内存限制:128 MB 时间限制:2 S
题面:Markdown 评测方式:文本比较 上传者:
提交:12 通过:6

题目描述

Note the **unusual memory limit** for this problem. Given a rooted tree with $\textstyle n$ nodes and $\textstyle n-1$ edges. The $\textstyle i$\-th edge can be described by $\textstyle (a_i, b_i)$, which represents an edge connecting node $\textstyle a_i$ and node $\textstyle b_i$, where node $\textstyle a_i$ is the parent of node $\textstyle b_i$. There are $\textstyle n-1$ queries. The $\textstyle i$\-th query contains an integer $\textstyle c_i$, asking for the length of the longest simple path starting from node $\textstyle c_i$ in the graph (forest) formed by the first $\textstyle i$ edges. **It is guaranteed that there does not exist $\textstyle j$ such that $\textstyle 1 \leq j \leq i$ and $\textstyle b_j = c_i$, i.e., $\textstyle c_i$ is the root of a tree in the forest.**

输入格式

Each test contains multiple test cases. The first line contains the number of test cases $\textstyle t$ ($\textstyle 1 \leq t \leq 10^5$). The description of the test cases follows. The first line of each test case contains an integer $\textstyle n$ ($\textstyle 2 \leq n \leq 10^6$), representing the number of nodes in the tree. The next $\textstyle n-1$ lines each contain three integers $\textstyle a_i, b_i, c_i$ ($\textstyle 1 \leq a_i, b_i, c_i \leq n, a_i \neq b_i$), representing that the parent of node $\textstyle b_i$ is $\textstyle a_i$, and the $\textstyle i$\-th query is $\textstyle c_i$. It is guaranteed that: - For each test case, the $\textstyle n-1$ edges form a rooted tree. - For each test case, for any $\textstyle 1 \leq i \leq n-1$, there does not exist $\textstyle j$ such that $\textstyle 1 \leq j \leq i$ and $\textstyle b_j = c_i$. - The sum of $\textstyle n$ over all test cases does not exceed $\textstyle 10^6$.

输出格式

For each test case, output $\textstyle n-1$ integers in one line. The $\textstyle i$\-th integer represents your answer to the $\textstyle i$\-th query.

输入样例 复制

6
4
3 4 1
2 3 1
1 2 1
4
3 4 3
2 1 2
3 2 3
4
1 2 1
3 4 3
2 3 1
4
2 3 1
2 4 2
1 2 1
2
1 2 1
2
2 1 2

输出样例 复制

0 0 3
1 1 2
1 1 3
0 1 2
1
1

数据范围与提示

输入 -- ``` 2 5 1 2 3 1 3 4 3 4 1 1 5 1 15 10 14 10 5 8 5 1 3 1 4 7 10 2 5 2 1 2 1 3 4 1 9 10 9 11 13 11 5 6 1 10 12 9 8 9 1 11 15 11 7 11 1``` 输出 -- 0 0 2 2 1 1 1 1 2 3 3 2 1 3 2 6 1 6 ```