QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#789684 | #7588. Monster Hunter | KFC | RE | 0ms | 0kb | C++17 | 1.8kb | 2024-11-27 21:27:44 | 2024-11-27 21:27:45 |
answer
// Hydro submission #67471e4e9592d6097b87066e@1732714063322
#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];
}
signed main(){
freopen("tree.in", "r", stdin);
freopen("tree.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
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;
cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
return 0;
}
详细
Test #1:
score: 0
Dangerous Syscalls
input:
1 4 2 6 5 4 6 2 1 2 2 3 3 4