const LL mod = 1000000007;
const int maxn = 200005;
int n,m,k;
int a[maxn],dp[maxn];
int vis[maxn],vv[maxn];
vector<int > vc[maxn],vs[maxn];
int ans;
void bfs(int u){
vv[u] = 1;
int z = a[u];
int len = vc[z].size();
for(int i = 0;i<len;i++){
int v = vc[z][i];
int le = vs[v].size();
for(int j = 0;j<le;j++){
int b = vs[v][j];
if(vv[b] == 0){
ans++;
bfs(b);
}
}
}
}
int main()
{
cin>>n;
for(int i = 0;i<n;i++){
int x;
scanf("%d",&a[i]);
x = a[i];
if(vis[x] == 1) {
int le = vc[x].size();
for(int j=0;j<le;j++){
int v = vc[x][j];
vs[v].push_back(i);
}
continue;
}
vis[x] = 1;
int y = x;
for(int j =2;j*j<=y;j++){
if(y%j==0){
vc[x].push_back(j);
while(y%j==0){
y/=j;
}
}
}
if(y!=1) vc[x].push_back(y);
int len = vc[x].size();
for(int j=0;j<len;j++){
int v = vc[x][j];
vs[v].push_back(i);
}
}
int ll = vs[2].size();
int maxx = 0;
for(int i = 0;i<n;i++){
if(vv[i] == 0){
ans = 1;
bfs(i);
maxx = max(ans,maxx);
}
}
cout<<maxx<<endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0)return a;
else return gcd(b, a%b);
}
int flag[1000];
int getFather(int x) {
if (flag[x] == -1)return x;
else {
int tmp = getFather(flag[x]);
flag[x] = tmp;
return tmp;
}
}
int main() {
int n;
cin >> n;
vector<int>sugar(n);
vector<int>nums(n);
for (int i = 0; i < n; i++) {
cin >> sugar[i];
flag[i] = -1;
nums[i] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (gcd(sugar[i], sugar[j]) > 1) {
int a = getFather(i);
int b = getFather(j);
if (a != b) {
flag[a] = b;
nums[b] += nums[a];
}
}
}
}
int res = 0;
for (int i = 0; i < n; i++)
res = max(res, nums[i]);
cout << res << endl;
return 0;
}
我用并查集做的,也是70%