CF1904E.Tree Queries

省选/NOI-

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Those who don't work don't eat. Get the things you want with your own power. But believe, the earnest and serious people are the ones who have the last laugh... But even then, I won't give you a present.

—Santa, Hayate no Gotoku!

Since Hayate didn't get any Christmas presents from Santa, he is instead left solving a tree query problem.

Hayate has a tree with nn nodes.

Hayate now wants you to answer qq queries. Each query consists of a node xx and kk other additional nodes a1,a2,…,aka_1,a_2,\ldots,a_k. These k+1k+1 nodes are guaranteed to be all distinct.

For each query, you must find the length of the longest simple path starting at node x†x^\dagger after removing nodes a1,a2,…,aka_1,a_2,\ldots,a_k along with all edges connected to at least one of nodes a1,a2,…,aka_1,a_2,\ldots,a_k.

†^\dagger A simple path of length kk starting at node xx is a sequence of distinct nodes x=u0,u1,…,ukx=u_0,u_1,\ldots,u_k such that there exists a edge between nodes ui−1u_{i-1} and uiu_i for all 1≤i≤k1 \leq i \leq k.

不劳动者不得食。用你自己的力量去获取想要的东西。但请相信,认真而严肃的人终将笑到最后……即便如此,我也不会送你礼物。

——圣诞老人,《旋风管家》!

由于《旋风管家》中的飒太没有从圣诞老人那里收到任何圣诞礼物,他只好转而求解一道树上查询问题。

飒太有一棵包含 nn 个节点的树。

现在,飒太希望你回答 qq 个查询。每个查询由一个节点 xx 和另外 kk 个节点 a1,a2,…,aka_1,a_2,\ldots,a_k 构成。这 k+1k+1 个节点保证互不相同。

对于每个查询,你需要在移除节点 a1,a2,…,aka_1,a_2,\ldots,a_k 及所有至少与其中某个节点 a1,a2,…,aka_1,a_2,\ldots,a_k 相连的边之后,求出以节点 x†x^\dagger 为起点的最长简单路径的长度。

†^\dagger 一条长度为 kk、以节点 xx 为起点的简单路径,是指一个由互异节点构成的序列 x=u0,u1,…,ukx=u_0,u_1,\ldots,u_k,使得对所有 1≤i≤k1 \leq i \leq k,节点 ui−1u_{i-1} 与 uiu_i 之间均存在一条边。

输入格式

The first line contains two integers nn and qq (1≤n,q≤2⋅1051 \le n, q \le 2 \cdot 10^5) — the number of nodes of the tree and the number of queries.

The following n−1n - 1 lines contain two integers uu and vv (1≤u,v≤n1 \le u, v \le n, u≠vu \ne v) — denoting an edge between nodes uu and vv. It is guaranteed that the given edges form a tree.

The following qq lines describe the queries. Each line contains the integers xx, kk and a1,a2,…,aka_1,a_2,\ldots,a_k (1≤x≤n1 \leq x \leq n, 0≤k<n0 \leq k \lt n, 1≤ai≤n1 \leq a_i \leq n) — the starting node, the number of removed nodes and the removed nodes.

It is guaranteed that for each query, x,a1,a2,…,akx,a_1,a_2,\ldots,a_k are all distinct.

It is guaranteed that the sum of kk over all queries will not exceed 2⋅1052 \cdot 10^5.

第一行包含两个整数 nn 和 qq(1≤n,q≤2⋅1051 \le n, q \le 2 \cdot 10^5)—— 分别表示树的节点数和查询次数。

接下来的 n−1n - 1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n,u≠vu \ne v)—— 表示节点 uu 与节点 vv 之间存在一条边。保证所给边构成一棵树。

接下来的 qq 行描述各次查询。每行包含整数 xx、kk 以及 a1,a2,…,aka_1,a_2,\ldots,a_k(1≤x≤n1 \leq x \leq n,0≤k<n0 \leq k \lt n,1≤ai≤n1 \leq a_i \leq n)—— 分别表示起始节点、被移除的节点个数,以及被移除的节点列表。

保证在每次查询中,x,a1,a2,…,akx, a_1, a_2, \ldots, a_k 互不相同。

保证所有查询中 kk 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each query, output a single integer denoting the answer for that query.

对于每个查询,输出一个整数,表示该查询的答案。

输入输出样例

  • 输入#1

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

    输出#1

    3
    2
    1
    4
    1
    4
    1
  • 输入#2

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

    输出#2

    1
    2
    2
    0

说明/提示

In the first example, the tree is as follows:

In the first query, no nodes are missing. The longest simple path starting from node 22 is 2→1→3→42 \to 1 \to 3 \to 4. Thus, the answer is 33.

In the third query, nodes 11 and 66 are missing and the tree is shown below. The longest simple path starting from node 22 is 2→52 \to 5. Thus, the answer is 11.

在第一个例子中,树的结构如下:

在第一个查询中,没有任何节点缺失。从节点 22 出发的最长简单路径为 2→1→3→42 \to 1 \to 3 \to 4。因此,答案为 33。

在第三个查询中,节点 11 和 66 缺失,此时的树如下图所示。从节点 22 出发的最长简单路径为 2→52 \to 5。因此,答案为 11。

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

首页