Codeforces Round 1056 (Div. 2)
F
Juan's Colorful Tree
Codeforces
2800
原题
← E→
翻译中…

Juan has a beautiful tree with $n$ nodes numbered from $1$ to $n$. There are also $k$ distinct colors numbered from $1$ to $k$. Each node $u$ in the tree has its own set $C_u$ which contains colors. Let's denote with $s$ the quantity $\sum_{i=1}^n \left| C_i \right|$.

翻译中…

There are $q$ queries where you will be given nodes $u$ and $v$. Let $P$ denote the set of all nodes in the simple path between $u$ and $v$, inclusive. You are asked to calculate for each query the following quantity: $$\left| \bigcap_{w \in P} C_w \right|$$

翻译中…

That is, the cardinality of the intersection of the sets of all the nodes in the path from $u$ to $v$. In other words, calculate how many colors appear in every set on the path from $u$ to $v$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains four integers $n$, $k$, $s$, and $q$ ($1 \le n, k, q \le 3 \cdot 10^5, 1 \le s \le \min (nk, 3 \cdot 10^5)$) — the number of nodes in the tree, the number of distinct colors, the sum of the cardinalities of all sets of colors, and the number of queries, respectively.

翻译中…

The next $n-1$ lines each contain two integers $u$, $v$ ($1 \le u, v \le n$), indicating that there's an edge between nodes $u$ and $v$.

翻译中…

The next $s$ lines each contain two integers $v$ and $x$ ($1 \le v \le n, 1 \le x \le k$) indicating that color $x$ is in $C_v$. All $s$ lines are pairwise distinct.

翻译中…

The next $q$ lines each contain two integers $u$ and $v$ ($1 \le u, v \le n$), indicating a query between nodes $u$ and $v$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.

翻译中…

It is guaranteed that the sum of $k$ over all test cases does not exceed $3 \cdot 10^5$.

翻译中…

It is guaranteed that the sum of $s$ over all test cases does not exceed $3 \cdot 10^5$.

翻译中…

It is guaranteed that the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output a line: the answer to all queries in the order they appear in the input, separated by spaces.

翻译中…

Note

翻译中…

In the first test case, there is a tree of $3$ nodes with edges $(1, 3)$ and $(2, 1)$. The colors in the sets of each node are:

翻译中…
  • $C_1 = \{ 1, 2, 3, 4, 5 \}$
  • $C_2 = \{ 1, 2, 5 \}$
  • $C_3 = \{ 1, 2 \}$
翻译中…

The $4$ queries are between the pairs of nodes $(1, 3)$, $(2, 3)$, $(1, 2)$, $(1, 1)$ and correspond to calculating the following quantities respectively:

翻译中…
  • $\left| C_1 \cap C_3 \right| = \left| \{ 1, 2 \} \right| = 2$
  • $\left| C_2 \cap C_1 \cap C_3 \right| = \left| \{ 1, 2 \} \right| = 2$
  • $\left| C_2 \cap C_1 \right| = \left| \{ 1, 2, 5 \} \right| = 3$
  • $\left| C_1 \right| = \left| \{ 1, 2, 3, 4, 5 \} \right| = 5$
翻译中…

样例

Input #1
2
3 5 10 4
1 3
2 1
1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 5
3 1
3 2
1 3
2 3
1 2
1 1
9 3 12 10
7 2
2 4
6 8
9 6
2 1
5 8
2 5
3 9
1 3
6 1
9 3
9 1
5 1
2 3
8 1
4 3
5 3
8 3
7 3
3 1
4 7
1 4
4 5
5 5
4 2
9 9
2 2
2 2
5 2
7 3
Output #1
2 2 3 5
1 1 1 2 1 2 1 1 1 0
InputCopy OutputCopy
题目信息
题号
2155F
时间限制
3 s
内存限制
1024 MB
标签
数据结构
DFS 及类似
并查集
图论
折半搜索
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。