QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#296493 | #7588. Monster Hunter | someone | AC ✓ | 842ms | 31864kb | C++14 | 2.5kb | 2024-01-03 06:27:27 | 2024-01-03 06:27:29 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct Node {
int sub, add, gain, id = 0;
};
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) {
if(a.add == b.add) {
if(a.sub == b.sub)
return a.id < b.id;
return a.sub < b.sub;
}
return a.add < b.add;
}
if(a.gain >= 0 && b.gain >= 0) {
if(a.sub == b.sub) {
if(a.add == b.add)
return a.id < b.id;
return a.add < b.add;
}
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 = node[0].gain = 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)>> nds(cmp);
for(int i = 1; i < n; i++)
nds.push(node[i]);
while(!nds.empty()) {
Node nd = nds.top();
nds.pop();
int i = F(nd.id);
//cout << "test " << i << ' ' << nd.id << ' ' << nd.sub << ' ' << nd.add << '\n';
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]);
//cout << "new " << i << ' ' << anc << ' ' << node[anc].sub << ' ' << node[anc].add << '\n';
nds.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: 0ms
memory: 16324kb
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: 801ms
memory: 30944kb
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: 842ms
memory: 31864kb
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