QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#788005 | #7588. Monster Hunter | KFC | RE | 0ms | 0kb | C++20 | 2.0kb | 2024-11-27 15:32:40 | 2024-11-27 15:33:02 |
answer
// Hydro submission #6746cb169592d6097b8668cf@1732692759037
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
vector<int> e[100005];
int a[100005],b[100005];
namespace union_find
{
int fa[100005];
void init(int n){for(int i=1;i<=n;i++)fa[i]=i;}
int root(int x){return (fa[x]==x?x:fa[x]=root(fa[x]));}
void merge(int u,int v){u=root(u),v=root(v),fa[u]=v;}
};
int fa[100005];
void dfs(int pos,int fa)
{
::fa[pos]=fa;
for(int x:e[pos])
{
if(x==fa)
continue;
dfs(x,pos);
}
}
bool f[100005];
struct node
{
int id,a,b;
bool operator < (const node x) const
{
bool f1=(b>=0);
bool f2=(x.b>=0);
if(!f1&&f2)
return 1;
if(f1&&!f2)
return 0;
if(f1&&f2)
return a>x.a;
else
return b+a<x.b+x.a;
}
};
void test()
{
cin>>n;
for(int i=2;i<=n;i++)
cin>>a[i]>>b[i],f[i]=0,b[i]-=a[i];
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
e[u].push_back(v);
e[v].push_back(u);
}
union_find::init(n);
dfs(1,0);
priority_queue<node> q;
for(int i=2;i<=n;i++)
q.push({i,a[i],b[i]});
while(!q.empty())
{
node x=q.top();
q.pop();
if(a[x.id]!=x.a||b[x.id]!=x.b||f[x.id])
continue;
// cout<<x.id<<" "<<x.a<<" "<<x.b<<" "<<union_find::root(fa[x.id])<<"\n";
int rt=union_find::root(fa[x.id]);
f[x.id]=1;
a[rt]=max(a[rt],a[x.id]-b[rt]);
b[rt]=b[rt]+b[x.id];
if(rt!=1)
q.push({rt,a[rt],b[rt]});
union_find::merge(x.id,rt);
}
cout<<a[1]<<"\n";
}
signed main()
{
freopen("tree.in","r",stdin);
freopen("tree.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
test();
}
/*
5
1 3
3 2
1 2
9 5
2 1
3 1
4 2
5 2
*/
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