QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#185961 | #6629. Travelling Trader | cy1999 | 0 | 4ms | 18440kb | C++14 | 4.5kb | 2023-09-22 21:41:21 | 2023-09-22 21:41:21 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 10;
int n, k, v[N];
vector<int> G[N];
namespace subtask1 {
void solve() {
}
}
namespace subtask2 {
struct node_F {
int opt, f, g, h;
node_F(int a = 0, int b = 0, int c = 0, int d = 0) {opt = a; f = b; g = c; h = d;}
};
struct node_H {
int opt, h, g, f, g1, g2;
node_H(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0, int F = 0) {opt = a; h = b; g = c; f = d; g1 = e; g2 = F;}
};
node_F pf[N];
node_H ph[N];
int nowg[N];
ll f[N], g[N], h[N];
vector<int> ans;
void dfs(int x, int fa) {
int num = 0; ll sum = 0;
for (auto y : G[x]) {
if (y == fa) continue;
dfs(y, x);
num++; sum += (ll)v[y];
}
if (num == 0) return;
else {
int fson = 0, gson = 0, hson = 0;
for (auto y : G[x]) {
if (y == fa) continue;
if (f[y] >= f[fson]) fson = y;
if (g[y] >= g[gson]) gson = y;
if (h[y] >= h[hson]) hson = y;
}
nowg[x] = gson;
g[x] = g[gson] + sum;
if (gson == fson) {
int ex_fson = 0, ex_gson = 0;
for (auto y : G[x]) {
if (y == fa) continue;
if (y != fson && f[y] >= f[ex_fson]) ex_fson = y;
if (y != gson && g[y] >= g[ex_gson]) ex_gson = y;
}
if (f[fson] + g[ex_gson] > f[ex_fson] + g[gson]) gson = ex_gson;
else fson = ex_fson;
}
if (h[hson] + v[hson] >= f[fson] + g[gson] + sum) {
f[x] = h[hson] + v[hson];
pf[x] = node_F(1, 0, 0, hson);
}else {
f[x] = f[fson] + g[gson] + sum;
pf[x] = node_F(2, fson, gson, 0);
}
int mx1 = 0, mx2 = 0, mx3 = 0, res_g1 = 0, res_g2 = 0, res_f = 0;
for (auto y : G[x]) {
if (y == fa) continue;
if (g[y] >= g[mx1]) mx3 = mx2, mx2 = mx1, mx1 = y;
else if (g[y] >= g[mx2]) mx3 = mx2, mx2 = y;
else if (g[y] >= g[mx3]) mx3 = y;
}
int tmp_g1, tmp_g2;
for (auto y : G[x]) {
if (y == fa) continue;
if (y == mx1) tmp_g1 = mx2, tmp_g2 = mx3;
else if (y == mx2) tmp_g1 = mx1, tmp_g2 = mx3;
else tmp_g1 = mx1, tmp_g1 = mx2;
if (g[tmp_g1] + g[tmp_g2] + f[y] >= g[res_g1] + g[res_g2] + f[res_f]) res_g1 = tmp_g1, res_g2 = tmp_g2, res_f = y;
}
int res_h = 0, res_g = 0, tmp_g;
for (auto y : G[x]) {
if (y == fa) continue;
if (y == mx1) tmp_g = mx2;
else tmp_g = mx1;
if (g[tmp_g] + h[y] >= g[res_g] + h[res_h]) res_g = tmp_g, res_h = y;
}
if (g[res_g] + h[res_h] >= g[res_g1] + g[res_g2] + f[res_f]) {
h[x] = g[res_g] + h[res_h] + sum;
ph[x] = node_H(1, res_h, res_g, 0, 0, 0);
}else {
h[x] = g[res_g1] + g[res_g2] + f[res_f] + sum;
ph[x] = node_H(2, 0, 0, res_f, res_g1, res_g2);
}
}
}
void work(int x, int fa, int opt, vector<int> &vec) {
if (x == 0) return;
if (opt == 0) {
vec.push_back(x);
if (pf[x].opt == 1) work(pf[x].h, x, 2, vec);
else {
vector<int> tmp;
work(pf[x].g, x, 1, tmp);
reverse(tmp.begin(), tmp.end());
for (auto y : tmp) vec.push_back(y);
for (auto y : G[x]) {
if (y == pf[x].f || y == pf[x].g || y == fa) continue;
vec.push_back(y);
}
work(pf[x].f, x, 0, tmp);
}
}else if (opt == 1) {
vec.push_back(x);
vector<int> tmp;
work(nowg[x], x, 1, tmp);
reverse(tmp.begin(), tmp.end());
for (auto y : tmp) vec.push_back(y);
for (auto y : G[x]) {
if (y == nowg[x] || y == fa) continue;
vec.push_back(y);
}
}else {
if (ph[x].opt == 1) {
for (auto y : G[x]) {
if (y == fa || y == ph[x].g || y == ph[x].h) continue;
vec.push_back(y);
}
work(ph[x].g, x, 1, vec);
vec.push_back(x);
work(ph[x].h, x, 2, vec);
}else {
work(ph[x].g1, x, 1, vec);
vec.push_back(x);
vector<int> tmp;
work(ph[x].g2, x, 1, tmp);
reverse(tmp.begin(), tmp.end());
for (auto y : tmp) vec.push_back(y);
for (auto y : G[x]) {
if (y == fa || y == ph[x].g1 || y == ph[x].g2 || y == ph[x].f) continue;
vec.push_back(y);
}
work(ph[x].f, x, 0, vec);
}
}
}
void solve() {
dfs(1, 1);
printf("%lld\n", f[1] + v[1]);
work(1, 1, 0, ans);
printf("%d\n", ans.size());
for (auto x : ans) printf("%d ", x);
}
}
namespace subtask3 {
void solve() {
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1, x, y; i < n; ++i) {
scanf("%d%d", &x, &y);
G[x].push_back(y); G[y].push_back(x);
}
for (int i = 1; i <= n; ++i) scanf("%d", &v[i]);
if (k == 1) subtask1 :: solve();
else if (k == 2) subtask2 :: solve();
else subtask3 :: solve();
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 16412kb
input:
2 1 1 2 255959470 961356354
output:
result:
wrong output format Unexpected end of file - int64 expected
Subtask #2:
score: 0
Wrong Answer
Test #12:
score: 7
Accepted
time: 0ms
memory: 18148kb
input:
2 2 2 1 243296356 635616793
output:
878913149 2 1 2
result:
ok correct!
Test #13:
score: 0
Accepted
time: 4ms
memory: 18152kb
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 6 5 7 9 3 10 4 2 8
result:
ok correct!
Test #14:
score: -7
Wrong Answer
time: 0ms
memory: 18440kb
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:
57921623677 69 1 89 194 179 151 39 83 135 27 112 40 125 180 120 117 122 33 131 105 96 114 171 28 110 149 170 59 193 21 88 162 94 138 45 129 25 78 62 199 36 127 15 99 72 12 76 70 53 159 17 178 24 44 41 67 173 186 42 116 92 82 197 101 5 32 121 87 29 198
result:
wrong answer your claimed profit is 57921623677 but the profit of your plan is 41436947372
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: 4ms
memory: 16600kb
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:
result:
wrong output format Unexpected end of file - int64 expected
Subtask #6:
score: 0
Skipped
Dependency #5:
0%