GCD on Tree

5

4 votes
Regular expression, Hard, Algorithms, String, Trees
Problem

Given a tree with vertices. Each vertex has a value . For vertex  and , if some vextex  is on the path between  and , and  is neither  nor , we update  means the greatest common divisor function.  is an array of  zeros initial.

Find the  array after all updation.

Input Format

First line contains an integer .

Second line contains  integers, represent the array .

Each of the next  lines contains two integers , represent an edge in the tree.

Output Format

 integers in one line, represent the array , separated by space.

Sample Input
7
2 1 6 1 6 1 3
1 2
2 3
3 4
4 5
3 6
6 7
Sample Output
0 2 3 6 0 3 0
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

Vertex  is updated by  and ,

Vertex  is updated by  and ,

Vertex  is updated by  and ,

Editor Image

?