QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#261298 | #4584. Not One | NYCU_gAwr_gurA# | RE | 1ms | 4116kb | C++17 | 1.7kb | 2023-11-22 20:01:58 | 2023-11-22 20:02:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define fast
#else
#define fast cin.tie(0)->sync_with_stdio(0)
#define endl '\n'
#define cerr if(1);else cerr
#endif
#define _ <<' '<<
#define ALL(v) v.begin(),v.end()
#define ft first
#define sd second
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
constexpr int MAXN = 1e5+5;
int fac[MAXN];
vector<int> prime{};
void build() {
fac[0] = fac[1] = 1;
for (int i = 2; i < MAXN; i++) {
if (!fac[i]) {
prime.emplace_back(i);
fac[i] = i;
}
for (int p: prime) {
if (i*p >= MAXN) break;
fac[i*p] = p;
if (i%p == 0) break;
}
}
}
struct DSU {
int mx;
vector<int> ary;
DSU(int n): mx(1), ary(n, -1) {}
int find(int x) { return ary[x] < 0 ? x : ary[x] = find(ary[x]); }
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
if (-ary[u] < -ary[v])
swap(u, v);
ary[u] += ary[v];
ary[v] = u;
if (mx < -ary[u])
mx = -ary[u];
}
};
signed main() {
fast;
build();
int n;
cin >> n;
vector<int> A(n);
for (auto &x: A) cin >> x;
vector<pii> edge(n-1);
for (auto &[u,v]: edge) cin >> u >> v, u--, v--;
map<int,vector<pii>> mp{};
for (auto [u,v]: edge) {
int g = __gcd(A[u], A[v]);
vector<int> ps{};
for (int x = g; x > 1; ) {
int p = fac[x];
ps.emplace_back(p);
while (x % p == 0)
x /= p;
}
for (auto p: ps)
mp[p].emplace_back(u, v);
}
int ans = 0;
DSU dsu(n);
for (auto &[p,E]: mp) {
for (auto [u,v]: E)
dsu.merge(u, v);
ans = max(ans, dsu.mx);
dsu.mx = 1;
for (auto [u,v]: E)
dsu.ary[u] = dsu.ary[v] = -1;
}
cout << ans << endl;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3992kb
input:
7 10 5 8 6 10 6 4 1 2 2 3 2 4 4 5 4 6 4 7
output:
4
result:
ok single line: '4'
Test #2:
score: 0
Accepted
time: 1ms
memory: 4060kb
input:
4 1 1 1 1 1 2 2 3 3 4
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 0ms
memory: 4036kb
input:
5 100 100 100 100 100 3 4 1 2 3 5 2 4
output:
5
result:
ok single line: '5'
Test #4:
score: 0
Accepted
time: 1ms
memory: 4116kb
input:
2 1 1 1 2
output:
0
result:
ok single line: '0'
Test #5:
score: -100
Runtime Error
input:
100000 860163 795323 862289 543383 792647 337047 353985 959953 874318 573652 69827 958063 571741 704399 311826 920477 792478 151531 872269 592307 853819 865817 940735 620657 937154 696551 749279 552523 836161 707467 389626 459089 563763 668884 810391 639709 419361 580342 519595 836124 494959 669379 ...