QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#347801 | #6629. Travelling Trader | ucup-team004 | 2 | 81ms | 41200kb | C++20 | 5.3kb | 2024-03-09 15:30:45 | 2024-03-09 15:30:46 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, K;
std::cin >> N >> K;
std::vector<std::vector<int>> adj(N);
for (int i = 1; i < N; i++) {
int u, v;
std::cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
std::vector<int> p(N);
for (int i = 0; i < N; i++) {
std::cin >> p[i];
}
std::vector<int> path;
i64 ans = 0;
if (K == 1) {
std::vector<i64> sum(N);
std::vector<int> par(N);
par[0] = -1;
auto dfs = [&](auto self, int x) -> void {
if (par[x] != -1) {
adj[x].erase(std::find(adj[x].begin(), adj[x].end(), par[x]));
}
sum[x] += p[x];
for (auto y : adj[x]) {
par[y] = x;
sum[y] = sum[x];
self(self, y);
}
};
dfs(dfs, 0);
int x = std::max_element(sum.begin(), sum.end()) - sum.begin();
ans = sum[x];
for (int i = x; i != -1; i = par[i]) {
path.push_back(i);
}
std::reverse(path.begin(), path.end());
} else if (K == 2) {
std::vector<i64> dp1(N), dp2(N), dp3(N);
std::vector<int> par(N);
par[0] = -1;
auto dfs = [&](auto self, int x, int option) -> void {
if (option == 0) {
if (par[x] != -1) {
adj[x].erase(std::find(adj[x].begin(), adj[x].end(), par[x]));
}
for (auto y : adj[x]) {
par[y] = x;
self(self, y, 0);
}
}
i64 sum = 0;
std::pair<i64, int> mx1[2] {{0, -1}, {0, -2}};
std::pair<i64, int> mx2[2] {{0, -3}, {0, -4}};
std::pair<i64, int> mx3[2] {{0, -5}, {0, -6}};
for (auto y : adj[x]) {
sum += p[y];
std::pair<i64, int> v {dp1[y] - p[y], y};
for (int i = 0; i < 2; i++) {
if (v > mx1[i]) {
std::swap(v, mx1[i]);
}
}
v = {dp2[y] - p[y], y};
for (int i = 0; i < 2; i++) {
if (v > mx2[i]) {
std::swap(v, mx2[i]);
}
}
v = {dp3[y] - p[y], y};
for (int i = 0; i < 2; i++) {
if (v > mx3[i]) {
std::swap(v, mx3[i]);
}
}
}
if (option == 0) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (mx1[i].second != mx2[j].second) {
dp1[x] = std::max(dp1[x], sum + mx1[i].first + mx2[j].first + p[x]);
}
}
}
dp2[x] = std::max(dp2[x], sum + mx3[0].first + p[x]);
dp3[x] = std::max(dp3[x], sum + mx2[0].first + p[x]);
} else if (option == 1) {
path.push_back(x);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (mx1[i].second != mx2[j].second) {
if (dp1[x] == sum + mx1[i].first + mx2[j].first + p[x]) {
if (mx2[j].second >= 0) {
self(self, mx2[j].second, 2);
}
for (auto y : adj[x]) {
if (y != mx1[i].second && y != mx2[j].second) {
path.push_back(y);
}
}
if (mx1[i].second >= 0) {
self(self, mx1[i].second, 1);
}
return;
}
}
}
}
} else if (option == 2) {
for (auto y : adj[x]) {
if (y != mx3[0].second) {
path.push_back(y);
}
}
if (mx3[0].second >= 0) {
self(self, mx3[0].second, 3);
}
path.push_back(x);
} else if (option == 3) {
path.push_back(x);
if (mx2[0].second >= 0) {
self(self, mx2[0].second, 2);
}
for (auto y : adj[x]) {
if (y != mx2[0].second) {
path.push_back(y);
}
}
}
};
dfs(dfs, 0, 0);
ans = dp1[0];
dfs(dfs, 0, 1);
} else {
}
std::cout << ans << "\n";
std::cout << path.size() << "\n";
for (auto x : path) {
std::cout << x + 1 << " \n"[x == path.back()];
}
return 0;
}
详细
Subtask #1:
score: 2
Accepted
Test #1:
score: 2
Accepted
time: 0ms
memory: 3652kb
input:
2 1 1 2 255959470 961356354
output:
1217315824 2 1 2
result:
ok correct!
Test #2:
score: 0
Accepted
time: 0ms
memory: 3724kb
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: 59ms
memory: 17620kb
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: 59ms
memory: 17544kb
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: 60ms
memory: 17252kb
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: 55ms
memory: 18196kb
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: 37ms
memory: 18084kb
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: 47ms
memory: 17484kb
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: 71ms
memory: 41200kb
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: 81ms
memory: 29448kb
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: 48ms
memory: 17248kb
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: 0ms
memory: 3892kb
input:
2 2 2 1 243296356 635616793
output:
878913149 2 1 2
result:
ok correct!
Test #13:
score: 0
Accepted
time: 0ms
memory: 3588kb
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 8 2 6 10 5 3 7 9
result:
ok correct!
Test #14:
score: -7
Wrong Answer
time: 0ms
memory: 3608kb
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:
54153356810 91 1 151 179 194 89 83 135 27 122 117 120 180 125 112 40 45 21 88 162 94 138 193 170 59 149 110 28 171 114 96 105 131 33 15 99 72 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 49 26 47 165 50 148 75 12...
result:
wrong answer your profit is 54153356810 but jury's plan is 57921623677, your plan is not optimal!
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: 0
Wrong Answer
time: 1ms
memory: 3656kb
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:
0 0
result:
wrong answer Integer 0 violates the range [1, 2000]
Subtask #6:
score: 0
Skipped
Dependency #5:
0%