CF1797F.Li Hua and Path

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Li Hua has a tree of nn vertices and n−1n-1 edges. The vertices are numbered from 11 to nn.

A pair of vertices (u,v)(u,v) (u<vu \lt v) is considered cute if exactly one of the following two statements is true:

  • uu is the vertex with the minimum index among all vertices on the path (u,v)(u,v).
  • vv is the vertex with the maximum index among all vertices on the path (u,v)(u,v).

There will be mm operations. In each operation, he decides an integer kjk_j, then inserts a vertex numbered n+jn+j to the tree, connecting with the vertex numbered kjk_j.

He wants to calculate the number of cute pairs before operations and after each operation.

Suppose you were Li Hua, please solve this problem.

李华有一棵包含 nn 个顶点和 n−1n-1 条边的树。顶点编号为 11 到 nn。

若一对顶点 (u,v)(u,v)(满足 u<vu \lt v)恰好满足以下两个命题中的一个为真,则称其为“可爱对”:

  • uu 是路径 (u,v)(u,v) 上所有顶点中编号最小的顶点;
  • vv 是路径 (u,v)(u,v) 上所有顶点中编号最大的顶点。

接下来将进行 mm 次操作。每次操作中,他选定一个整数 kjk_j,并向树中插入一个编号为 n+jn+j 的新顶点,并将其与编号为 kjk_j 的顶点相连。

他希望计算初始时(所有操作前)以及每次操作后的“可爱对”的数量。

假设你是李华,请解决该问题。

输入格式

The first line contains the single integer nn (2≤n≤2⋅1052\le n\le 2\cdot 10^5) — the number of vertices in the tree.

Next n−1n-1 lines contain the edges of the tree. The ii-th line contains two integers uiu_i and viv_i (1≤ui,vi≤n1\le u_i,v_i\le n; ui≠viu_i\ne v_i) — the corresponding edge. The given edges form a tree.

The next line contains the single integer mm (1≤m≤2⋅1051\le m\le 2\cdot 10^5) — the number of operations.

Next mm lines contain operations — one operation per line. The jj-th operation contains one integer kjk_j (1≤kj<n+j1\le k_j \lt n+j) — a vertex.

第一行包含一个整数 nn(2≤n≤2⋅1052\le n\le 2\cdot 10^5)——树中顶点的数量。

接下来的 n−1n-1 行描述树的边。第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1\le u_i,v_i\le n;ui≠viu_i\ne v_i)——对应的一条边。所给的边构成一棵树。

接下来一行包含一个整数 mm(1≤m≤2⋅1051\le m\le 2\cdot 10^5)——操作的数量。

接下来的 mm 行每行描述一个操作。第 jj 个操作包含一个整数 kjk_j(1≤kj<n+j1\le k_j \lt n+j)——一个顶点。

输出格式

Print m+1m+1 integers — the number of cute pairs before operations and after each operation.

输出 m+1m+1 个整数——分别为操作前以及每次操作后的“可爱对”数量。

输入输出样例

  • 输入#1

    7
    2 1
    1 3
    1 4
    4 6
    4 7
    6 5
    2
    5
    6

    输出#1

    11
    15
    19

说明/提示

The initial tree is shown in the following picture:

There are 1111 cute pairs — (1,5),(2,3),(2,4),(2,6),(2,7),(3,4),(3,6),(3,7),(4,5),(5,7),(6,7)(1,5),(2,3),(2,4),(2,6),(2,7),(3,4),(3,6),(3,7),(4,5),(5,7),(6,7).

Similarly, we can count the cute pairs after each operation and the result is 1515 and 1919.

初始树如下图所示:

共有 1111 个“可爱对”——(1,5),(2,3),(2,4),(2,6),(2,7),(3,4),(3,6),(3,7),(4,5),(5,7),(6,7)(1,5),(2,3),(2,4),(2,6),(2,7),(3,4),(3,6),(3,7),(4,5),(5,7),(6,7)。

类似地,我们可以统计每次操作后的“可爱对”数量,结果分别为 1515 和 1919。

输入解题思路,AI测评打分。不知道怎么写?

首页