CF1904E.Tree Queries
普及/提高-
通过率:0%
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 n nodes.
Hayate now wants you to answer q queries. Each query consists of a node x and k other additional nodes a1,a2,…,ak . These k+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† after removing nodes a1,a2,…,ak along with all edges connected to at least one of nodes a1,a2,…,ak .
† A simple path of length k starting at node x is a sequence of distinct nodes x=u0,u1,…,uk such that there exists a edge between nodes ui−1 and ui for all 1≤i≤k .
输入格式
The first line contains two integers n and q ( 1≤n,q≤2⋅105 ) — the number of nodes of the tree and the number of queries.
The following n−1 lines contain two integers u and v ( 1≤u,v≤n , u=v ) — denoting an edge between nodes u and v . It is guaranteed that the given edges form a tree.
The following q lines describe the queries. Each line contains the integers x , k and a1,a2,…,ak ( 1≤x≤n , 0≤k<n , 1≤ai≤n ) — the starting node, the number of removed nodes and the removed nodes.
It is guaranteed that for each query, x,a1,a2,…,ak are all distinct.
It is guaranteed that the sum of k over all queries will not exceed 2⋅105 .
输出格式
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 2 is 2→1→3→4 . Thus, the answer is 3 .
In the third query, nodes 1 and 6 are missing and the tree is shown below. The longest simple path starting from node 2 is 2→5 . Thus, the answer is 1 .