QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#789694 | #7588. Monster Hunter | KFC | ML | 2ms | 7884kb | C++17 | 1.9kb | 2024-11-27 21:28:55 | 2024-11-27 21:29:01 |
Judging History
answer
// Hydro submission #67471e959592d6097b8706c3@1732714134256
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 1e5 + 10;
vector<int> v[N];
int rt[N];
void dfs(int x, int fa){
rt[x] = fa;
for(int y : v[x]){
if(y == fa) continue;
dfs(y, x);
}
}
int n;
struct Node{
int a, b, id, res;
bool operator < (const Node &A) const {
if(a > b && A.a <= A.b) return 1;//回血在前
if(a <= b && A.a > A.b) return 0;
if(a > b && A.a > A.b) return b <= A.b;
else return a > A.a;
}
}p[N];
bool vis[N];
int ans[N];
priority_queue<Node> q;
int find(int x) {
if(vis[rt[x]]) return rt[x] = find(rt[x]);
return rt[x];
}
void solve(){
cin >> n;
for(int i = 2; i <= n; i++){
int a, b;
cin >> a >> b;
p[i] = Node{a, b, i, 0};
q.push(p[i]);
}
for(int i = 1; i < n; i++){
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1, 0);
int cnt = 0;
while(!q.empty()){
auto now = q.top();
q.pop();
// cout << now.id << endl;
if(vis[now.id] || now.res != ans[now.id]) continue;
vis[now.id] = 1;
int x = now.id, fax = find(now.id);
int a = p[fax].a;
p[fax].a = max(p[fax].a, p[fax].a - p[fax].b + p[x].a);
p[fax].b = p[fax].b - a - p[x].a + p[x].b + p[fax].a;
if(fax > 1){
p[fax].res = ans[fax] = ++cnt;
q.push(p[fax]);
}
}
cout << p[1].a << endl;
}
signed main(){
// freopen("tree.in", "r", stdin);
// freopen("tree.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T--) solve();
cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
return 0;
}
/*
1
4
2 6
5 4
6 2
1 2
2 3
3 4
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 7884kb
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
Memory 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...