QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#471427#9110. Zayin and Treereal_sigma_team#WA 461ms10980kbC++172.6kb2024-07-10 21:06:002024-07-10 21:06:00

Judging History

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

  • [2024-07-10 21:06:00]
  • 评测
  • 测评结果:WA
  • 用时:461ms
  • 内存:10980kb
  • [2024-07-10 21:06:00]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int tests;
    cin >> tests;
    while (tests--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (auto& i : a) cin >> i;
        vector<vector<int>> g(n);
        for (int i = 0; i < n -1 ; ++i) {
            int u, v;
            cin >> u >> v;
            --u, --v;
            g[u].push_back(v);
            g[v].push_back(u);
        }
        vector<int> sz(n);
        vector<bool> del(n);
        auto sizes = [&](auto sizes, int u, int p) -> int {
            sz[u] = 1;
            for (auto to : g[u]) {
                if (del[to] || to == p) continue;
                sz[u] += sizes(sizes, to, u);
            }
            return sz[u];
        };
        int ans = 2;
        auto solve = [&](auto solve, int u) -> void {
            n = sizes(sizes, u, -1);
            while (true) {
                bool fnd = false;
                for (auto to : g[u]) {
                    if (sz[to] < sz[u] && sz[to] > n / 2) {
                        u = to;
                        fnd = true;
                        break;
                    }
                }
                if (!fnd) break;
            }
            vector<int> cur(4, 1e9);
            vector<int> tmp(4, 1e9);
            auto dfs = [&](auto dfs, int u, int p, int mx, int mn, int len) -> void {
                len++;
                mx = max(mx, a[u]);
                mn = min(mn, a[u]);
                tmp[0] = min(tmp[0], len);
                tmp[1] = min(tmp[1], len - mx);
                tmp[2] = min(tmp[2], len + mn);
                tmp[3] = min(tmp[3], len + mn - mx);
                for (auto to : g[u]) {
                    if (to == p || del[to]) continue;
                    dfs(dfs, to, u, mn, mx, len);
                }
            };
            cur[0] = cur[3] = 0;
            cur[1] = -a[u];
            cur[2] = a[u];
            for (auto to : g[u]) {
                if (del[to]) continue;
                fill(tmp.begin(), tmp.end(), 1e9);
                dfs(dfs, to, u, a[u], a[u], 1);
                for (int j = 0; j < 4; ++j) {
                    ans = min(ans, cur[3 ^ j] + tmp[j]);
                }
                for (int j = 0; j < 4; ++j) {
                    cur[j] = min(cur[j], tmp[j]);
                }
            }
            del[u] = true;
            for (auto to : g[u]) {
                if (!del[to]) solve(solve, to);
            }
        };
        solve(solve, 0);
        cout << ans << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 461ms
memory: 10980kb

input:

3009
5
4 5 3 4 2
1 2
2 3
3 4
3 5
5
4 4 1 1 2
1 2
2 3
3 4
3 5
10
5 8 1 0 8 7 5 2 0 4
2 4
3 8
3 9
1 2
1 3
3 6
4 5
5 7
6 10
10
6 8 8 4 8 0 6 6 0 2
7 10
1 7
2 9
2 3
3 4
1 5
1 6
6 8
1 2
10
9 0 4 0 4 6 0 2 0 0
1 5
1 3
1 7
2 6
1 2
1 9
1 4
5 8
7 10
10
8 8 1 2 7 4 8 6 0 8
1 6
1 7
1 5
7 9
1 3
1 2
2 10
3 4
1 8...

output:

0
-1
-6
-6
-7
-6
-7
-4
-3
-7
-5
-6
-4
-4
-6
-3
-4
-7
-4
-4
-6
-6
-5
-4
-4
-5
-6
-6
-7
-7
-5
-7
-6
-6
-7
-6
-5
-5
-4
-6
-5
-4
-6
-6
-6
-6
-3
-6
-3
-6
-4
-6
-7
-6
-7
-5
-6
-5
-7
-6
-4
-7
-2
-4
-5
-6
-3
-4
-7
-6
-5
-5
-4
-3
-5
-2
-4
-2
-6
-5
-7
-4
-5
-4
-7
-7
-4
-6
-5
-4
-6
-5
-4
-6
-3
-5
-7
-7
-7
-5
-...

result:

wrong answer 13th lines differ - expected: '-5', found: '-4'