QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#788717 | #7588. Monster Hunter | sunrise1024 | WA | 2ms | 7860kb | C++14 | 1.7kb | 2024-11-27 18:01:34 | 2024-11-27 18:01:35 |
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];
a[fid]+=a[no];
f[no]=fid;
if(fid!=1)q.emplace(fid,a[fid],b[fid]);
}
printf("%lld\n",a[1]);
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 7860kb
input:
1 4 2 6 5 4 6 2 1 2 2 3 3 4
output:
15
result:
wrong answer 1st numbers differ - expected: '3', found: '15'