QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#788719 | #7588. Monster Hunter | sunrise1024 | WA | 541ms | 17536kb | C++14 | 1.7kb | 2024-11-27 18:02:02 | 2024-11-27 18:02:02 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1e5+5;
int T;
int n;
ll a[N],b[N];
vector<int> e[N];
int fa[N];
void dfs(int no,int ff){
fa[no]=ff;
for(int to:e[no]){
if(to==ff)continue;
dfs(to,no);
}
}
struct node{
int id;
ll a,b;
bool fl;
node()=default;
node(int id,ll a,ll b):id(id),a(a),b(b),fl(b>=0){}
bool operator<(const node& y)const{
if(fl!=y.fl)return fl<y.fl;
if(fl)return a>y.a;
return b<y.b;
}
};
priority_queue<node> q;
int f[N];
bool fl[N];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=1;i<=n;++i){
a[i]=b[i]=0;
e[i].clear();
fa[i]=0;
fl[i]=0;
}
for(int i=2;i<=n;++i)scanf("%lld%lld",&a[i],&b[i]);
for(int i=1;i<n;++i){
int u,v;
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
for(int i=1;i<=n;++i)f[i]=i;
dfs(1,0);
for(int i=2;i<=n;++i){
b[i]-=a[i];
q.emplace(i,a[i],b[i]);
}
while(!q.empty()){
int no=q.top().id;
ll lsa=q.top().a,lsb=q.top().b;
q.pop();
if(lsa!=a[no]||lsb!=b[no])continue;
if(fl[no])continue;
fl[no]=1;
int fid=find(fa[no]);
a[fid]=max(a[fid],a[no]-b[fid]);
b[fid]+=b[no];
f[no]=fid;
if(fid!=1)q.emplace(fid,a[fid],b[fid]);
}
printf("%lld\n",a[1]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7912kb
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
Wrong Answer
time: 541ms
memory: 17536kb
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:
63548062 541246690 191617241 177090559 383609891 27352232431 19194364588 37967885071 84584890 84657366
result:
wrong answer 1st numbers differ - expected: '63495498', found: '63548062'