QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#258282 | #7588. Monster Hunter | ddl_VS_pigeon# | AC ✓ | 497ms | 19828kb | C++17 | 3.0kb | 2023-11-19 16:45:13 | 2023-11-19 16:45:13 |
Judging History
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;
int t;
bool operator<(const Pa& y) const { return a > y.a; }
};
struct Pb {
ll a, b, id;
int t;
bool operator<(const Pb& y) const { return b < y.b; }
};
priority_queue<Pa> sa;
priority_queue<Pb> sb;
auto insert = [&](int i, int t) {
if (i > 1) {
if (p[i].first <= p[i].second) {
sa.push({p[i].first, p[i].second, i, t});
} else {
sb.push({p[i].first, p[i].second, i, t});
}
}
};
for (int i = 2; i <= n; i++) {
insert(i, 0);
}
vector<int> tag(n + 1);
for (int i = 2; i <= n; i++) {
if (sa.empty()) {
auto [_a, _b, id, _t] = sb.top();
sb.pop();
int f = dsu.getfa(fa[id]);
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, ++tag[f]);
dsu.merge(id, f);
} else {
auto [_a, _b, id, _t] = sa.top();
sa.pop();
int f = dsu.getfa(fa[id]);
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, ++tag[f]);
dsu.merge(id, f);
}
while (!sa.empty() && sa.top().t < tag[sa.top().id]) {
sa.pop();
}
while (!sb.empty() && sb.top().t < tag[sb.top().id]) {
sb.pop();
}
}
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: 3464kb
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: 0
Accepted
time: 484ms
memory: 18912kb
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 541156749 191617241 177090559 383609891 27231993394 19064806867 37194996614 84584890 84657366
result:
ok 10 numbers
Test #3:
score: 0
Accepted
time: 497ms
memory: 19828kb
input:
1109 100 49335125 540429 21327621 30666761 84726139 32583318 62343520 86386375 68371125 80203961 11703807 57711659 4086043 20964787 71845269 8258000 70046842 34257600 86312745 39362281 92327731 31351601 27782858 53379001 51945765 34797724 7197540 99710490 43627335 61003907 256869 8469838 89150549 45...
output:
74012350 485460189 68654921 22821963 702398949 944191 63683983 49851540 539751698 378185590 17649578 153086960 85518043 218934054 40886105 365498548 568650050 115493085 13675680 20327516 19299519 47328093 101160112 314233771 7506569 20797884 140632255 699457438 11257338 71975351 699154224 597663853 ...
result:
ok 1109 numbers