QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#182924#4584. Not OneIamtui1010WA 288ms20244kbC++143.3kb2023-09-18 19:10:522023-09-18 19:10:53

Judging History

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

  • [2023-09-18 19:10:53]
  • 评测
  • 测评结果:WA
  • 用时:288ms
  • 内存:20244kb
  • [2023-09-18 19:10:52]
  • 提交

answer

// Copyright (c) 2023, Le Duc Phuc Long

/*If you don't think twice, you have to code twice.*/

// Include
#include <bits/stdc++.h>  // NOLINT
// #include <conio.h>

// Namespace
using namespace std;  // NOLINT

// Define
#define nln '\n'

// Typedef and Struct
typedef int64_t lo;

// Constant

// Declare global variables.
vector<lo> prm;
// End of global variable declaration.

bool is_prime(lo n) {
    if (n < 2) return false;
    for (lo i = 2; i <= round(sqrt(n)); ++i)
        if (n % i == 0) return false;
    return true;
}

void precompute() {
    lo limit = 1e7, prd = 1;
    for (lo i = 2; i <= limit; i++) {
        if (prd > limit)
            break;
        if (is_prime(i)) {
            prd *= i;
            prm.push_back(i);
        }
    }
}

class Disjoint_set {
    private:
    public:
    lo *par, *rnk, n;

    Disjoint_set(lo n) {
        this->n = n;
        par = new lo[n+1], rnk = new lo[n+1];
        for (lo i = 1; i <= n; ++i)
            par[i] = i, rnk[i] = 0;
    }

    ~Disjoint_set() {
        delete[] par, delete[] rnk;
    }

    lo Find(lo v) {
        if (par[v] == v)
            return v;
        lo p = Find(par[v]);
        par[v] = p;
        return par[v];
    }

    void Union(lo u, lo v) {
        lo par_u = Find(u), par_v = Find(v);
        if (rnk[par_u] < rnk[par_v]) {
            par[par_u] = par_v;
        } else {
            par[par_v] = par_u;
            if (rnk[par_u] == rnk[par_v])
                rnk[par_u]++;
        }
    }

    lo Biggest_set() {
        lo mav = 0;
        map<lo, lo> cnt;
        for (lo i = 1; i <= n; ++i) {
            lo pi = Find(i);
            cnt[pi]++;
            mav = max(mav, cnt[pi]);
        }
        return mav;
    }
};

void solve() {
    // Input
    lo n;   cin >> n;
    set<lo> sav;
    vector<lo> arr(n+1);
    for (lo i = 1; i <= n; ++i) {
        cin >> arr[i];
        sav.insert(arr[i]);
    }
    vector<pair<lo, lo>> edges(n-1);
    for (auto &p : edges)
        cin >> p.first >> p.second;
    // Solve
    // Special case
    if (arr[1] == 1 and sav.size() == 1) {
        cout << 0 << nln;
        return;
    }
    // Another special case, when all nodes are prime ans the same value
    map<lo, set<lo>> clt;
    for (auto &e : edges)
        if (arr[e.first] == arr[e.second]) {
            clt[arr[e.first]].insert(e.first);
            clt[arr[e.first]].insert(e.second);
        }
    lo s_ans = 0;
    for (auto &p : clt)
        if (p.second.size() > s_ans)
            s_ans = p.second.size();
    // Process the latter case
    lo ans = 1;  // at least 1 is size of sastified subtree
    // Use Disjoint Set
    for (auto p : prm) {
        Disjoint_set s(n);
        for (auto e : edges) 
            if (__gcd(arr[e.first], arr[e.second]) % p == 0)
                s.Union(e.first, e.second);
        ans = max(ans, s.Biggest_set());
    }
    cout << max(ans, s_ans) << nln;
}

int main(int argc, char* argv[]) {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0)->sync_with_stdio(0);
    int T = 1;
    // cin >> T;
    precompute();
    while (T--) {
        solve();
    }
    cerr << "It's ok Long, Good for now !!" << nln;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

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: 0ms
memory: 3520kb

input:

2
1 1
1 2

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 288ms
memory: 20244kb

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: 267ms
memory: 15688kb

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:

16905

result:

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