아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Water Container System

메모리 제한1024 MB

요약
단위 용량 컨테이너로 이루어진 루트 트리에서 각 질의마다 한 컨테이너에 1리터를 붓고, 최종적으로 가득 찬 컨테이너 수를 센다.
난이도

보통10점 중 7점

유형
트리, BFS, 시뮬레이션
정답자
아직 제출이 없습니다

문제

There is a water container system with N\mathbf{N} identical containers, which can be represented as a tree, where each container is a vertex. The containers are connected to each other with N−1\mathbf{N}-1 bidirectional pipes. Two containers connected to each other are always placed on adjacent levels. Formally, if two containers aa and bb are connected to each other, then ∣level_a−level_b∣=1|level\_a - level\_b | = 1. Container 11 is placed at the bottommost level. Each container is connected to exactly one container on the level below (the only exception is container 11, which has no connections below it), but can be connected to zero or more containers on the level above. The maximum capacity of each container is 11 liter, and initially all the containers are empty. Assume that the pipe has a capacity of 00 liters. In other words, they do not store any water, but only allow water to pass through them in any direction.

Consider the following diagram which is an example of a water container system:

The first level of the system consists of a single container, container 11 (root). Container 11 is connected to container 22 and container 33, which are present in the above level, level 22. Container 22 is also connected to container 44, which is present at level 33.

You are given Q\mathbf{Q} queries. Each query contains a single integer ii which represents a container. For each query, add an additional 11 liter of water in container ii.

The following diagram illustrates the flow of the water in the system in different conditions:

In step 11, after adding 11 liter of water in container 33, the water flows downward because the water containers at the lower level are still empty.

In step 22, after adding 11 liter of water in container 33, the water flows downward, but as the container 11 is already filled completely, the water is distributed evenly between water containers 22 and 33.

In step 33, after adding 11 liter of water in container 33, the water containers 22 and 33 are completely filled.

In step 44, after adding 11 liter of water in container 33, the water is pushed up to water container 44, which is then completely filled.

As illustrated in the example above, containers at the same level will have the same amount of water. Find the number of water containers that are completely filled after processing all the queries.

입력

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow.

The first line of each test case contains the two integers N\mathbf{N} and Q\mathbf{Q}, where N\mathbf{N} is the number of containers and Q\mathbf{Q} is the number of queries.

The next N−1\mathbf{N}-1 lines contain two integers ii and jj (1≤i,j≤N1 \le i, j \le \mathbf{N}, and i≠ji ≠ j) meaning that the ii-th water container is connected to the jj-th water container.

Each of the next Q\mathbf{Q} lines contain a single integer ii (1≤i≤N1 \le i \le \mathbf{N}) that represents the container to which 11 liter of water should be added.

출력

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the number of water containers that are completely filled after processing all the queries.

제한

  • 1≤T≤1001 \le \mathbf{T} \le 100.
  • 1≤Q≤N1 \le \mathbf{Q} \le \mathbf{N}.
  • It is guaranteed that the given water container system is a tree.

예제2

  1. 예제 1

    입력
    2
    1 1
    1
    3 2
    1 2
    1 3
    1
    2
    
    예상 출력
    Case #1: 1
    Case #2: 1
    
  2. 예제 2

    입력
    2
    4 4
    1 2
    1 3
    2 4
    3
    3
    3
    3
    5 2
    1 2
    5 3
    3 1
    2 4
    4
    5
    
    예상 출력
    Case #1: 4
    Case #2: 1