[Solution] Minimize Blocked Roads CodeChef Solution | CodeChef Problem Solution 2022
Given a network of cities (numbered from to ) and roads arranged in a tree format with the root at city .
Each of the roads is assigned a value of either or .
- If the value of a road is , it cannot be blocked by the government.
- If the value of a road is , it may or may not be blocked depending on the decision of the government.
Initially, city numbered is infected by a virus. The infection spreads from an infected to an uninfected city only if all the roads present on the shortest path between the cities are unblocked.
The government wants to control the number of infected cities and has asked you to devise a plan. Determine the minimum number of roads (with value ) you need to block such that at most cities are infected at the end.
If it is not possible that at most cities are infected, print .
Input Format
- The first line contains an integer denoting the number of test cases. The test cases then follow.
- The first line of each test case contains two space-separated integers and , as given in the statement.
- Each of the next lines consists of space-separated integers , , and denoting that there exists a road between cities and with a value or .
Output Format
For each test case, output a single integer representing the minimum number of roads that need to be blocked such that at most cities are infected at the end.
If it is not possible that at most cities are infected, print .
Constraints
- and .
- Sum of over all cases does not exceed .
No comments:
Post a Comment