QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#788821#7588. Monster HunterHunsterWA 545ms19976kbC++231.9kb2024-11-27 18:25:072024-11-27 18:25:08

Judging History

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

  • [2024-11-27 18:25:08]
  • 评测
  • 测评结果:WA
  • 用时:545ms
  • 内存:19976kb
  • [2024-11-27 18:25:07]
  • 提交

answer

#include <bits/stdc++.h>

using LL = long long;

constexpr int N = 100005;

struct Val { 
    LL x, y; 
    friend bool operator<(Val a, Val b) {
        if (a.y >= 0 && b.y < 0) return 1;
        if (a.y < 0 && b.y >= 0) return 0;
        if (a.y < 0) return std::max(a.x, b.x - a.y) < std::max(b.x, a.x - b.y);
        else return a.x < b.x;
    }
};

int n;
Val val[N];
int fa[N], par[N], g[N];
std::vector<int> tree[N];

void predfs(int u, int p) {
    fa[u] = p;
    for (int v : tree[u]) {
        if (v == p) continue;
        predfs(v, u);
    }
}
int find_par(int x) { return par[x] == x ? x : par[x] = find_par(par[x]); }

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int T;
    std::cin >> T;
    while (T--) {
        std::cin >> n;
        for (int i = 1; i <= n; i++) tree[i].clear();
        for (int i = 2; i <= n; i++) {
            int a, b;
            std::cin >> a >> b;
            val[i] = {a, b - a};
        }
        for (int i = 1; i < n; i++) {
            int x, y;
            std::cin >> x >> y;
            tree[x].push_back(y);
            tree[y].push_back(x);
        }
        predfs(1, 0);
        std::priority_queue<std::tuple<Val, int, int>, std::vector<std::tuple<Val, int, int>>, std::greater<std::tuple<Val, int, int>>> que;
        for (int i = 2; i <= n; i++) que.push({val[i], i, g[i] = 0});
        std::iota(par + 1, par + n + 1, 1);
        while (que.size()) {
            auto [v, x, t] = que.top();
            que.pop();
            if (x == 1) continue;
            if (t != g[x]) continue;
            int p = find_par(fa[x]);
            val[p].x = std::max(val[p].x, v.x - val[p].y);
            val[p].y += v.y;
            que.push({val[p], p, ++g[p]});
            par[x] = p;
        }
        printf("%lld\n", val[1].x);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
4
2 6
5 4
6 2
1 2
2 3
3 4

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: -100
Wrong Answer
time: 545ms
memory: 19976kb

input:

10
50000
112474 198604
847262 632597
871374 962220
971398 284540
10360 695245
43233 569941
64019 203468
598469 997911
41841 657552
333198 936119
546370 180011
58831 901040
959767 396595
710441 277461
429299 209831
494164 138327
393982 581911
993164 617488
108113 160310
55841 611360
301395 291074
149...

output:

63495498
604652243
796269298
796269298
796269298
796269298
796269298
796269298
796269298
796269298

result:

wrong answer 2nd numbers differ - expected: '541156749', found: '604652243'