QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#484095#4584. Not OneThirvin#WA 58ms9980kbC++203.7kb2024-07-19 15:56:172024-07-19 15:56:17

Judging History

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

  • [2024-07-19 15:56:17]
  • 评测
  • 测评结果:WA
  • 用时:58ms
  • 内存:9980kb
  • [2024-07-19 15:56:17]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) signed(size(x))
#define fi first
#define se second

inline void solve() {
        int n;
        cin >> n;

        vector<int> a(n + 1);
        for (int i = 1; i <= n; ++i)
                cin >> a[i];

        vector<vector<int>> edges(n + 1);
        for (int i = 1; i < n; ++i) {
                int u, v;
                cin >> u >> v;
                edges[u].emplace_back(v);
                edges[v].emplace_back(u);
        }

        {
                int tmp = a[1];
                for (int i = 2; i <= n; ++i) {
                        tmp = gcd(tmp, a[i]);
                        if (tmp == 1)
                         break;
                }

                if (tmp != 1) {
                        cout << n;
                        return;
                }
        }

        vector<int> sub(n + 1), si(n + 1);
        auto dfs1 = [&](auto&& self, int cur, int par) -> void {
                sub[cur] = a[cur];
                si[cur] = 1;
                for (int nxt : edges[cur]) {
                        if(nxt == par) continue;
                        self(self, nxt, cur);
                        sub[cur] = gcd(sub[cur], sub[nxt]);
                        si[cur] += si[nxt];
                }
                return;
        };

        dfs1(dfs1, 1, 0);

        int ans = 0;
        auto dfs2 = [&](auto&& self, int cur, int par, int pre) -> void {
                vector<pair<int, int>> v;
                for (int nxt : edges[cur]) {
                        if (nxt == par)
                         v.emplace_back(gcd(a[cur], pre), n - si[cur]);
                        else
                         v.emplace_back(gcd(a[cur], sub[nxt]), si[nxt]);
                }

                vector<int> gg(sz(v)), ggg(sz(v) + 1);
                ggg.back() = a[cur];
                gg[0] = v[0].fi;

                for (int i = 1; i < sz(v); ++i)
                        gg[i] = gcd(gg[i - 1], v[i].fi);
                for (int i = sz(v) - 1; i >= 0; --i)
                        ggg[i] = gcd(ggg[i + 1], v[i].fi);

                if (sz(v) == 1)
                        ans = max(ans, (a[cur] != 1 ? 1 : 0));
                else {
                        for (int i = 0; i < sz(v); ++i) {
                         if (i == 0) {
                                if (ggg[1] == 1) continue;
                                ans = max(ans, n - v[i].se);
                         }
                         else if (i == sz(v) - 1) {
                                if (gg[i - 1] == 1) continue;
                                ans = max(ans, n - v[i].se);
                         }
                         else {
                                if (gcd(gg[i - 1], ggg[i + 1]) == 1) continue;
                                ans = max(ans, n - v[i].se);
                         }
                        }
                }

                for (int i = 0; i < sz(edges[cur]); ++i) {
                        if (edges[cur][i] == par) continue;
                        int tmp;
                        if (i == 0)
                         tmp = ggg[i + 1];
                        else if (i == sz(edges[cur]) - 1)
                         tmp = gg[i - 1];
                        else
                         tmp = gcd(gg[i - 1], ggg[i + 1]);

                        self(self, edges[cur][i], cur, tmp);
                }

        };


        dfs2(dfs2, 1, 0, 0);
        cout << ans << '\n';
        return;
}

signed main() {
        cin.tie(0)->sync_with_stdio(0);
        int t = 1;
        while (t--)
                solve();
        return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3708kb

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

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

input:

2
1 1
1 2

output:

0

result:

ok single line: '0'

Test #5:

score: -100
Wrong Answer
time: 58ms
memory: 9980kb

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:

6

result:

wrong answer 1st lines differ - expected: '213', found: '6'