Ald

시간 제한4초메모리 제한2048 MB

요약
트리 위 경로들의 중복 집합을 삽입과 삭제로 관리하면서, 각 질의 d마다 저장된 모든 경로에서 거리 d 이내에 있는 정점의 개수를 구한다.
난이도

어려움10점 중 9점

유형
트리, 동적 계획법, 그리디, 분할 정복
정답자
아직 제출이 없습니다

문제

You are given a tree. The tree has nn vertices, labeled from 11 to nn.

Let us denote the path between vertices aa and bb as (a,b)(a, b). Let the dd-set of a path be the set of vertices on the tree located within a distance ≤d\le d from at least one vertex of the path. For example, the 00-set of a path is the set of its vertices. The distance between vertices is the number of edges on the path between these vertices.

Let SS be a multiset of tree paths. Initially, SS is empty. Your task is to process the following queries:

  • "1 uu vv": add path (u,v)(u, v) into SS (1≤u,v≤n1 \le u, v \le n).
  • "2 uu vv": delete a single path (u,v)(u, v) from SS (1≤u,v≤n1 \le u, v \le n). Note that (u,v)(u, v) and (v,u)(v, u) denote the same path. For example, if S=(2,3),(2,3)S=\\{(2, 3), (2, 3)\\}, then after a query "2 3 2", we will have S=(2,3)S=\\{(2, 3)\\}. Before this query, it is guaranteed that at least one path (u,v)(u, v) or (v,u)(v, u) is present in SS.
  • "3 dd": print the size of intersection of dd-sets of all paths from SS (0≤d≤n0 \le d \le n). If SS is empty, print 00.

입력

The first line contains an integer tt, the number of test cases (1≤t≤1041 \le t \le 10^4). The test cases follow.

The first line of each test case contains two integers nn and qq (1≤n,q≤1051 \le n, q \le 10^5), the number of vertices in the tree and the number of queries.

Each of the following n−1n - 1 lines contains two integers u_iu\_i and v_iv\_i: indices of vertices connected by the ii-th edge of the tree (1≤u_i,v_i≤n1 \le u\_i, v\_i \le n).

The following qq lines contain queries in the format described in the statement.

The sum of nn over all test cases does not exceed 10510^5. The sum of qq over all test cases does not exceed 10510^5.

출력

For each query of the third type, output a single line with the answer.

예제1

  1. 예제 1

    입력
    1
    8 7
    1 2
    1 3
    3 4
    2 5
    4 6
    1 7
    6 8
    3 1
    1 7 8
    3 1
    2 7 8
    1 8 6
    1 7 7
    3 3
    
    예상 출력
    0
    7
    3