QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#258691#4584. Not Onejiayi_koWA 108ms34688kbC++142.0kb2023-11-20 00:12:522023-11-20 00:12:53

Judging History

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

  • [2023-11-20 00:12:53]
  • 评测
  • 测评结果:WA
  • 用时:108ms
  • 内存:34688kb
  • [2023-11-20 00:12:52]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
vector<int> a(100005);
vector<int> v[100005], pdiv[1000005];
int k, m;
vector<int> prime;
vector<bool> isprime(1e3+5, true);
vector<bool> vis(1e6+5, true);
int x, res, ans=0;
void dfs(int now){
    res++;
    vis[now]=true;
    for(auto i: v[now]){
        // cout<<"tetangga "<<now<<" : "<<i<<" dan "<<x<<"\n";
        if(vis[i]) continue;
        dfs(i);
    }
}
void sieve(){
    isprime[1]=false;
    prime.push_back(2);
    prime.push_back(3);
    for(int i=4; i<=1e3; i+=2) isprime[i]=false;
    for(int i=9; i<=1e3; i+=6) isprime[i]=false;
    for(ll i=5; i<=1e3; i+=2){
        if(isprime[i]){
            prime.push_back(i);
            for(ll j=i*i; j<=1e3; j+=i*2){
                isprime[j]=false;
            }
        }
    }
}
int main(){
    sieve();
    int n;
    cin>>n;
    for(int i=1; i<=n; i++){
        cin>>a[i];
    }
    for(int i=0; i<n-1; i++){
        cin>>k>>m;
        v[k].push_back(m);
        v[m].push_back(k);
    }
    for(int i=1; i<=n; i++){
        for(int j=0; j<prime.size(); j++){
            if(a[i]%prime[j] == 0){
                pdiv[prime[j]].push_back(i);
            }
            while(a[i]%prime[j] == 0) a[i]/=prime[j];
        }
        if(a[i] != 1){
            pdiv[a[i]].push_back(i);
        }
    }
    for(int i=2; i<1e6; i+=2){
        if(pdiv[i].size() == 0) continue;
        // cout<<"now "<<i<<"\n";
        for(int j=0; j<pdiv[i].size(); j++){
            // cout<<pdiv[i][j]<<" ";
            vis[pdiv[i][j]]=false;
        }
        // cout<<"\n";
        for(int j=0; j<pdiv[i].size(); j++){
            if(!vis[pdiv[i][j]]){
                res=0;
                dfs(pdiv[i][j]);
                ans=max(ans, res);
            }
        }
        for(int j=0; j<pdiv[i].size(); j++){
            vis[pdiv[i][j]]=true;
        }
        if(i == 2) i--;
    }
    cout<<ans<<"\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 29312kb

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

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: 10ms
memory: 29328kb

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: 5ms
memory: 29264kb

input:

2
1 1
1 2

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 108ms
memory: 34688kb

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:

213

result:

ok single line: '213'

Test #6:

score: -100
Wrong Answer
time: 94ms
memory: 33268kb

input:

100000
999983 999983 999961 999961 999979 999979 999979 999961 999983 999961 999979 999961 999961 999983 999961 999983 999983 999979 999961 999979 999983 999979 999983 999961 999979 999961 999979 999979 999961 999979 999983 999979 999961 999961 999961 999961 999961 999983 999979 999983 999979 999961...

output:

0

result:

wrong answer 1st lines differ - expected: '70', found: '0'