GCD on Tree
Practice
5 (4 votes)
Regular expression
Hard
Algorithms
String
Trees
Problem
53% Success 977 Attempts 50 Points 3s Time Limit 256MB Memory 1024 KB Max Code

Given a tree with \(N\) vertices. Each vertex has a value \(a_i\). For vertex \(i\) and \(j\), if some vextex \(x\) is on the path between \(i\) and \(j\), and \(x\) is neither \(i\) nor \(j\), we update \(Ans_x=max(Ans_x,GCD(a_i,a_j))\)\(GCD\) means the greatest common divisor function. \(Ans\) is an array of \(N\) zeros initial.

Find the \(Ans\) array after all updation.

Input Format

First line contains an integer \(N(1\le N\le 10^5)\).

Second line contains \(N\) integers, represent the array \(a(1\le a_i\le 10^5)\).

Each of the next \(N-1\) lines contains two integers \(u,v\), represent an edge in the tree.

Output Format

\(N\) integers in one line, represent the array \(Ans\), separated by space.

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:50
3 votes
Tags:
Lowest Common AncestorTreesData Structures
Points:50
8 votes
Tags:
Data StructuresDynamic ProgrammingTreesLowest Common Ancestor
Points:20
38 votes
Tags:
AlgorithmsApprovedEasyOpenSorting