QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#296489#7588. Monster HuntersomeoneWA 641ms29460kbC++142.2kb2024-01-03 06:05:502024-01-03 06:05:51

Judging History

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

  • [2024-01-03 06:05:51]
  • 评测
  • 测评结果:WA
  • 用时:641ms
  • 内存:29460kb
  • [2024-01-03 06:05:50]
  • 提交

answer

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

struct Node {
    int sub, add, gain, id;
};

const int N = 2e5 + 42;

Node node[N];
vector<int> adj[N];
int n, fa[N], uf[N];

int F(int i) {
    if(uf[i] == i)
        return i;
    return uf[i] = F(uf[i]);
}

int U(int a, int b) {
    a = F(a), b = F(b);
    uf[b] = a;
    return a;
}

void dfs(int i, int pre = 0) {
    fa[i] = pre;
    for(int j : adj[i]) if(j != pre)
        dfs(j, i);
}

Node add(Node a, Node b) {
    int gain = a.gain + b.gain,
        requis = max(a.sub, a.sub - a.add + b.sub);
    return {requis, requis + gain, gain, U(a.id, b.id)};
}

bool cmp(Node a, Node b) {
    if(a.gain <= 0 && b.gain <= 0) return a.add < b.add;
    if(a.gain >= 0 && b.gain >= 0) return a.sub > b.sub;
    return a.gain < b.gain;
}

void solve() {
    cin >> n;
    for(int i = 0; i < n; i++)
        uf[i] = i;
    for(int i = 0; i < n; i++)
        adj[i].clear();
    node[0].sub = node[0].add = node[0].id = 0;
    for(int i = 1; i < n; i++) {
        node[i].id = i;
        cin >> node[i].sub >> node[i].add;
        node[i].gain = node[i].add - node[i].sub;
    }
    for(int i = 1; i < n; i++) {
        int a, b; cin >> a >> b;
        a--, b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(0);

    priority_queue<Node, vector<Node>, function<bool(Node, Node)>> pos(cmp), neg(cmp);
    for(int i = 1; i < n; i++)
        if(node[i].gain >= 0)
            pos.push(node[i]);
        else
            neg.push(node[i]);
    while(!pos.empty() || !neg.empty()) {
        Node nd = (!pos.empty() ? pos.top() : neg.top());
        if(!pos.empty()) pos.pop();
        else neg.pop();
        int i = F(nd.id);
        if(i != 0 && i == nd.id && nd.sub == node[i].sub && nd.add == node[i].add) {
            int anc = F(fa[i]);
            node[anc] = add(node[anc], node[i]);
            if(node[anc].gain >= 0)
                pos.push(node[anc]);
            else
                neg.push(node[anc]);
        }
    }
    cout << node[F(0)].sub << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int nbt; cin >> nbt;
    while(nbt--)
        solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 11792kb

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

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
177090559
383609891
1086228605
505892074
409176759
84584890
84657366

result:

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