Tax Collection

4

5 votes
Algorithms, Graphs, Dynamic Programming, Trees, Number theory, Depth First Search
Problem

You are a CEO of a group of companies (N companies form a rooted tree structure), and the country you live in has a weird tax system. Suppose if a company makes X rs profit then it has to pay Y rs tax, here the Y is the number of prime factors of X.Calculate the total tax paid by all the companies in the group. Company 1 is the parent of all companies.

Tax paid by a company is equal to the tax paid by its direct children and the tax paid on the profits earned by that company.

Input : 

The first line contains T, the number of test cases (1T103)
The first line of each test case contains N, the number of companies (1N105)
The second line of each test case contains an array profit a1,a2,a3,....an (1ai106)
The next (N1) lines contain two numbers u,v. There is an edge between u and v
The structure is guaranteed to be a tree.

It is guaranteed that the sum of N over all test cases doesn't exceed 105.

Output:

Print an array of size N ,where Ai is the amount of tax paid by the company i

Time Limit: 3
Memory Limit: 512
Source Limit:
Explanation

In the first test case,Company 1 is the parent of company 2 and company 3. So the total prime factors of 10,12 and 16 are 2,2 and 1 respectively

So the tax paid by company 2 will be 2Rs and the tax paid by company 3 will be 1Rs.

Now company 1 will pay 2Rs as its own tax and 3Rs is already paid by its children companies

Editor Image

?