[Solution] Water Container System Round F 2022 - Kick Start 2022 Solution
Problem
There is a water container system with identical containers, which can be represented as a tree, where each container is a vertex. The containers are connected to each other with bidirectional pipes. Two containers connected to each other are always placed on adjacent levels. Formally, if two containers and are connected to each other, then . Container is placed at the bottommost level. Each container is connected to exactly one container on the level below (the only exception is container , 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 liter, and initially all the containers are empty. Assume that the pipe has a capacity of 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 (root). Container is connected to container and container , which are present in the above level, level . Container is also connected to container , which is present at level .
You are given queries. Each query contains a single integer which represents a container. For each query, add an additional liter of water in container .
The following diagram illustrates the flow of the water in the system in different conditions:
In step , after adding liter of water in container , the water flows downward because the water containers at the lower level are still empty.
In step , after adding liter of water in container , the water flows downward, but as the container is already filled completely, the water is distributed evenly between water containers and .
In step , after adding liter of water in container , the water containers and are completely filled.
In step , after adding liter of water in container , the water is pushed up to water container , 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.
Input
The first line of the input gives the number of test cases, . test cases follow.
The first line of each test case contains the two integers and , where is the number of containers and is the number of queries.
The next lines contain two integers and (, and ) meaning that the -th water container is connected to the -th water container.
Each of the next lines contain a single integer () that represents the container to which liter of water should be added.
Output
For each test case, output one line containing Case #:
, where is the test case number (starting from 1) and is the number of water containers that are completely filled after processing all the queries.
Limits
Memory limit: 1 GB.
.
.
It is guaranteed that the given water container system is a tree.
Test Set 1
Time limit: 20 seconds.
.
The water container system is a perfect binary tree.
Test Set 2
Time limit: 60 seconds.
.
No comments:
Post a Comment