QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#788119 | #7588. Monster Hunter | KFC | RE | 0ms | 0kb | C++14 | 1.2kb | 2024-11-27 15:59:28 | 2024-11-27 15:59:42 |
answer
// Hydro submission #6746d15d9592d6097b8675e2@1732694366071
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
vector<int>g[N];
int a[N], b[N], fa[N], f[N];
int find(int x){return x == f[x] ? x : f[x] = find(f[x]);}
void dfs(int x, int f)
{
fa[x] = f;
for(auto j:g[x]) if(j!=f) dfs(j,x);
}
struct ab{int a,b,id;};
bool operator < (const ab &a, const ab &b)
{
int va = (a.a <= a.b), vb = (b.a <= b.b);
if(va!=vb) return va < vb;
if(!va) return a.b < b.b;
return a.a > b.a;
}
priority_queue<ab>pq;
signed main()
{
freopen("tree.in","r",stdin);
freopen("tree.out","w",stdout);
int n, i, u, v;
scanf("%lld", &n);
for(i=1;i<=n;i++) f[i] = i;
for(i=2;i<=n;i++) scanf("%lld%lld", &a[i], &b[i]);
for(i=1;i<n;i++) scanf("%lld%lld", &u, &v), g[u].emplace_back(v), g[v].emplace_back(u);
dfs(1,0);
for(i=2;i<=n;i++) pq.push({a[i],b[i],i});
while(!pq.empty())
{
ab t = pq.top();pq.pop();
int d = t.id;
if(t.a!=a[d]||t.b!=b[d]) continue;
int f = find(fa[d]);
int ab = b[f] + b[d] - a[f] - a[d];
a[f] = max(a[f],a[f]-b[f]+a[d]), b[f] = a[f] + ab;
if(f!=1) pq.push({a[f],b[f],f});
::f[d] = f;
}
printf("%lld", a[1]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
1 4 2 6 5 4 6 2 1 2 2 3 3 4