QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#657992#6437. Paimon's TreellleiWA 417ms3908kbC++203.0kb2024-10-19 15:56:302024-10-19 15:56:31

Judging History

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

  • [2024-10-19 15:56:31]
  • 评测
  • 测评结果:WA
  • 用时:417ms
  • 内存:3908kb
  • [2024-10-19 15:56:30]
  • 提交

answer

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

constexpr LL INF = 1e18;

void solve() {
    int n;
    cin >> n;
    vector<LL> a(n);

    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    vector<vector<int>> e(n + 1);
    for (int i = 0; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        e[u].push_back(v);
        e[v].push_back(u);
    }

    int tot = n + 1;
    for (int i = 0; i <= n; ++i) {
        if (e[i].size() == 1) {
            e.push_back({});
            e[i].push_back(tot);
            e[tot].push_back(i);
            tot++;
        }
    }

    vector dis(tot, vector<int>(tot, -1));


    vector<array<int, 3>> b;
    int s = -1;
    auto dfs = [&](auto &&self, int u, int fa, int dep) -> void {
        for (int v : e[u]) {
            if (v == fa) {
                continue;
            }
            if (s < v) {
                dis[s][v] = dep + 1;
                b.push_back({dep + 1, s, v});
            }
            self(self, v, u, dep + 1);
        }
    };

    for (int i = 0; i < tot; ++i) {
        s = i;
        dfs(dfs, i, -1, 0);
    }

    sort(b.begin(), b.end());

    vector f(tot, vector(tot, vector<LL>(tot, -INF)));

    auto myMx = [&](LL &x, const LL &y) {
        if (x < y) {
            x = y;
        }
    };

    LL ans = 0;
    for (auto [len, x, y] : b) {
        if (len == 1) {
            continue;
        }
        if (len == 2) {
            f[x][y][0] = 0;
        }
        
        int cnt = 0;
        bool flag = false;
        auto dfs = [&](auto &&self, int u, int fa) -> void {
            for (int v : e[u]) {
                if (v == y) {
                    flag = true;
                    continue;
                }
                if (v == fa) {
                    continue;
                }
                if (v <= n) {
                    ++cnt;
                }
                self(self, v, u);
            }
        };

        for (int v : e[x]) {
            cnt = 0;
            dfs(dfs, v, x);
            if (flag) {
                break;
            }
        }

        auto update = [&](int x, int y, int z) {
            for (int p : e[x]) {
                int q = y;
                if (p > q) {
                    swap(p, q);
                }

                if (dis[p][q] != len + 1) {
                    continue;
                }

                myMx(f[p][q][z + 1], f[x][y][z] + a[z]);
            }
        };

        for (int i = 0; i <= cnt; ++i) {
            if (i) {
                myMx(f[x][y][i], f[x][y][i - 1]);
            }
            myMx(ans, f[x][y][i]);

            update(x, y, i);
            update(y, x, i);
        }
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
5
1 7 3 5 4
1 3
2 3
3 4
4 5
4 6
1
1000000000
1 2

output:

16
1000000000

result:

ok 2 number(s): "16 1000000000"

Test #2:

score: -100
Wrong Answer
time: 417ms
memory: 3908kb

input:

5000
19
481199252 336470888 634074578 642802746 740396295 773386884 579721198 396628655 503722503 971207868 202647942 2087506 268792718 46761498 443917727 16843338 125908043 691952768 717268783
9 4
4 18
12 9
10 9
4 1
6 12
2 18
9 13
6 14
4 8
2 3
10 17
2 20
19 20
5 1
12 15
15 16
4 7
17 11
4
240982681 ...

output:

5750811120
1896999359
4208559611
4140156713
5361004844
1875024569
3690026656
3647635727
3412485417
7807375141
5341435147
2355946110
3090496776
5626636202
4729664767
2207592767
572868833
4759005973
2944749369
2538044586
3083947956
5757497518
1421427135
3971435093
1197051728
396588615
251138097
221986...

result:

wrong answer 8th numbers differ - expected: '3702623113', found: '3647635727'