Dhaka Regional ACM ICPC 2016 Preleminary Contest Solution D - Double Trouble

D. Double Trouble

Score: 1

CPU: 1s
Memory: 1200MB

Camden is an evil town with N zones and (N - 1) tunnels in between the zones. The newly elected mayor of Camden ensured that every zone is reachable from all the other zones through these tunnels.
You may wonder why it is being called ‘evil’. Well, people say that every zone of Camden offers some level of trouble. Depending on this level, each zone is categorized to be either Minder (less trouble) or Meer (more trouble).
And now say hello to Mr. Milo, who will be moving to Camden very soon, thanks to his new job! He was wiki-ing about Camden while he freaked out as he learned about the extreme risk of getting into trouble in the town he is relocating to. Fortunately, his office is going to be in a Minder zone, and before going to sleep, he decided that by all means he is going to live in a Minder zone as well.
But that night he had a nightmare that even though he was living in a Minder zone, on his direct way to the office from his home, he had to face, not one, but two different Meer zones!
He woke up terrified and tried to distract himself quickly, so instead of freaking out anymore, he began to think in how many ways, he can have a situation where he is living in a Minder zone which has exactly two different Meer zones on the way to the office from itself. But he is too nervous to solve it right now, can you help him?
N.B: Out of extreme anxiety after waking up, Mr. Milo forgot which Minder zone his office is situated in. So, for the problem he made up, he is considering all possible Minder zones as his possible office location.

Input

Input file contains an integer in the first line, number of test cases T (1 ≤ T ≤ 15), and T cases follow. Each case begins with an integerN(1 ≤ N ≤ 100000). The next line contains N space separated integers. The i’th integer is 0 if zone-i is Minder, and the i’th integer is 1 if zone-i is Meer. Then N-1 lines follow, each line have two integers x, y, which denotes there is a tunnel between zone-x and zone-y.

Output

The output file contains case number, followed by the number of ways you can pick two Minder zones so that there are exactly 2 Meer zones on the direct way from one to the other.

Sample

InputOutput
1 5 0 1 1 0 0 1 2 2 3 3 4 4 5 Case 1: 4

Explanation of Sample Test Case:
There can be four possible scenarios for zone selection as (home, office): (1, 4), (1, 5), (4, 1), (5, 1).























Comments

Popular posts from this blog

Solution - Timus Problem 1293. Eniya

Solution - Codeforces Problem 327B - Hungry Sequence

Solution - Timus Problem 1409. Two Gangsters