QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#65320 | #4584. Not One | T3alaadl3k2olyehymn3k# | TL | 0ms | 0kb | C++17 | 1.7kb | 2022-11-29 20:21:20 | 2022-11-29 20:21:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 100005, P = 19;
const int M = 1e6 + 5;
int a[N];
struct Edge {
int u, v, g;
Edge(int u, int v, int g): u(u), v(v),g(g) {}
Edge(){}
};
vector<Edge > edges;
vector< int > primes[M];
void seive(){
for(int i = 2; i < M; ++i){
for(int j = i; j < M; j += i)
primes[j].push_back(i);
}
}
bool vis[N];
struct {
int par[N], rnk[N];
void init(int n){
for(int i = 1; i <= n; ++i)
par[i] = i, rnk[i] = 1;
}
int findParent(int u){
return par[u] = (par[u] == u ? u : findParent(par[u]));
}
void merge(int u, int v){
u = findParent(u);
v = findParent(v);
if(u == v)return ;
if(rnk[u] < rnk[v])
swap(u,v);
par[v] = u;
rnk[u] += rnk[v];
}
}dsu;
signed main() {
int n;
cin >> n;
seive();
for(int i = 1; i <= n; ++i)
cin >> a[i];
int u , v;
set<int> st;
for(int i = 0; i < n - 1; ++i){
cin >> u >> v;
int gc = __gcd(a[u],a[v]);
if(gc == 1)continue;
edges.emplace_back(u,v, gc);
if(!vis[gc]){
for(auto &p: primes[gc])
st.insert(p);
vis[gc] = 1;
}
}
int ans = 0;
for(auto &gc: st){
dsu.init(n);
for(int i = 0; i < edges.size(); ++i){
if(edges[i].g % gc == 0){
dsu.merge(edges[i].u, edges[i].v);
}
}
for(int i = 1; i <= n; ++i){
ans = max(ans, dsu.rnk[dsu.findParent(i)]);
}
}
cout << ans << '\n';
return 0;
}
詳細信息
Test #1:
score: 0
Time Limit Exceeded
input:
7 10 5 8 6 10 6 4 1 2 2 3 2 4 4 5 4 6 4 7