LIS On Tree

시간 제한4초메모리 제한1024 MB

요약
매 갱신마다 i번째 값을 새 노드에 채우고, 채워진 노드들로 이루어진 임의의 경로 위에서 가장 긴 증가 부분 수열의 길이를 구한다.
난이도

어려움10점 중 8점

유형
트리, 동적 계획법, DFS, 세그먼트 트리
정답자
아직 제출이 없습니다

문제

There were too many constructive problems in this contest, so we decided to set a standard data structure problem.

You are given a tree with nn labeled nodes. Each node also has a blank value initially.

The longest increasing tree subsequence between two nodes (u,v)(u, v) on the tree is computed as follows:

  • Write down all the nonblank values from the nodes on the path from uu to vv, in order. Compute the longest increasing subsequence†^\dagger of the resulting sequence.

You are given nn updates, x_1,x_2…x_nx\_1, x\_2 \dots x\_n. For update ii, fill in the value ii at node x_ix\_i. After each update, compute the length of the longest longest increasing tree subsequence among all pairs of nodes (u,v)(u, v) in the tree.

It is guaranteed that all x_ix\_i values are distinct.

†^\dagger Define a sequence of integers a_i...a_ma\_i...a\_m. A subsequence a_i_1,a_i_2,...,a_i_ka\_{i\_1}, a\_{i\_2}, ..., a\_{i\_k} where 1≤i_1<i_2<⋯<i_k≤m1 \leq i\_1 < i\_2 < \cdots < i\_k \leq m is called increasing if a_i_1<a_i_2<a_i_3<...<a_i_ka\_{i\_1} < a\_{i\_2} < a\_{i\_3} < ... < a\_{i\_k}. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.

입력

The first line of the input contains an integer 1≤t≤1041 \leq t \leq 10^4, denoting the number of test cases.

The first line of each test case contains one integer 2≤n≤5⋅1052 \leq n \leq 5 \cdot 10^5.

The next n−1n - 1 lines contain two integers 1≤u,v≤n1 \leq u, v \leq n, denoting an undirected edge between the nodes with labels uu and vv, respectively. It is guaranteed that the input edges form a tree.

The last line of input for the testcase consists of nn integers x_1...x_nx\_1...x\_n, denoting the updates in order. It is guaranteed that all x_ix\_i values are distinct.

It is guaranteed that the sum of nn across all test cases does not exceed 5⋅1055 \cdot 10^5.

출력

For each test case, print nn space-separated integers on a single line, denoting the answer after the ithi^\textrm{th} update.

힌트

Remember that the updates **tell you the value of the x\_i^\textrm{th** node}, not that the value of node ii is x_ix\_i.

An example of the process for the first tree is shown below. The yellow nodes are one potential longest increasing tree subsequence after each operation. The node's labels are 1-5 from left to right, initially with blank values.

예제1

  1. 예제 1

    입력
    4
    5
    1 2
    2 3
    3 4
    4 5
    3 1 5 2 4
    10
    5 1
    3 4
    3 6
    3 7
    8 3
    5 8
    2 5
    9 2
    9 10
    3 6 9 4 5 2 1 8 10 7
    15
    10 1
    3 4
    13 5
    3 7
    8 3
    15 8
    12 13
    9 12
    2 9
    11 2
    11 14
    6 11
    10 6
    10 15
    9 2 10 5 13 14 7 15 11 12 1 6 8 3 4
    2
    1 2
    1 2
    
    예상 출력
    1 2 2 2 3
    1 2 2 2 2 3 3 3 4 4
    1 2 3 3 3 3 4 4 4 4 4 4 5 6 7
    1 2