#P15608. [ICPC 2021 Jakarta R] Not One

[ICPC 2021 Jakarta R] Not One

题目描述

The greatest common divisor (GCD) of a set of positive integers SS is defined as the largest positive integer dd such that dd is a divisor for all elements in SS, e.g., GCD(10)=10\text{GCD}(10) = 10, GCD(6,9)=3\text{GCD}(6, 9) = 3, GCD(20,12,16,36)=4\text{GCD}(20, 12, 16, 36) = 4.

In this problem, you are given a tree of NN nodes where each node is numbered from 11 to NN and has a positive integer AiA_i 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 11, or output 00 if there is no such a subtree. A tree TT' is a subtree of TT if and only if TT' is connected and is a subset of TT. The size of a subtree is the number of nodes in that subtree.

For example, consider the following tree of N=7N = 7 nodes where A1..7={10,5,8,6,10,6,4}A_{1..7} = \{10, 5, 8, 6, 10, 6, 4\}.

:::align{center} :::

In this example, there are 1515 subtrees where the GCD of all its nodes' weight is not 11, i.e. seven subtrees of size 11, four subtrees of size 22, three subtrees of size 33, and one subtree of size 44 (the largest). The largest subtree contains nodes 44, 55, 66, and 77, 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 NN (2N1000002 \leq N \leq 100\,000) representing the number of nodes in the given tree. The next line contains NN integers AiA_i (1Ai1061 \leq A_i \leq 10^6) representing the weight of the ithi^{th} node. The next N1N - 1 line each contains two integers uju_j vjv_j (1uj<vjN1 \leq u_j < v_j \leq N) representing an edge connecting node uju_j and node vjv_j. 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 11. If there is no such a subtree, output 00 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 11 in this case.