QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#311832 | #6629. Travelling Trader | oscaryang | 0 | 3ms | 8736kb | C++14 | 2.0kb | 2024-01-22 20:49:23 | 2024-01-22 20:49:23 |
answer
#include<bits/stdc++.h>
#define vc vector
#define pb emplace_back
#define pii pair<int, int>
#define mkp make_pair
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define lep(i, a, b) for(int i = (a); i >= (b); --i)
#define int long long
using namespace std;
inline int read() {
int x = 0, w = 0; char ch = getchar(); while(!isdigit(ch)) w |= (ch == '-'), ch = getchar();
while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return w ? -x : x;
}
const int N = 2e5 + 5;
int n, k, ans, pos, fa[N], a[N], nxt[N], f[N], g[N];
vc<int> G[N], chain, path;
inline void dfs1(int x) {
int sum = a[x], u = 0;
for(auto y : G[x]) if(y != fa[x]) {
fa[y] = x; dfs1(y); sum += a[y]; u = 0;
for(auto z : G[y]) if(z != x && f[z] > f[u]) u = z;
if(u && f[u] > f[x]) g[x] = f[x], f[x] = f[u], nxt[x] = y;
else if(u && f[u] > g[x]) g[x] = f[u];
}
f[x] += sum; g[x] += sum;
}
inline void dfs2(int x, int tot) {
if(tot + a[x] > ans) ans = tot + a[x], pos = x;
for(auto y : G[x]) if(y != fa[x]) {
int val = nxt[x] == y ? g[x] : f[x];
dfs2(y, tot + val - a[y]);
}
}
inline void construct(int x) {
path.pb(x);
if(nxt[x]) construct(nxt[x]);
for(auto y : G[x]) if(y != fa[x]) path.pb(y);
}
signed main() {
n = read(); k = read();
for(int i = 1, u, v; i < n; i++)
u = read(), v = read(), G[u].pb(v), G[v].pb(u);
rep(i, 1, n) a[i] = read();
dfs1(1); dfs2(1, 0);
cout << ans << endl;
for(int x = pos; x; x = fa[x]) chain.pb(x);
reverse(chain.begin(), chain.end()); chain.pb(0);
path.pb(1);
for(int i = 0, x, y, u; i + 1 < chain.size(); i++) {
x = chain[i]; y = chain[i + 1]; u = 0;
for(auto z : G[x]) if(z != y && z != fa[x])
for(auto v : G[z]) if(v != fa[z] && f[v] > f[u]) u = v;
if(u) construct(u);
for(auto z : G[x]) if(z != y && z != fa[x]) path.pb(z);
if(y) path.pb(y);
}
printf("%lld\n", (int)path.size());
for(auto u : path) printf("%lld ", u); putchar(10);
return 0;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 2
Accepted
time: 0ms
memory: 8556kb
input:
2 1 1 2 255959470 961356354
output:
1217315824 2 1 2
result:
ok correct!
Test #2:
score: -2
Wrong Answer
time: 3ms
memory: 8592kb
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:
1002345 267 1 929 264 819 173 858 728 704 418 961 471 418 392 642 704 391 902 654 728 656 511 283 449 410 169 368 896 694 472 921 103 540 185 470 505 874 185 540 778 824 89 69 103 358 202 747 437 396 42 96 975 611 289 108 344 889 991 30 816 991 889 492 834 959 518 344 238 815 432 108 561 20 551 13 2...
result:
wrong answer dist(1, 929) = 2 > k = 1
Subtask #2:
score: 0
Wrong Answer
Test #12:
score: 7
Accepted
time: 0ms
memory: 8544kb
input:
2 2 2 1 243296356 635616793
output:
878913149 2 1 2
result:
ok correct!
Test #13:
score: 0
Accepted
time: 0ms
memory: 8524kb
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 9 7
result:
ok correct!
Test #14:
score: -7
Wrong Answer
time: 3ms
memory: 8536kb
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:
43348596076 88 1 151 179 194 83 135 89 27 122 84 46 112 125 40 45 138 98 167 163 182 22 163 167 109 81 184 98 187 15 99 72 33 12 159 92 41 173 67 173 186 42 116 44 24 41 178 17 92 82 197 101 5 32 87 29 121 198 64 26 143 107 51 79 107 168 143 148 50 75 124 123 14 49 137 54 93 61 19 108 52 9 3 100 8 3...
result:
wrong answer dist(194, 83) = 4 > k = 2
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: 3ms
memory: 8684kb
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: -4
Wrong Answer
time: 0ms
memory: 8736kb
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:
835467435153 1502 1 1789 598 488 419 525 269 202 694 1379 1724 1545 88 1123 701 1522 1158 1364 1918 131 1832 872 1057 345 1725 67 1634 1174 719 650 693 718 554 841 1935 175 220 917 1784 997 1315 92 257 802 1369 1257 1046 993 319 140 670 607 750 1885 838 1326 1333 675 615 722 477 517 1441 656 163 723...
result:
wrong answer your profit is 835467435153 but jury's plan is 1012330476243, your plan is not optimal!
Subtask #6:
score: 0
Skipped
Dependency #5:
0%