Tax Collection
Practice
4 (5 votes)
Algorithms
Graphs
Dynamic programming
Trees
Number theory
Depth first search
Problem
68% Success 567 Attempts 30 Points 3s Time Limit 512MB Memory 1024 KB Max Code

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 (\(1 \leq T \leq 10^3\))
The first line of each test case contains \(N\), the number of companies (\(1 \leq N \leq 10^5\))
The second line of each test case contains an array profit \(a_1,a_2,a_3,....a_n\) (\(1 \leq a_i \leq 10^6\))
The next \((N-1)\) 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 \(10^5\).

Output:

Print an array of size \(N\) ,where \(A_i\) is the amount of tax paid by the company \(i\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
3 votes
Tags:
AlgorithmsDepth First SearchGraphsC++
Points:30
8 votes
Tags:
AlgorithmsCombinatoricsDepth First SearchGraphsMedium
Points:30
4 votes
Tags:
Depth First SearchAlgorithmsGraphs