QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#790201#7588. Monster Hunterkonata2828AC ✓437ms12372kbC++202.6kb2024-11-28 07:56:062024-11-28 07:56:07

Judging History

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

  • [2024-11-28 07:56:07]
  • 评测
  • 测评结果:AC
  • 用时:437ms
  • 内存:12372kb
  • [2024-11-28 07:56:06]
  • 提交

answer

// Hydro submission #6747b1939592d6097b871a0b@1732751764451
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>

#define N 100009
#define LL long long
#define pii pair<int,int>
using namespace std;
int n,idx=1,h[N],eg[N*2],ne[N*2],fa[N],die[N];
int tim[N];
LL a[N],b[N];
struct node1{
    LL a,b;
    int t,id;
    bool operator<(const node1 c)const{
        return a>c.a;
    }
};
struct node2{
    LL a,b;
    int t,id;
    bool operator<(const node2 c)const{
        return b<c.b;
    }
};
priority_queue<node1> q1;
priority_queue<node2> q2;
void add(int a,int b){
    ++idx,eg[idx]=b,ne[idx]=h[a],h[a]=idx;
}
void dfs(int u){
    for(int y,i=h[u];i;i=ne[i]){
        y=eg[i];
        if(y==die[u]) continue;
        die[y]=u;
        dfs(y);
    }
}
int find(int u){
    if(fa[u]==u) return u;
    return fa[u]=find(fa[u]);
}
void solve(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) fa[i]=i,tim[i]=h[i]=0;
    a[1]=b[1]=0;
    idx=1;
    for(int i=2;i<=n;i++) scanf("%lld%lld",&a[i],&b[i]);
    for(int a,b,i=1;i<n;i++){
        scanf("%d%d",&a,&b);
        add(a,b);
        add(b,a);
    }
    dfs(1);
    for(int i=2;i<=n;i++){
        if(a[i]<=b[i]) q1.push({a[i],b[i],0,i});
        else q2.push({a[i],b[i],0,i});
    }
    int tot=0;
    while(q1.size() || q2.size()){
        if(q1.size()){
            node1 u=q1.top();
            q1.pop();
            if(u.t<tim[u.id]) continue;
            int y=find(u.id);
            if(y>1){
                int t=find(die[y]);
                fa[y]=t;
                LL mb=-a[t]+b[t]-a[y]+b[y];
                a[t]=-min(-a[t],-a[t]+b[t]-a[y]);
                b[t]=mb+a[t];
                tim[t]=++tot;
                if(a[t]<=b[t]) q1.push({a[t],b[t],tot,t});
                else q2.push({a[t],b[t],tot,t});
            }
        }else{
            node2 u=q2.top();
            q2.pop();
            if(u.t<tim[u.id]) continue;
            int y=find(u.id);
            if(y>1){
                int t=find(die[y]);
                fa[y]=t;
                LL mb=-a[t]+b[t]-a[y]+b[y];
                a[t]=-min(-a[t],-a[t]+b[t]-a[y]);
                b[t]=mb+a[t];
                tim[t]=++tot;
                if(a[t]<=b[t]) q1.push({a[t],b[t],tot,t});
                else q2.push({a[t],b[t],tot,t});
            }
        }
    }
    printf("%lld\n",a[1]);
}
int main() {
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++) solve();
    // freopen("tree.in","r",stdin);
    // freopen("tree.out","w",stdout);
    
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 5860kb

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: 0
Accepted
time: 401ms
memory: 12372kb

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:

63495498
541156749
191617241
177090559
383609891
27231993394
19064806867
37194996614
84584890
84657366

result:

ok 10 numbers

Test #3:

score: 0
Accepted
time: 437ms
memory: 12364kb

input:

1109
100
49335125 540429
21327621 30666761
84726139 32583318
62343520 86386375
68371125 80203961
11703807 57711659
4086043 20964787
71845269 8258000
70046842 34257600
86312745 39362281
92327731 31351601
27782858 53379001
51945765 34797724
7197540 99710490
43627335 61003907
256869 8469838
89150549 45...

output:

74012350
485460189
68654921
22821963
702398949
944191
63683983
49851540
539751698
378185590
17649578
153086960
85518043
218934054
40886105
365498548
568650050
115493085
13675680
20327516
19299519
47328093
101160112
314233771
7506569
20797884
140632255
699457438
11257338
71975351
699154224
597663853
...

result:

ok 1109 numbers