QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#65330#4584. Not OneT3alaadl3k2olyehymn3k#TL 12ms13592kbC++202.2kb2022-11-29 20:58:122022-11-29 20:58:14

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-29 20:58:14]
  • 评测
  • 测评结果:TL
  • 用时:12ms
  • 内存:13592kb
  • [2022-11-29 20:58:12]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define int long long
const int N = 100005, P = 19;
const int M = 1e6 + 5;

int a[N];
struct Edge {
    int u, v, g;
    Edge(int u, int v, int g): u(u), v(v),g(g) {}
    Edge(){}
};
vector<Edge > edges;

int prime[M];

void seive(){
    for(int i = 2; i < M; ++i){
        if (prime[i]) continue;
        for(int j = i; j < M; j += i)
            prime[j] = i;
    }
}
bool vis[N];

struct {
    int par[N], rnk[N];
    void init(int n){
        for(int i = 1; i <= n; ++i)
            par[i] = i, rnk[i] = 1;
    }
    int findParent(int u){
        return par[u] = (par[u] == u ? u : findParent(par[u]));
    }
    bool merge(int u, int v){
        u = findParent(u);
        v = findParent(v);
        if(u == v)return false;
        if(rnk[u] < rnk[v])
            swap(u,v);
        par[v] = u;
        rnk[u] += rnk[v];
        return true;
    }

}dsu;
signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    seive();
    for(int i = 1; i <= n; ++i)
        cin >> a[i];
    int u , v;
    set<int> st;
    for(int i = 0; i < n - 1; ++i){
        cin >> u >> v;
        int gc = __gcd(a[u],a[v]);
        if(gc == 1)continue;
        edges.emplace_back(u,v, gc);
        if(!vis[gc]){
            int x = gc;
            for (int p = prime[x]; p > 1; p = prime[x]) {
                st.insert(p);
                for (; x % p == 0; x /= p);
            }
            vis[gc] = 1;
        }
    }
    dsu.init(n);

    int ans = 0;
    vector<pair<int, int> > changes;
    for(auto &gc: st){
        for(int i = 0; i < edges.size(); ++i){
            if(edges[i].g % gc == 0){
                bool res = dsu.merge(edges[i].u, edges[i].v);
                if(res)changes.emplace_back(edges[i].u, edges[i].v);
            }
        }
        for(int i = 1; i <= n; ++i){
            ans = max(ans, dsu.rnk[dsu.findParent(i)]);
        }
        for(auto &[u,v ]: changes){
            dsu.par[u] = u;
            dsu.par[v] = v;
            dsu.rnk[u] = dsu.rnk[v] = 1;
        }
    }
    cout << ans << '\n';

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 12ms
memory: 13592kb

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

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: 6ms
memory: 11552kb

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

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: