QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#328965#4584. Not OneNYCU_CartesianTree#WA 220ms98548kbC++141.8kb2024-02-16 11:16:232024-02-16 11:16:23

Judging History

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

  • [2024-02-16 11:16:23]
  • 评测
  • 测评结果:WA
  • 用时:220ms
  • 内存:98548kb
  • [2024-02-16 11:16:23]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5, M = 1e6 + 5;
int a[N];
bitset<N> is_same_group;
vector<int> prime, point, prime_div[M], prime_node[M], graph[N];
int dsu[N];
int Find(int x){
    if(dsu[x] < 0)
        return x;
    return dsu[x] = Find(dsu[x]);
}
int Union(int x, int y){
    x = Find(x);
    y = Find(y);
    if(x == y)
        return -dsu[x];
    if(dsu[x] > dsu[y])
        swap(x, y);
    dsu[x] += dsu[y];
    dsu[y] = x;
    return -dsu[x];
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        dsu[i] = -1;
    }
    int x, y;
    for(int i = 0; i < n - 1; i++){
        cin >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    for(int i = 2; i <= 1e6; i++){
        if(prime_div[i].empty()){
            prime.push_back(i);
            prime_div[i].push_back(i);
            for(int j = i * i; j <= 1e6; j += i){
                prime_div[j].push_back(i);
            }
        }
    }
    for(int i = 1; i <= n; i++){
        for(auto j : prime_div[a[i]]){
            prime_node[j].push_back(i);
        }
    }
    int ans = 0;
    for(auto i : prime){
        for(auto j : prime_node[i]){
            is_same_group[j] = 1;
        }
        int maxc = !prime_node[i].empty();
        for(auto j : prime_node[i]){
            for(auto u : graph[j]){
                if(is_same_group[u]){
                    maxc = max(maxc, Union(j, u));
                }
            }
        }
        ans = max(ans, maxc);
        for(auto j : prime_node[i]){
            is_same_group[j] = 0;
            dsu[j] = -1;
        }
    }
    cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 156ms
memory: 92320kb

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: 160ms
memory: 92072kb

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: 147ms
memory: 92672kb

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: 135ms
memory: 92556kb

input:

2
1 1
1 2

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 220ms
memory: 98120kb

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: 0
Accepted
time: 192ms
memory: 98356kb

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:

70

result:

ok single line: '70'

Test #7:

score: 0
Accepted
time: 188ms
memory: 98096kb

input:

100000
721703 392879 695588 695588 360569 721703 721703 721703 392879 721703 521691 173897 173897 31699 605629 330661 521691 887572 869485 721703 538883 633980 347794 721703 173897 524464 380388 983370 330661 196674 982669 327790 392879 721703 557243 347794 65558 163895 31699 521691 392879 426127 22...

output:

23467

result:

ok single line: '23467'

Test #8:

score: 0
Accepted
time: 185ms
memory: 98548kb

input:

100000
999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983 999983...

output:

100000

result:

ok single line: '100000'

Test #9:

score: 0
Accepted
time: 141ms
memory: 92852kb

input:

16666
499521 566687 918452 827210 997739 405921 930466 453499 449367 302663 658220 713125 615536 484783 586258 469620 984395 526320 799319 849099 284114 723734 22671 661826 325985 662335 107207 564634 836931 323058 117954 661326 658962 599495 103335 377185 45338 385027 50770 961843 691799 693591 281...

output:

13

result:

ok single line: '13'

Test #10:

score: 0
Accepted
time: 188ms
memory: 96504kb

input:

66666
10 6 90 60 15 150 90 10 150 12 30 10 12 150 15 50 6 60 60 150 50 50 50 30 10 45 150 6 6 90 50 6 30 12 90 6 60 15 90 45 6 90 45 45 10 60 150 30 30 6 90 12 30 30 6 60 12 45 50 60 45 50 50 50 12 60 10 50 30 150 15 150 90 10 10 12 10 30 6 60 45 6 6 60 12 12 45 12 12 45 12 60 30 30 150 90 6 150 6 6...

output:

46

result:

ok single line: '46'

Test #11:

score: -100
Wrong Answer
time: 156ms
memory: 93372kb

input:

28571
921969 307323 580499 751234 102441 648793 743442 68294 955092 600043 568071 610323 614646 102441 136588 962429 307588 239029 990263 170735 50247 221505 614646 232539 102441 424891 682940 102441 682940 887822 424891 239029 849782 68294 819528 850736 751234 375617 400566 34147 768051 341470 1024...

output:

12

result:

wrong answer 1st lines differ - expected: '12698', found: '12'