시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
5 초 1024 MB 47 28 16 47.059%

문제

N개의 정점으로 이루어진 트리가 있다. 정점은 1번부터 N번까지 번호가 매겨져 있다. 간선의 가중치는 모두 1이다.

아래의 쿼리를 수행하는 프로그램을 작성하시오.

  • k v1 r1 v2 r2 ... vk rk: 어떠한 정점 x가 vi와 거리 ri 이내에 있다면 (거리가 ri보다 작거나 같다면), x가 i번 조건을 만족한다고 하자. 트리에 있는 모든 정점들 중, 쿼리로 주어진 k개 조건 중 k-1개 이상의 조건을 만족하는 정점의 개수를 출력하라.

입력

첫 번째 줄에 정수 N이 주어진다. (1 ≤ N ≤ 100,000)

이후 N-1개의 줄에는 각 간선이 연결하는 두 정점 번호 u, v가 주어진다. (1 ≤ u, v ≤ N)

다음 줄에 정수 M이 주어진다. (1 ≤ M ≤ 300,000)

이후 M개의 줄에 위에서 설명한 것과 같은 쿼리가 주어진다. 각 쿼리는 지문과 다르게 한 줄에 들어오지 않으며, k + 1개의 줄로 분리되어 주어진다. 예제 입력을 참고하라. (1 ≤ vi ≤ N, 0 ≤ ri < N, 1 ≤ k)

쿼리로 주어지는 k의 합은 300000을 넘지 않는다.

출력

쿼리의 결과를 한 줄에 하나씩 출력한다.

예제 입력 1

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

예제 출력 1

5
7
W3sicHJvYmxlbV9pZCI6IjE4MzcyIiwicHJvYmxlbV9sYW5nIjoiMCIsInRpdGxlIjoiXHVkMmI4XHViOWFjXHVjNjQwIFx1Y2ZmY1x1YjlhYyAxNCIsImRlc2NyaXB0aW9uIjoiPHA+Tlx1YWMxY1x1Yzc1OCBcdWM4MTVcdWM4MTBcdWM3M2NcdWI4NWMgXHVjNzc0XHViOGU4XHVjNWI0XHVjOWM0IFx1ZDJiOFx1YjlhY1x1YWMwMCBcdWM3ODhcdWIyZTQuIFx1YzgxNVx1YzgxMFx1Yzc0MCAx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