103027: [Atcoder]ABC302 Ex - Ball Collector

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:1 Solved:0

Description

Score : $625$ points

Problem Statement

We have a tree with $N$ vertices. The $i$-th $(1 \le i \le N-1)$ edge is an undirected edge between vertices $U_i$ and $V_i$. Vertex $i$ $(1 \le i \le N)$ has a ball with $A_i$ written on it and another with $B_i$.

For each $v = 2,3,\dots,N$, answer the following question. (Each query is independent.)

  • Consider traveling from vertex $1$ to vertex $v$ on the shortest path. Every time you visit a vertex (including vertices $1$ and $v$), you pick up one ball placed there. Find the maximum number of distinct integers written on the picked-up balls.

Constraints

  • $2 \le N \le 2 \times 10^5$
  • $1 \le A_i,B_i \le N$
  • The given graph is a tree.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

$N$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$A_N$ $B_N$
$U_1$ $V_1$
$U_2$ $V_2$
$\vdots$
$U_{N-1}$ $V_{N-1}$

Output

Print the answers for $v=2,3,\dots,N$, separated by spaces.


Sample Input 1

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

Sample Output 1

2 3 3

For example, when $v=4$, you visit vertices $1,2,3$, and $4$. By choosing balls with $A_1,B_2,B_3,B_4(=1,3,1,2)$ written on them, the number of distinct integers on the balls is $3$, which is the maximum.


Sample Input 2

10
2 5
2 2
8 8
4 3
6 10
8 1
9 10
1 7
9 3
5 10
9 3
1 9
3 6
4 1
3 8
10 9
5 4
7 2
9 7

Sample Output 2

4 3 2 3 4 3 4 2 3

Input

题意翻译

有一棵 $N$ 个点的树,每个顶点 $i$ 上有两个球,一个写着 $A_i$,一个写着 $B_i$。 树共有 $N-1$ 条边,对于每条边 $i$ 连接点 $U_i$ 和 $V_i$。 接着,给定 $N-1$ 次**互相独立的询问**: 当 $v=2,3,\dots,N$ 时: 求点 $1$ 到点 $v$ 的**最短路径**,这条路径(包含 $1$ 和 $v$)所经过的点 $i$,必须选择 $A_i$ 和 $B_i$ 两个小球中的一个。求问每次操作最多能选几个标数不同的小球。

Output

得分:$625$分

问题描述

我们有一个有$N$个顶点的树。第$(1 \le i \le N-1)$条边是一个连接顶点$U_i$和$V_i$的无向边。第$(1 \le i \le N)$个顶点有一个写有$A_i$的球和另一个写有$B_i$的球。

对每个$v = 2,3,\dots,N$,回答以下问题。(每个查询都是独立的。)

  • 考虑从顶点$1$沿最短路径到达顶点$v$。每次访问一个顶点(包括顶点$1$和$v$),你都会捡起那里放置的一个球。找出写在捡起的球上的不同整数的最大数量。

限制条件

  • $2 \le N \le 2 \times 10^5$
  • $1 \le A_i,B_i \le N$
  • 给定的图是一棵树。
  • 输入中的所有值都是整数。

输入

输入以以下格式从标准输入给出:

$N$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$A_N$ $B_N$
$U_1$ $V_1$
$U_2$ $V_2$
$\vdots$
$U_{N-1}$ $V_{N-1}$

输出

以空格分隔的方式打印出对于$v=2,3,\dots,N$的答案。


样例输入 1

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

样例输出 1

2 3 3

例如,当$v=4$时,您访问顶点$1,2,3$和$4$。通过选择写有$A_1,B_2,B_3,B_4(=1,3,1,2)$的球,球上的不同整数的数量为$3$,这是最大的。


样例输入 2

10
2 5
2 2
8 8
4 3
6 10
8 1
9 10
1 7
9 3
5 10
9 3
1 9
3 6
4 1
3 8
10 9
5 4
7 2
9 7

样例输出 2

4 3 2 3 4 3 4 2 3

加入题单

算法标签: