QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#305430#4584. Not OneLeeShoW#TL 1ms7000kbC++201.4kb2024-01-15 11:49:192024-01-15 11:49:19

Judging History

你现在查看的是最新测评结果

  • [2024-01-15 11:49:19]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:7000kb
  • [2024-01-15 11:49:19]
  • 提交

answer

#include<iostream>
#include<algorithm>
#include<random>
#include<vector>
#include<chrono>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#define pii pair<int,int>
using namespace std;
int all[100005];
int n;
vector<int>v[100005];
vector<int>pr;
vector<pii>edge;
int mx=-1;
int ans=1;
bool is[1000005];
void init(){
    int now=mx;
    memset(is,0,sizeof(is));
    for(int i=2;i<=now;i++){
        if(is[i]==0)pr.push_back(i);
        for(int j=i+i;j<=now;j+=i){
            is[j]=1;
        }
    }
}
int f[100005];
int sz[100005];
int find(int x){
    if(x==f[x])return x;
    else return f[x]=find(f[x]);
}
void init1(){
    for(int i=1;i<=n;i++){
        f[i]=i;
        sz[i]=1;
    }
}
signed main(){
    cin>>n;
    int one=0;
    for(int i=1;i<=n;i++){
        cin>>all[i];
        mx=max(mx,all[i]);
        if(all[i]==1)one++;
    }
    init();
    for(int i=0;i<n-1;i++){
        int x,y;
        cin>>x>>y;
        edge.push_back({x,y});
    }
    if(one==n){
        cout<<0<<endl;
        return 0;
    }

    for(auto i:pr){
        init1();
        for(auto [u,v]:edge){
            if(all[u]%i==0&&all[v]%i==0){
                int xx=find(u);
                int yy=find(v);
                f[xx]=yy;
                sz[yy]+=sz[xx];
                ans=max(sz[yy],ans);
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 6944kb

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: 6880kb

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: 1ms
memory: 6948kb

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: 7000kb

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 ...

output:


result: