QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#651363 | #6438. Crystalfly | DING | AC ✓ | 1250ms | 20072kb | C++23 | 1.7kb | 2024-10-18 17:39:21 | 2024-10-18 17:39:25 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define N 100007
using namespace std;
int t[N],a[N];
//链式前向星
int e[N*2],ne[N*2],h[N],idx;//点数开两倍
int f[N],g[N];
void add(int u,int v){
e[idx]=v;ne[idx]=h[u],h[u]=idx++;
}
void dfs(int u,int fa){
g[u]=a[u];//这个根一定是会取到的
int max1=0,max2=0;
for(int i=h[u];~i;i=ne[i]){
int j=e[i];
if(j==fa)continue;
dfs(j,u);
g[u]+=f[j]-a[j];
//提前处理出来当前根对应的两个最大叶子贡献
//因为g[u]里面包含了所有j(u的叶子)的叶子结点及以下的所有元素,所以求max1和max2时要
if(g[j]-(f[j]-a[j])>max1){max2=max1;max1=g[j]-(f[j]-a[j]);}
else if(g[j]-(f[j]-a[j])>max2)max2=g[j]-(f[j]-a[j]);
}
f[u]=g[u];//因为叶子结点的存在,所以必须有这一步
for(int i=h[u];~i;i=ne[i]){
int j=e[i];
if(j==fa)continue;
f[u]=max(f[u],g[u]+a[j]);
if(t[j]==3){
if(g[j]-(f[j]-a[j])==max1)f[u]=max(f[u],max2+g[u]+a[j]);
else f[u]=max(f[u],max1+g[u]+a[j]);
}
}
}
void solve(){
memset(h,-1,sizeof h);
// memset(t,0,sizeof t);
// memset(a,0,sizeof a);
// memset(e,0,sizeof e);
// memset(ne,0,sizeof ne);
idx=0;
int n;cin>>n;
for(int i=1;i<=n;i++)cin>>a[i],f[i]=0,g[i]=0;
for(int i=1;i<=n;i++)cin>>t[i];
for(int i=1;i<=n-1;i++){
int u,v;cin>>u>>v;
add(u,v);
add(v,u);
}
dfs(1,-1);//当前和父节点
cout<<f[1]<<'\n';
}
signed main(){
fast;int t;cin>>t;while(t--){solve();} return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9748kb
input:
2 5 1 10 100 1000 10000 1 2 1 1 1 1 2 1 3 2 4 2 5 5 1 10 100 1000 10000 1 3 1 1 1 1 2 1 3 2 4 2 5
output:
10101 10111
result:
ok 2 number(s): "10101 10111"
Test #2:
score: 0
Accepted
time: 2ms
memory: 7776kb
input:
10 6 8 1 1 5 8 9 2 1 2 2 2 2 1 2 2 3 2 4 1 5 2 6 6 6 4 4 1 3 6 2 1 3 3 3 3 1 2 1 3 3 4 4 5 5 6 6 10 5 1 8 5 1 1 3 1 2 2 2 1 2 2 3 2 4 2 5 3 6 10 6 8 8 9 6 9 5 6 6 4 2 1 3 3 2 2 2 2 3 1 1 2 1 3 3 4 4 5 5 6 4 7 2 8 7 9 9 10 7 10 9 1 5 7 5 4 1 1 1 2 1 3 2 1 2 1 3 3 4 3 5 5 6 1 7 5 7 1 1 4 2 3 1 3 2 2 1...
output:
25 24 24 56 31 14 16 28 19 19
result:
ok 10 numbers
Test #3:
score: 0
Accepted
time: 1250ms
memory: 9764kb
input:
100000 10 9 1 7 9 5 1 10 5 3 8 2 1 1 3 1 2 2 3 3 1 1 2 2 3 3 4 1 5 2 6 2 7 6 8 7 9 7 10 3 6 6 1 2 1 2 1 2 1 3 10 6 5 3 7 1 5 1 9 7 3 3 1 3 3 1 3 2 2 2 3 1 2 1 3 3 4 4 5 2 6 6 7 4 8 7 9 1 10 7 2 8 9 7 7 9 10 2 3 2 2 3 2 1 1 2 2 3 1 4 3 5 4 6 3 7 1 8 1 1 4 2 7 9 9 9 8 4 2 7 3 1 2 1 1 1 1 1 2 2 3 2 4 3...
output:
49 12 41 45 8 4 38 22 20 21 5 19 23 44 26 5 21 28 28 32 36 15 5 26 38 36 20 35 27 36 20 9 32 32 22 11 41 15 20 54 38 20 45 36 20 29 24 4 37 44 30 45 17 17 36 29 3 6 24 44 25 28 50 13 5 1 44 27 17 21 15 17 17 24 29 39 10 39 38 26 22 24 9 17 41 7 28 33 51 18 14 14 7 35 23 13 11 43 30 24 35 2 43 33 17 ...
result:
ok 100000 numbers
Test #4:
score: 0
Accepted
time: 89ms
memory: 7888kb
input:
1000 420 4 7 10 4 6 9 7 9 3 5 3 10 7 2 8 4 4 7 9 4 3 7 1 10 1 5 4 5 3 9 6 4 2 8 7 1 1 10 2 2 2 4 2 1 3 2 4 8 2 1 6 3 2 5 4 9 9 9 5 9 5 2 2 5 4 2 10 9 1 10 7 4 8 4 10 10 6 2 10 2 3 2 6 2 3 3 2 9 8 5 3 1 6 3 4 5 6 1 7 6 10 7 7 8 2 6 10 10 9 8 6 2 9 7 7 10 4 5 9 2 10 9 9 10 1 5 1 6 1 1 4 8 2 5 7 7 4 10...
output:
1633 2047 3251 576 3701 2231 700 2254 105 1518 3179 914 1396 2417 2019 3397 3013 3659 443 2773 3354 110 3445 888 2380 1525 2881 1841 1969 810 752 1026 1794 3273 3021 2011 3307 3832 95 51 3731 1374 2842 1675 1960 1118 431 2199 2747 3882 1305 971 1490 3028 908 73 2376 1341 1821 3565 721 3195 2388 266 ...
result:
ok 1000 numbers
Test #5:
score: 0
Accepted
time: 211ms
memory: 10704kb
input:
10 99991 10 6 5 5 7 1 4 5 2 3 9 5 4 9 2 10 8 3 6 9 4 4 4 2 10 8 8 4 8 5 10 6 8 9 2 5 6 10 9 8 2 1 2 6 9 10 7 1 8 1 8 3 5 3 9 8 2 8 1 3 7 3 5 3 1 10 8 1 3 10 6 7 5 3 10 6 10 6 5 5 2 5 1 8 4 4 8 6 8 7 4 1 3 1 10 2 3 5 2 6 10 4 1 6 1 10 5 4 2 8 8 6 9 6 5 8 10 8 2 4 8 2 2 7 9 5 7 8 9 2 9 10 3 9 1 9 6 7 ...
output:
385673 385638 385999 385351 383937 385702 384372 386280 385985 383976
result:
ok 10 numbers
Test #6:
score: 0
Accepted
time: 232ms
memory: 10652kb
input:
10 100000 691638189 376118101 486584606 394669567 486373897 939526687 503807724 278251188 231412672 45682405 745048495 28500413 156838889 12700279 797455152 755903587 326532893 701805548 78389204 486114025 367059077 361684203 829984938 129623201 460608105 143355017 412267713 65576852 778434431 93425...
output:
35466839477975 35514425587777 35461993022217 35475724605159 35564188763634 35640546302274 35641995513963 35686586117287 35536376359863 35605912922111
result:
ok 10 numbers
Test #7:
score: 0
Accepted
time: 201ms
memory: 20072kb
input:
10 99995 84858301 147496073 555948385 755493899 790427326 752195181 302302440 279635456 464535302 606782338 170553531 791509094 500796960 353102692 659801138 547244567 807600470 547009591 831176050 525406957 885722892 446683877 240520416 817746097 580155412 595574043 942015203 24512946 378725451 395...
output:
49789565036402 49906949178594 49939507193075 50150069484316 49922940531510 50138902547182 49988715524002 50084859317301 50155505003722 50051981684634
result:
ok 10 numbers
Test #8:
score: 0
Accepted
time: 189ms
memory: 15332kb
input:
10 99999 457859907 940473195 693318692 12318672 13733365 917623091 458582505 208500503 914054213 679912356 928272558 655395186 826512890 459725698 702695255 748285967 869848527 540446227 965631810 771687238 506164324 540163930 354152058 821643041 578233412 673793657 965135940 121043329 258633321 872...
output:
24975379263129 24903857589285 24946010682571 25113930005155 25038661368497 24937493169990 25073187919093 24952572373741 24930855990405 25025151559800
result:
ok 10 numbers