QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#177176 | #5092. 森林游戏 | Star32 | 100 ✓ | 3ms | 22336kb | C++14 | 1.1kb | 2023-09-12 17:00:22 | 2023-09-12 17:00:23 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,val[200005],cnt,lst[200005],tot;
struct edge{
int f,t,lst;
edge(int f=0,int t=0,int lst=0):
f(f),t(t),lst(lst){}
}e[400005];
void add(int u,int v){
e[++cnt]=edge(u,v,lst[u]);lst[u]=cnt;
e[++cnt]=edge(v,u,lst[v]);lst[v]=cnt;
}
priority_queue<int> q[200005];
void dfs(const int u,const int fa){
for(int i=lst[u];i;i=e[i].lst){
int v=e[i].t;
if(v==fa)continue;
dfs(v,u);
if(q[u].size()<q[v].size())swap(q[u],q[v]);
while(!q[v].empty())q[u].push(q[v].top()),q[v].pop();
}
int op=1,now=val[u];
while(!q[u].empty()){
if(op==1&&now>=q[u].top())break;
now+=-op*q[u].top();
q[u].pop();op*=-1;
}
if(op==-1){
if(n&1)tot-=now;
else tot+=now;
}
else q[u].push(now);
}
signed main(){
cin>>n;
for(int i=1;i<=n;i++)scanf("%lld",&val[i]),tot+=val[i];
for(int i=1;i<n;i++){
int u,v;scanf("%lld%lld",&u,&v);
add(u,v);
}
dfs(1,0);
int now=1;
while(!q[1].empty()){
tot+=now*q[1].top();
q[1].pop(),now*=-1;
}
cout<<tot/2;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 20
Accepted
Test #1:
score: 20
Accepted
time: 0ms
memory: 20672kb
input:
20 530933472 920863930 136179483 46535289 291417568 338674057 731327836 375973098 986104110 203163644 489326483 785212564 712578139 801609721 666347686 282530991 823910542 217884304 785578553 116701831 8 3 18 19 11 20 2 18 8 13 5 15 12 16 7 8 10 9 13 6 17 10 17 18 13 15 18 4 13 12 1 14 17 15 15 14 5...
output:
4611098449
result:
ok 1 number(s): "4611098449"
Test #2:
score: 0
Accepted
time: 3ms
memory: 20532kb
input:
20 384529189 217795442 901954855 742992564 354875060 497288585 40376145 575315239 263212445 574630916 520470316 917128880 461333242 407666839 565926566 390970156 568486150 291329847 493439854 637783217 10 17 19 8 20 17 7 17 9 6 17 14 16 18 4 3 9 14 6 2 14 18 13 4 11 15 9 3 7 12 16 1 8 14 5 14 9 15
output:
4410782058
result:
ok 1 number(s): "4410782058"
Test #3:
score: 0
Accepted
time: 0ms
memory: 21204kb
input:
19 601996737 696498327 385657564 527861058 529330573 376647612 223077352 338754937 682264670 671903443 645156487 755321105 978945836 120368247 611275923 947566064 98892994 858404931 401419272 11 14 8 14 3 6 13 12 16 15 1 4 11 4 2 7 4 3 15 5 2 1 13 5 10 16 9 16 1 17 18 6 3 19 5 7
output:
5848746543
result:
ok 1 number(s): "5848746543"
Subtask #2:
score: 20
Accepted
Dependency #1:
100%
Accepted
Test #4:
score: 20
Accepted
time: 0ms
memory: 21252kb
input:
1000 379354617 199235046 102686848 841958378 178689385 896777301 798019293 538540178 877449071 825184825 426375184 882476194 614078703 438824267 731949932 770345838 655572525 687098129 758287148 690715403 984914877 365231609 752380073 509777049 511312331 889780846 745450511 740863321 552773286 15071...
output:
252915929540
result:
ok 1 number(s): "252915929540"
Test #5:
score: 0
Accepted
time: 0ms
memory: 22336kb
input:
999 402468827 468124822 806603931 945584323 480102625 19722 169790452 515757998 19958108 528988204 415841643 111279190 885615782 740479752 550815713 556065700 511850711 148616044 552183365 471481958 946651162 968386282 610171866 971986310 377716133 564442897 222395758 16053401 613716892 487668831 48...
output:
249829986618
result:
ok 1 number(s): "249829986618"
Test #6:
score: 0
Accepted
time: 2ms
memory: 21784kb
input:
1000 637313417 62510663 83128345 702987667 374713776 778401168 619811617 896369345 486498325 448824176 297332011 916674955 895413407 803990450 21693588 866905388 169932411 340234047 259184127 331450756 506720779 194809709 896076578 936050255 925430496 741889050 638981221 628789074 358540287 89791215...
output:
226446817254
result:
ok 1 number(s): "226446817254"
Test #7:
score: 0
Accepted
time: 0ms
memory: 21928kb
input:
1000 147397255 520346638 619331018 431963137 427476094 995854324 640680706 473132459 1061281 701469953 87766202 316181634 506906587 181791922 174614534 639267313 489608085 693127159 412762229 906080005 562083964 319379642 148511208 32431198 908725162 531631359 390987308 965197199 851576914 95327968 ...
output:
229874635771
result:
ok 1 number(s): "229874635771"
Test #8:
score: 0
Accepted
time: 0ms
memory: 21308kb
input:
999 98690497 488296773 570509174 87593828 566189245 345232397 309963286 886305731 195509167 561473976 304758565 239428403 9228891 38443063 343536428 606219587 806177708 94853421 426729965 393545389 820769556 365126116 489908193 276668395 217091957 976510596 415255517 670037302 793997764 947495706 10...
output:
266569042607
result:
ok 1 number(s): "266569042607"