QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#258271#7588. Monster Hunterddl_VS_pigeon#TL 1ms3616kbC++173.1kb2023-11-19 16:37:592023-11-19 16:38:00

Judging History

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

  • [2023-11-19 16:38:00]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3616kb
  • [2023-11-19 16:37:59]
  • 提交

answer

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

struct DSU {
    vector<int> fa;
    DSU(int n) : fa(n + 1) {
        for (int i = 1; i <= n; i++) {
            fa[i] = i;
        }
    }
    int getfa(int v) { return (fa[v] == v) ? v : (fa[v] = getfa(fa[v])); }
    void merge(int u, int v) { fa[getfa(u)] = getfa(v); }
};

struct Tree {
    int n;
    vector<vector<int>> e;
    vector<pair<ll, ll>> p;
    vector<int> fa;
    DSU dsu;
    Tree(int _n) : n(_n), e(n + 1), p(n + 1), fa(n + 1), dsu(n + 1) {
        for (int i = 2; i <= n; i++) {
            cin >> p[i].first >> p[i].second;
        }
        for (int i = 2; i <= n; i++) {
            int u, v;
            cin >> u >> v;
            e[u].emplace_back(v);
            e[v].emplace_back(u);
        }
        getfa(1, 1);
    }
    void getfa(int v, int f) {
        fa[v] = f;
        for (auto u : e[v]) {
            if (u != f) {
                getfa(u, v);
            }
        }
    }
    ll solve() {
        struct Pa {
            ll a, b, id;
            bool operator<(const Pa& y) const { return a < y.a; }
        };
        struct Pb {
            ll a, b, id;
            bool operator<(const Pb& y) const { return b > y.b; }
        };
        set<Pa> sa;
        set<Pb> sb;
        auto insert = [&](int i) {
            if (i > 1) {
                if (p[i].first <= p[i].second) {
                    sa.insert({p[i].first, p[i].second, i});
                } else {
                    sb.insert({p[i].first, p[i].second, i});
                }
            }
        };
        auto remove = [&](int i) {
            if (i > 1) {
                if (p[i].first <= p[i].second) {
                    sa.erase({p[i].first, p[i].second, i});
                } else {
                    sb.erase({p[i].first, p[i].second, i});
                }
            }
        };
        for (int i = 2; i <= n; i++) {
            insert(i);
        }
        for (int i = 2; i <= n; i++) {
            if (sa.empty()) {
                auto [_a, _b, id] = *sb.begin();
                sb.erase(sb.begin());
                int f = dsu.getfa(fa[id]);
                remove(f);
                auto [a, b] = p[f];
                p[f].first = max(a, a - b + _a);
                p[f].second = -a + b - _a + _b + p[f].first;
                insert(f);
                dsu.merge(id, f);
            } else {
                auto [_a, _b, id] = *sa.begin();
                sa.erase(sa.begin());
                int f = dsu.getfa(fa[id]);
                remove(f);
                auto [a, b] = p[f];
                p[f].first = max(a, a - b + _a);
                p[f].second = -a + b - _a + _b + p[f].first;
                insert(f);
                dsu.merge(id, f);
            }
        }
        return p[1].first;
    }
};

void solve() {
    int n;
    cin >> n;
    Tree tr(n);
    cout << tr.solve() << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    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: 3616kb

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
Time Limit Exceeded

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:


result: