QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#118664 | #6629. Travelling Trader | kshitij_sodani# | 2 | 115ms | 58008kb | C++14 | 7.0kb | 2023-07-03 20:34:04 | 2024-05-31 18:54:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long llo;
#define endl '\n'
vector<llo> adj[200001];
llo it[200001];
llo par[200001];
pair<llo,llo> ma={-1,-1};
void dfs(llo no,llo par2=-1,llo levv=0){
par[no]=par2;
levv+=it[no];
ma=max(ma,{levv,no});
for(auto j:adj[no]){
if(j!=par2){
dfs(j,no,levv);
}
}
}
llo dp[200001][2][2];//start with root
llo dp2[200001][2];//start with direct child or root,end with root
vector<pair<pair<llo,llo>,llo>> adj2[200001][2][2];
vector<pair<pair<llo,llo>,llo>> adj3[200001][2];
void dfs2(llo no,llo par2=-1){
dp[no][0][0]=it[no];
adj2[no][0][0].pb({{no,-1},-1});
pair<llo,llo> ma={0,-1};
pair<llo,llo> ma2={0,-1};
dp[no][0][1]=it[no];
adj2[no][0][1].pb({{no,-1},-1});
dp[no][1][0]=it[no];
//dp[no][1][1]=0;
vector<pair<llo,llo>> aa;
vector<pair<llo,llo>> bb;
vector<pair<llo,llo>> cc;
vector<pair<llo,llo>> dd;
dp2[no][0]=it[no];
dp2[no][1]=it[no];
for(auto j:adj[no]){
if(j!=par2){
dfs2(j,no);
dp[no][0][1]+=it[j];
dp[no][1][0]+=it[j];
// dp[no][1][1]+=it[j];
dp2[no][0]+=it[j];
dp2[no][1]+=it[j];
ma=max(ma,{dp[j][1][0]-it[j],j});
ma2=max(ma2,{dp[j][0][1]-it[j],j});
//if(no>0){
aa.pb({dp[j][0][1]-it[j],j});
// }
bb.pb({dp[j][1][0]-it[j],j});
cc.pb({dp2[j][1]-it[j],j});
dd.pb({dp2[j][0]-it[j],j});
}
}
aa.pb({0,-1});
bb.pb({0,-1});
cc.pb({0,-1});
dd.pb({0,-1});
sort(aa.begin(),aa.end());
sort(bb.begin(),bb.end());
sort(cc.begin(),cc.end());
sort(dd.begin(),dd.end());
reverse(aa.begin(),aa.end());
reverse(bb.begin(),bb.end());
reverse(cc.begin(),cc.end());
reverse(dd.begin(),dd.end());
while(aa.size()>3){
aa.pop_back();
}
while(bb.size()>3){
bb.pop_back();
}
while(cc.size()>3){
cc.pop_back();
}
while(dd.size()>3){
dd.pop_back();
}
llo ma3=0;
for(auto i:aa){
for(auto j:bb){
for(auto k:cc){
map<llo,llo> ss;
ss[i.b]=i.a;
if(ss.find(j.b)!=ss.end()){
ss[j.b]=max(ss[j.b],j.a);
}
else{
ss[j.b]=j.a;
}
if(ss.find(k.b)!=ss.end()){
ss[k.b]=max(ss[k.b],k.a);
}
else{
ss[k.b]=k.a;
}
llo su=0;
for(auto jj:ss){
su+=jj.b;
}
ma3=max(ma3,su);
}
}
}
for(auto j:aa){
for(auto k:dd){
map<llo,llo> ss;
ss[j.b]=j.a;
if(ss.find(k.b)!=ss.end()){
ss[k.b]=max(ss[k.b],k.a);
}
else{
ss[k.b]=k.a;
}
llo su=0;
for(auto jj:ss){
su+=jj.b;
}
ma3=max(ma3,su);
}
}
llo ma4=0;
for(auto j:bb){
for(auto k:cc){
map<llo,llo> ss;
ss[j.b]=j.a;
if(ss.find(k.b)!=ss.end()){
ss[k.b]=max(ss[k.b],k.a);
}
else{
ss[k.b]=k.a;
}
llo su=0;
for(auto jj:ss){
su+=jj.b;
}
ma4=max(ma4,su);
}
}
llo st=0;
//0,1
//1,0
//end
llo x,y,z;
x=-1;
y=-1;
z=-1;
llo za=0;
for(auto i:aa){
for(auto j:bb){
for(auto k:cc){
map<llo,pair<llo,llo>> ss;
ss[i.b]={i.a,0};
if(ss.find(j.b)!=ss.end()){
ss[j.b]=max(ss[j.b],{j.a,1});
}
else{
ss[j.b]={j.a,1};
}
if(ss.find(k.b)!=ss.end()){
ss[k.b]=max(ss[k.b],{k.a,2});
}
else{
ss[k.b]={k.a,2};
}
llo su=0;
for(auto jj:ss){
su+=jj.b.a;
}
if(su==ma3 and st==0){
st=1;
for(auto jj:ss){
if(jj.b.b==0){
x=jj.a;
}
if(jj.b.b==1){
y=jj.a;
}
if(jj.b.b==2){
/*if(dp2[jj.a][1]>dp2[jj.a][0]){
za=1;
}*/
za=1;
z=jj.a;
}
}
}
}
}
}
for(auto j:aa){
for(auto k:dd){
map<llo,pair<llo,llo>> ss;
ss[j.b]={j.a,1};
if(ss.find(k.b)!=ss.end()){
ss[k.b]=max(ss[k.b],{k.a,2});
}
else{
ss[k.b]={k.a,2};
}
llo su=0;
for(auto jj:ss){
su+=jj.b.a;
}
if(su==ma3 and st==0){
st=1;
for(auto jj:ss){
if(jj.b.b==1){
x=jj.a;
}
if(jj.b.b==2){
za=0;
z=jj.a;
}
}
}
}
}
dp2[no][0]+=ma3;
dp[no][0][1]+=ma.a;
dp[no][1][0]+=ma2.a;
for(auto j:adj[no]){
if(j!=par2 and j!=x and j!=y and j!=z){
adj3[no][0].pb({{j,0},0});
}
}
if(x>=0){
adj3[no][0].pb({{x,0},1});
}
adj3[no][0].pb({{no,-1},-1});
if(y>=0){
adj3[no][0].pb({{y,1},0});
}
if(z>=0){
adj3[no][0].pb({{z,2},za});
}
llo st2=0;
y=-1;
z=-1;
x=-1;
za=0;
dp2[no][1]+=ma4;
if(dd[0].a+it[no]>dp2[no][1]){
dp2[no][1]=dd[0].a+it[no];
adj3[no][1].pb({{no,-1},-1});
if(dd[0].a>0){
adj3[no][1].pb({{dd[0].b,2},0});
}
}
else{
for(auto j:bb){
for(auto k:cc){
map<llo,pair<llo,llo>> ss;
ss[j.b]={j.a,1};
if(ss.find(k.b)!=ss.end()){
ss[k.b]=max(ss[k.b],{k.a,2});
}
else{
ss[k.b]={k.a,2};
}
llo su=0;
for(auto jj:ss){
su+=jj.b.a;
}
if(su==ma4 and st2==0){
st2=1;
for(auto jj:ss){
if(jj.b.b==1){
y=jj.a;
}
if(jj.b.b==2){
za=1;
z=jj.a;
}
}
}
}
}
/* if(st2==0){
adj3[no][1].pb({{no,-1},-1});
if(dd[0].a>0){
adj3[no][1].pb({{dd[0].b,2},0});
}
}
else{*/
adj3[no][1].pb({{no,-1},-1});
if(y>=0){
adj3[no][1].pb({{y,1},0});
}
for(auto j:adj[no]){
if(j!=par2 and j!=y and j!=z){
adj3[no][1].pb({{j,0},0});
}
}
if(z>=0){
adj3[no][1].pb({{z,2},za});
}
//}
}
if(ma.b>=0){
adj2[no][0][1].pb({{ma.b,1},0});
}
for(auto j:adj[no]){
if(j!=par2 and j!=ma.b){
adj2[no][0][1].pb({{j,0},0});
}
}
for(auto j:adj[no]){
if(j!=par2 and j!=ma2.b){
adj2[no][1][0].pb({{j,0},0});
}
}
if(ma2.b>=0){
adj2[no][1][0].pb({{ma2.b,0},1});
}
adj2[no][1][0].pb({{no,-1},-1});
}
vector<llo> fin;
void solve(llo no,llo x=-1,llo y=-1,llo tt=0){
if(tt==0){
for(auto j:adj3[no][y]){
if(j.a.b==-1){
fin.pb(j.a.a);
}
else if(j.a.b==2){
solve(j.a.a,-1,j.b,0);
}
else{
solve(j.a.a,j.a.b,j.b,1);
}
}
}
else{
for(auto j:adj2[no][x][y]){
if(j.a.b==-1){
fin.pb(j.a.a);
}
else if(j.a.b==2){
solve(j.a.a,-1,j.b,0);
}
else{
solve(j.a.a,j.a.b,j.b,1);
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
llo n,k;
cin>>n>>k;
for(llo i=0;i<n-1;i++){
llo aa,bb;
cin>>aa>>bb;
aa--;
bb--;
adj[aa].pb(bb);
adj[bb].pb(aa);
}
for(llo i=0;i<n;i++){
cin>>it[i];
}
if(k==1){
dfs(0);
cout<<ma.a<<endl;
llo cur=ma.b;
vector<llo> ans;
while(cur>=0){
ans.pb(cur);
cur=par[cur];
}
reverse(ans.begin(),ans.end());
cout<<ans.size()<<endl;
for(auto j:ans){
cout<<j+1<<" ";
}
cout<<endl;
return 0;
}
dfs2(0);
cout<<dp2[0][1]<<endl;
solve(0,2,1,0);
cout<<fin.size()<<endl;
for(auto j:fin){
cout<<j+1<<" ";
}
cout<<endl;
/* cout<<it[6]<<",,,,"<<endl;
cout<<dp[6][1][0]<<":::"<<endl;
cout<<dp2[2][0]<<"::"<<endl;
*/
return 0;
}
詳細信息
Subtask #1:
score: 2
Accepted
Test #1:
score: 2
Accepted
time: 3ms
memory: 40748kb
input:
2 1 1 2 255959470 961356354
output:
1217315824 2 1 2
result:
ok correct!
Test #2:
score: 0
Accepted
time: 3ms
memory: 42828kb
input:
1000 1 730 89 762 280 923 523 740 22 679 350 448 769 102 712 154 965 219 32 238 289 484 502 183 311 999 682 806 450 275 101 131 197 749 720 131 937 960 202 503 320 95 262 595 133 809 560 659 451 843 218 258 842 564 316 729 727 413 237 606 531 469 258 612 8 707 539 359 680 957 639 381 708 104 490 234...
output:
95535 17 1 173 449 472 396 545 668 270 981 489 852 836 763 6 218 843 758
result:
ok correct!
Test #3:
score: 0
Accepted
time: 57ms
memory: 50312kb
input:
200000 1 111811 133538 179217 151840 171974 117009 187613 169656 64662 13136 132434 89348 52802 175565 194821 191312 196047 99457 40339 152969 29669 180026 147677 57771 119598 58048 80707 146623 72232 101624 48109 11800 71536 69 31573 129 24409 17263 1033 148614 66977 149926 138624 87653 141889 1178...
output:
221 35 1 145832 90178 52464 3517 55709 39776 67451 59386 143855 102872 38865 13093 177086 7366 190908 160039 69864 196809 13257 612 171083 182883 14221 93359 52156 27994 103745 151704 138607 5346 14735 29598 89600 128747
result:
ok correct!
Test #4:
score: 0
Accepted
time: 70ms
memory: 48224kb
input:
200000 1 102636 78442 179388 84484 161437 35179 102313 154299 62439 71542 176361 125315 174129 186376 180886 54947 154823 114239 174647 112385 136495 187134 157035 96260 101101 192444 58209 23947 55494 191600 168007 162648 140149 73180 130665 180633 129328 67380 90262 134914 185905 104220 111321 154...
output:
21795891322 36 1 13557 199188 104317 71891 69787 1221 63258 191536 179446 83510 187880 124824 43888 83237 194602 59080 196038 195977 18490 43421 110298 60011 137785 140692 48345 68279 128780 198550 29394 56331 112092 192199 177180 16418 142142
result:
ok correct!
Test #5:
score: 0
Accepted
time: 79ms
memory: 50080kb
input:
200000 1 682 75953 92444 160568 113369 93705 154622 193304 149619 128186 104784 48913 131684 161196 25886 151867 89191 19511 99233 137377 104650 120096 64347 93526 111350 71598 7568 120116 123497 76821 25436 190715 99884 33654 109438 69462 165464 2475 163215 34551 33926 85078 101208 193355 50705 828...
output:
99327575017 197 1 178606 82034 53029 10508 21404 203 109187 121716 142023 3901 36728 9916 192913 18250 170199 113960 179753 163922 179588 31797 31645 183321 83207 13973 128176 38001 160968 9055 62809 168173 43933 187373 123795 114656 2192 193151 25062 141855 133596 155793 64049 57320 93903 33957 139...
result:
ok correct!
Test #6:
score: 0
Accepted
time: 61ms
memory: 48844kb
input:
200000 1 91999 92900 195726 58991 132067 99937 168188 152017 188495 19779 105961 45241 53406 75757 85118 170259 46250 47585 132248 8609 195110 32777 164307 95643 133411 739 170952 145623 19297 14414 171045 97619 74663 193421 139543 189434 36319 56453 77520 91164 91469 30021 128798 62259 183807 15271...
output:
9098893435 13 1 164355 56677 150505 174723 115829 88068 105453 199874 190131 165416 182628 114943
result:
ok correct!
Test #7:
score: 0
Accepted
time: 49ms
memory: 48112kb
input:
200000 1 189797 1 1 148138 1 95067 141831 1 168151 1 1 25692 95612 1 1 135979 1 141581 119622 1 1 131946 86508 1 98799 1 1 189104 1 117526 46338 1 1 166375 1 28026 165221 1 54204 1 1 98743 1 181414 1 34313 1 71302 1 161200 1 146339 1 47014 1 137258 1 57857 1 196493 1 99105 54487 1 104725 1 1 45203 1...
output:
1175349557 2 1 153544
result:
ok correct!
Test #8:
score: 0
Accepted
time: 53ms
memory: 47352kb
input:
199999 1 56367 178046 1 156890 170478 1 111308 177326 1 188427 1 90169 126610 1 161930 167698 96500 126424 118330 158517 186608 1 95505 107863 1 142887 72279 27494 1 114700 118535 1 68584 63156 97555 19966 39239 1 128030 1 1 86200 66974 1 34616 47100 173578 1 1 117279 89769 43412 1 89670 103573 1 13...
output:
2999144210 3 1 52552 129910
result:
ok correct!
Test #9:
score: 0
Accepted
time: 115ms
memory: 58008kb
input:
200000 1 95601 67789 174512 65854 198542 123861 153390 92355 141969 168754 177054 101194 25665 15524 131869 168080 171051 30732 97293 119758 103002 59019 141990 124310 161550 116618 2585 170410 132999 38200 99202 98733 73949 155033 144918 64086 1594 34916 37138 165382 13452 170885 136973 62178 15250...
output:
200000000000000 200000 1 47213 179780 132180 145558 41335 179095 156350 24912 104386 94658 54370 97034 108043 73905 141018 157563 199841 176455 147422 87545 190562 135095 24903 62484 36389 156106 45144 120321 4911 173474 102976 13602 68252 7332 141515 59337 182112 124040 38089 15458 161370 41901 144...
result:
ok correct!
Test #10:
score: 0
Accepted
time: 84ms
memory: 54604kb
input:
200000 1 122636 169264 76896 89915 72116 125306 186356 74852 84394 177419 21725 144848 106395 111991 189102 195804 6151 170169 185460 146764 6304 149801 147880 99539 6202 175326 104277 26515 39402 33436 116555 185545 44341 92305 197925 125286 28215 102176 182103 160554 105237 169007 105618 75618 190...
output:
49951940813408 100001 1 88700 18534 14218 21693 84470 150823 121376 192964 139616 11067 93019 188349 55336 13628 87630 31553 28945 29827 140175 179655 10038 38915 99968 89953 72978 102045 45280 176852 171879 100086 93399 183932 84482 111738 112608 136016 101850 19371 96135 54333 95939 2865 140685 13...
result:
ok correct!
Test #11:
score: 0
Accepted
time: 41ms
memory: 50548kb
input:
200000 1 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 8 16 8 17 9 18 9 19 10 20 10 21 11 22 11 23 12 24 12 25 13 26 13 27 14 28 14 29 15 30 15 31 16 32 16 33 17 34 17 35 18 36 18 37 19 38 19 39 20 40 20 41 21 42 21 43 22 44 22 45 23 46 23 47 24 48 24 49 25 50 25 51 26 52 26 53 27 54...
output:
13954593167 18 1 2 5 10 20 40 80 161 323 647 1295 2590 5181 10363 20727 41454 82908 165817
result:
ok correct!
Subtask #2:
score: 0
Wrong Answer
Test #12:
score: 7
Accepted
time: 3ms
memory: 42768kb
input:
2 2 2 1 243296356 635616793
output:
878913149 2 1 2
result:
ok correct!
Test #13:
score: 0
Accepted
time: 3ms
memory: 42736kb
input:
10 2 6 4 3 7 5 10 6 10 8 2 3 9 3 5 4 2 1 4 2 4 2 5 5 4 2 3 4 2
output:
33 10 1 4 10 3 9 7 5 6 2 8
result:
ok correct!
Test #14:
score: -7
Wrong Answer
time: 3ms
memory: 42816kb
input:
200 2 150 170 21 33 96 152 143 26 136 70 92 159 34 164 163 182 74 115 93 61 151 83 81 119 10 146 114 170 39 83 139 4 173 41 193 96 87 57 14 164 107 51 45 15 157 17 43 183 96 30 11 137 55 18 138 81 87 12 112 122 159 82 195 185 75 71 16 191 33 88 70 195 149 114 106 160 96 118 92 44 9 141 107 143 151 2...
output:
57201715916 98 1 151 179 194 89 83 135 27 112 40 125 180 120 117 122 99 72 33 96 105 131 114 171 28 110 149 170 59 193 21 88 162 94 138 45 129 25 78 62 199 36 127 15 12 53 70 76 159 44 24 178 17 41 67 173 186 42 116 92 82 197 101 5 32 121 87 29 198 64 93 19 141 8 126 37 100 3 9 108 52 61 54 14 137 4...
result:
wrong answer your claimed profit is 57201715916 but the profit of your plan is 57511291854
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #83:
score: 4
Accepted
time: 12ms
memory: 44156kb
input:
2000 3 1359 90 1703 163 158 188 360 1501 195 664 1414 215 1546 1756 536 1096 1726 1223 1150 104 1757 703 1982 282 1023 998 1180 419 576 1759 1496 1993 44 670 1703 952 855 849 1998 1399 1280 980 1533 1090 1270 678 1680 387 469 1734 1799 263 473 588 303 226 5 295 1489 1471 1094 1667 1912 210 1368 1360...
output:
1008611451196 2000 1 379 1954 1539 531 605 961 1091 1613 1300 540 377 1565 1823 1237 454 1101 99 864 359 1866 1840 1369 1617 562 867 1831 243 710 1040 901 1256 709 88 916 1341 1158 1526 1238 1291 129 745 260 1967 773 1919 816 523 674 1788 832 890 1626 297 452 1961 1903 1349 1808 1428 1337 739 560 16...
result:
ok correct!
Test #84:
score: 0
Accepted
time: 17ms
memory: 44124kb
input:
2000 3 1727 567 1783 1850 205 985 323 1094 1153 821 1756 117 377 1928 1026 1303 1343 1814 268 745 242 948 1140 1218 7 1675 101 1798 1403 1752 1184 671 87 248 1953 30 1580 1441 507 1438 525 419 901 421 1585 1405 1575 883 1952 1930 1988 1325 615 722 994 1202 178 474 1978 1500 899 481 216 409 999 1817 ...
output:
1012330476243 2000 1 369 1789 598 488 419 422 525 269 202 1079 694 1379 1454 1724 1545 88 246 1123 701 1522 1158 1696 1364 1918 131 1589 1832 872 1057 1532 345 1725 67 761 1634 1174 719 1807 650 693 61 718 554 841 234 1935 175 220 105 917 1784 997 1315 1530 92 257 802 1071 1369 1257 1046 1275 993 31...
result:
ok correct!
Test #85:
score: -4
Wrong Answer
time: 10ms
memory: 43680kb
input:
2000 3 1213 130 101 508 72 1199 1550 1096 1099 861 1515 627 1299 1672 1338 105 1444 1019 15 1560 1949 971 52 1312 30 529 186 1687 1917 484 1971 349 537 1223 1955 1377 300 1060 1786 1811 1960 90 1959 1353 1831 1548 303 511 1073 1197 863 1527 1379 994 31 9 1247 1707 1395 1532 29 1544 119 296 1919 1554...
output:
803960409885 1502 1 1365 1487 1721 810 1986 1821 668 513 1002 830 1255 1557 751 574 1802 1658 1491 60 1261 640 1010 374 1292 1450 1710 1896 942 718 1246 706 295 1377 1955 729 954 242 1809 93 1610 381 435 1127 863 384 1356 1067 181 1503 1169 1153 1997 1241 563 1475 1303 1310 1208 388 1069 1483 1181 8...
result:
wrong answer your profit is 803960409885 but jury's plan is 1001405462082, your plan is not optimal!
Subtask #6:
score: 0
Skipped
Dependency #5:
0%