1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include<bits/stdc++.h> typedef int int32; #define int long long using namespace std; const int N = 2e5 + 5; int n, a[N], b[N], c, d, sz[N], cnt[N], tmp[N], tot, ctr, ans, maxi; bitset<N>del, vis; vector<int>nbr[N], e[N]; void get_ctr(int x, int fa) { sz[x] = 1; int maxi = 0; for (auto& nxt : nbr[x]) if (!del[nxt] && nxt != fa) { get_ctr(nxt, x); if (ctr) return; maxi = max(maxi, sz[nxt]); sz[x] += sz[nxt]; } maxi = max(maxi, n - sz[x]); if (maxi <= n / 2) { ctr = x; sz[fa] = n - sz[x]; } return; } void dfs(int x, int fa, int len, int len1) { if (len1 == 1) return; ans = max(ans, maxi + len - 1); tmp[++tot] = len; for (auto& nxt : nbr[x]) if (!del[nxt] && nxt != fa) dfs(nxt, x, len + 1, __gcd(len1, a[nxt])); return; } void work(int x) { for (auto& j : e[a[x]]) if (!(a[x] % b[j])) { maxi = 1; for (auto& nxt : nbr[x]) if (!del[nxt] && __gcd(__gcd(a[x], a[nxt]), b[j]) == b[j]) { int tmp1 = tot; dfs(nxt, x, 2, b[j]); for (int i = tmp1 + 1; i <= tot; i++) maxi = max(maxi, tmp[i]); } tot = 0; } del[x] = true; for (auto& nxt : nbr[x]) if (!del[nxt]) { n = sz[nxt]; ctr = 0; get_ctr(nxt, 0); work(ctr); } return; } signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); for (int i = 2; i <= 2e5; i++) { if (!vis[i]) { b[++c] = i; for (int j = i; j <= 2e5; j += i) e[j].push_back(c); } for (int j = 1; j <= c; j++) { if (i * b[j] > 2e5) break; vis[i * b[j]] = true; if (!(i % b[j])) break; } } cin >> n; for (int i = 1; i <= n; i++) cin >> a[i], ans = max<int>(ans, a[i] > 1); for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; nbr[x].push_back(y); nbr[y].push_back(x); } get_ctr(1, 0); work(ctr); cout << ans; return 0; }
|