QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#321591 | #4584. Not One | ssuyu | TL | 0ms | 31112kb | C++17 | 2.1kb | 2024-02-04 23:37:06 | 2024-02-04 23:37:06 |
Judging History
answer
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ssuyu \
ios_base::sync_with_stdio(0); \
cin.tie(0)
#define pb push_back
#define F first
#define S second
#define pii pair<int, int>
#define int long long
#define PI acos(-1)
#define all(x) (x).begin(), (x).end()
const int N = 1e6 + 5;
int a[N];
int prime[N] = {0}, f[N] = {0};
vector<int> primes;
int Find(int a) {
if (f[a] <= 0) {
return a;
} else
return f[a] = Find(f[a]);
}
int Union(int a, int b) {
int fa = Find(a);
int fb = Find(b);
if (fa == fb)
return -f[fa];
if (f[fa] > f[fb])
swap(fa, fb);
f[fa] += f[fb];
f[fb] = fa;
return -f[fa];
}
void getprimes(int n) {
for (int i = 2; i <= n; i++) {
if (!prime[i]) {
primes.pb(i);
}
for (auto u : primes) {
if (u * i > n)
break;
prime[u * i] = 1;
if (!(i % u))
break;
}
}
}
struct edge {
int u;
int v;
int w = 1;
} edge[N];
signed main() {
int n, m, x, y;
int mx = 0, ans = 0;
vector<int> affect;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mx = max(mx, a[i]);
}
getprimes(mx);
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
edge[i].v = x;
edge[i].u = y;
f[edge[i].v] = -1;
f[edge[i].u] = -1;
edge[i].w = __gcd(a[x], a[y]);
}
for (auto p : primes) {
for (int i = 0; i < n - 1; i++) {
if (!(edge[i].w % p)) {
edge[i].w /= p;
ans = max(Union(edge[i].u, edge[i].v), ans);
affect.pb(edge[i].u);
affect.pb(edge[i].v);
}
}
for (auto u : affect)
f[u] = -1;
affect.clear();
}
cout << ans;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 30624kb
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: 0ms
memory: 29640kb
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: 31112kb
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: 0ms
memory: 30008kb
input:
2 1 1 1 2
output:
0
result:
ok single line: '0'
Test #5:
score: -100
Time Limit Exceeded
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 ...