#P15608. [ICPC 2021 Jakarta R] Not One
[ICPC 2021 Jakarta R] Not One
题目描述
The greatest common divisor (GCD) of a set of positive integers is defined as the largest positive integer such that is a divisor for all elements in , e.g., , , .
In this problem, you are given a tree of nodes where each node is numbered from to and has a positive integer assigned to it. Your task is to find the size of the largest subtree such that the GCD of the weight of all nodes in that subtree is not , or output if there is no such a subtree. A tree is a subtree of if and only if is connected and is a subset of . The size of a subtree is the number of nodes in that subtree.
For example, consider the following tree of nodes where .
:::align{center}
:::
In this example, there are subtrees where the GCD of all its nodes' weight is not , i.e. seven subtrees of size , four subtrees of size , three subtrees of size , and one subtree of size (the largest). The largest subtree contains nodes , , , and , and the GCD of their weights is $\text{GCD}(A_4, A_5, A_6, A_7) = \text{GCD}(6, 10, 6, 4) = 2$.
输入格式
Input begins with a line containing an integer () representing the number of nodes in the given tree. The next line contains integers () representing the weight of the node. The next line each contains two integers () representing an edge connecting node and node . It is guaranteed that the given tree is connected.
输出格式
Output contains an integer in a line representing the size of the largest subtree such that the GCD of all its nodes' weight is not . If there is no such a subtree, output in a line.
7
10 5 8 6 10 6 4
1 2
2 3
2 4
4 5
4 6
4 7
4
4
1 1 1 1
1 2
2 3
3 4
0
5
100 100 100 100 100
3 4
1 2
3 5
2 4
5
提示
Explanation for the sample input/output #2
There is no subtree where the GCD of all its nodes' weight is not in this case.