0% found this document useful (0 votes)
113 views2 pages

Tree Path GCD Challenge

The document describes a problem involving finding the maximum distance between pairs of vertices in a tree where the greatest common divisor (GCD) of the numbers written on the vertices in the path between them is greater than 1. It provides details on the input format which includes the number of vertices, numbers written on each vertex, and the edges of the tree. It also describes the output, which is the maximum distance between such vertex pairs if one exists, or 0 if no pairs have a GCD greater than 1. Examples of input and output are given.

Uploaded by

Samin Afnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views2 pages

Tree Path GCD Challenge

The document describes a problem involving finding the maximum distance between pairs of vertices in a tree where the greatest common divisor (GCD) of the numbers written on the vertices in the path between them is greater than 1. It provides details on the input format which includes the number of vertices, numbers written on each vertex, and the edges of the tree. It also describes the output, which is the maximum distance between such vertex pairs if one exists, or 0 if no pairs have a GCD greater than 1. Examples of input and output are given.

Uploaded by

Samin Afnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Input

The first line contains one integer nn — the number of vertices (1≤n≤2⋅105)(1≤n≤2⋅105).
The second line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤2⋅105)(1≤ai≤2⋅105) — the numbers
written on vertices.

e given a tree consisting of nn vertices. A number is written on each vertex; the number on
vertex ii is equal to aiai.

Let's denote the function g(x,y)g(x,y) as the greatest common divisor of the numbers written on the
vertices belonging to the simple path from vertex xx to vertex yy (including these two vertices). Also
let's denote dist(x,y)dist(x,y) as the number of vertices on the simple path between
vertices xx and yy, including the endpoints. dist(x,x)=1dist(x,x)=1 for every vertex xx.
Your task is calculate the maximum value of dist(x,y)dist(x,y) among such pairs of vertices
that g(x,y)>1g(x,y)>1.

ty. GCD Counting


time limit per test
4.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Input
The first line contains one integer nn — the number of vertices (1≤n≤2⋅105)(1≤n≤2⋅105).
The second line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤2⋅105)(1≤ai≤2⋅105) — the numbers
written on vertices.
Then n−1n−1 lines follow, each containing two
integers xx and yy (1≤x,y≤n,x≠y)(1≤x,y≤n,x≠y) denoting an edge connecting vertex xx with
vertex yy. It is guaranteed that these edges form a tree.
Output
If there is no pair of vertices x,yx,y such that g(x,y)>1g(x,y)>1, print 00. Otherwise print the
maximum value of dist(x,y)dist(x,y) among such pairs.

Examples
input
Copy
3
2 3 4
1 2
2 3
output
Copy
1

input
Copy
3
2 3 4
1 3
2 3
output
Copy
2

input
Copy
3
1 1 1
1 2
2 3
output
Copy
0

You might also like