QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#788235#7588. Monster Hunterkonata2828RE 0ms0kbC++171.5kb2024-11-27 16:19:172024-11-27 16:19:18

Judging History

你现在查看的是最新测评结果

  • [2024-11-27 16:19:18]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-27 16:19:17]
  • 提交

answer

// Hydro submission #6746d6039592d6097b8682b5@1732695555854
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1e5+5;
int n;
ll a[N],b[N];
ll be[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(a<=b){}
    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(){
    freopen("tree.in","r",stdin);
    freopen("tree.out","w",stdout);
    scanf("%d",&n);
    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){
        be[i]=a[i];
        q.emplace(i,a[i],b[i]);
    }
    while(!q.empty()){
        int no=q.top().id;
        q.pop();
        if(fl[no])continue;
        fl[no]=1;
        int fid=find(fa[no]);
        be[fid]=max(be[fid],be[no]-(b[fid]-a[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",be[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

output:


result: