QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#321781#4584. Not Oneowen0806#TL 20ms103400kbC++201.8kb2024-02-05 15:38:122024-02-05 15:38:12

Judging History

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

  • [2024-02-05 15:38:12]
  • 评测
  • 测评结果:TL
  • 用时:20ms
  • 内存:103400kb
  • [2024-02-05 15:38:12]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define PB push_back
#define pii pair<int, int>
using namespace std;
const int N = 2e6 + 5;
const int INF = 1e18;
vector<int> prime;
bool isprime[N];
void init(){
    for(int i=2; i<=1e6; i++) isprime[i] = 1;
    for(int i=2; i<=1e6; i++){
        if(!isprime[i]) continue;
        prime.PB(i);
        for(int j=i*i; j<=1e6; j+=i){
            isprime[j] = 0;
        }
    }
}
int a[N], vis[N];
map<int, vector<int>> edge[N];
set<int> st;

int dfs(int x, int f, int pr){
    vis[x] = 1;
    for(auto i : edge[x][pr]){
        if(i == f || vis[i]) continue;
        vis[x] += dfs(i, x, pr);
    }
    return vis[x];
}
void solve(){
    init();
    int n, ans = 0;
    cin >> n;
    for(int i=1; i<=n; i++){
        cin >> a[i];
        if(a[i] > 1) ans = 1;
    }
    for(int i=0; i<n-1; i++){
        int aa, bb;
        cin >> aa >> bb;
        int g = gcd(a[aa], a[bb]);
        int x = g;
        for(int j=2; j*j<=g; j++){
            if(x % j == 0){
                st.insert(j);
                edge[aa][j].PB(bb);
                edge[bb][j].PB(aa);
                while(x % j == 0)
                    x /= j;
            }
        }
        if(x > 1){
            st.insert(x);
            edge[aa][x].PB(bb);
            edge[bb][x].PB(aa);
        }
    }

    for(auto pr : st){
        for(int i=1; i<=n; i++){
            if(a[i] % pr) continue;
            if(vis[i]) continue;
            ans = max(ans, dfs(i, i, pr));
        }
        for(int i=1; i<=n; i++)
            vis[i] = 0;
    }
    cout << ans << endl;
}
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int T = 1;
    // cin >> T;
    while(T--){
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 11ms
memory: 103400kb

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: 20ms
memory: 100728kb

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: 19ms
memory: 103288kb

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: 16ms
memory: 101936kb

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: