#2797. [HBCPC2024] Colorful Tree

    ID: 2797 传统题 2000ms 512MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>P10866并查集2024O2优化省赛/邀请赛湖北

[HBCPC2024] Colorful Tree

[HBCPC2024] Colorful Tree

题目描述

You are given a tree with $n$ nodes numbered from $1$ to $n$ and $n - 1$ edges. Each node has a color. Initially, all of them are white.

We are going to perform qq operations. In each operation, two vertices uu and vv will be given, and we will color black to the points along the simple path from uu to vv (u,vu,v inclusive). Note that a simple path in the tree is defined as a path that does not pass through any vertex more than once.

After each operation, you are required to determine the length of the longest simple path in the tree where all nodes on the path are the same color. The length of a path is defined as the number of nodes on the path.

输入格式

The first line contains a single integer $T$ ($1 \le T \le 100$) indicating the number of test cases.

For each test case, the first line contains two integers nn (1≤n≤2×1051 \le n \le 2\times 10^5) and qq (1≤q≤2×1051 \le q \le 2\times 10^5), indicating the number of nodes in the tree and the number of operations, respectively.

In the following n−1n-1 lines, each contains two integers uu and vv, representing an edge from vertex uu to vv in the tree.

Then follow qq lines, each contains two integers uu and vv, representing an operation that colors black to the points along the path from vertex uu to vv.

It is guaranteed that the sum of nn and qq of all the test cases in a test does not exceed 2×1052\times 10^5 respectively.

输出格式

For each test case, output $q$ lines, each line should contain an integer representing the length of the longest simple path in the tree where all nodes on the path are the same color after the corresponding operation.
1
8 6
1 2
1 3
2 4
4 5
2 6
4 8
3 7
4 8
7 7
4 5
2 2
4 6
5 1
5
4
4
3
4
4

提示

(暂无提示) 标签: P10866|并查集|2024|O2优化|省赛/邀请赛|湖北

来源

P10866|[HBCPC2024] Colorful Tree