QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#627259 | #6437. Paimon's Tree | Corzica | WA | 15ms | 9960kb | C++14 | 2.4kb | 2024-10-10 15:22:58 | 2024-10-10 15:22:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
long long f[155][155][165], maxn[155][155][165];
int n, qwe, a[155], siz[155], cnt, sum[155];
vector<int> to[155], v, vv;
int maxa, maxb;
inline void dfs(int p, int q, int w) {
if (w > maxa) {
maxa = w, maxb = p;
}
for (int i : to[p]) {
if (i != q) {
dfs(i, p, w + 1);
}
}
}
inline void ddfs(int p, int q, int w) {
vv.push_back(p);
if (w > maxa) {
maxa = w;
v = vv;
}
for (int i : to[p]) {
if (i != q) ddfs(i, p, w + 1);
}
vv.pop_back();
}
inline void getsiz(int p, int pp, int qq, int q, int name) {
siz[name]++;
for (int i : to[p]) {
if (i != pp && i != qq && i != q) {
getsiz(i, pp, qq, p, name);
}
}
}
signed main() {
// freopen("length.in", "r", stdin);
// freopen("length.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> qwe;
while (qwe--) {
cin >> n;
n++;
int p, q;
for (int i = 1; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
cin >> p >> q;
to[p].push_back(q), to[q].push_back(p);
}
maxa = -1;
dfs(1, 0, 0);
v.clear(), vv.clear();
maxa = -1;
vv.push_back(0);
ddfs(maxb, 0, 0);
cnt = v.size() - 1;
v.push_back(0);
for (int i = 1; i <= cnt; i++) {
siz[i] = 0;
getsiz(v[i], v[i - 1], v[i + 1], 0, i);
siz[i]--;
sum[i] = sum[i - 1] + siz[i];
}
for (int i = 1; i <= cnt; i++) {
for (int j = 1; j <= cnt; j++) {
for (int k = 0; k <= cnt + 3; k++) {
f[i][j][k] = maxn[i][j][k] = -0x3f3f3f3f3f3f3f3f;
}
}
}
for (int i = 1; i < cnt; i++) {
for (int j = 0; j <= max(siz[i], siz[i + 1]); j++) {
f[i][i + 1][siz[i] + siz[i + 1] - j] = a[j + 1];
}
for (int j = sum[cnt]; j >= 0; j--) {
maxn[i][i + 1][j] = max(maxn[i][i + 1][j + 1], f[i][i + 1][j]);
}
}
for (int len = 3; len <= cnt; len++) {
for (int l = 1, r = len; r <= cnt; l++, r++) {
for (int k = sum[r] - sum[l - 1]; k >= 0; k--) {
if (k >= siz[l]) f[l][r][k] = max(f[l][r][k], maxn[l + 1][r][k - siz[l]]);
if (k >= siz[r]) f[l][r][k] = max(f[l][r][k], maxn[l][r - 1][k - siz[r]]);
f[l][r][k] += a[sum[r] - sum[l - 1] - k + len - 1];
maxn[l][r][k] = max(maxn[l][r][k + 1], f[l][r][k]);
}
}
}
cout << maxn[1][cnt][0] << "\n";
for (int i = 1; i <= n; i++) to[i].clear();
}
}
//1
//5
//1 7 3 5 4
//1 3
//2 3
//3 4
//4 5
//4 6
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5792kb
input:
2 5 1 7 3 5 4 1 3 2 3 3 4 4 5 4 6 1 1000000000 1 2
output:
16 1000000000
result:
ok 2 number(s): "16 1000000000"
Test #2:
score: -100
Wrong Answer
time: 15ms
memory: 9960kb
input:
5000 19 481199252 336470888 634074578 642802746 740396295 773386884 579721198 396628655 503722503 971207868 202647942 2087506 268792718 46761498 443917727 16843338 125908043 691952768 717268783 9 4 4 18 12 9 10 9 4 1 6 12 2 18 9 13 6 14 4 8 2 3 10 17 2 20 19 20 5 1 12 15 15 16 4 7 17 11 4 240982681 ...
output:
5750811120 1896999359 4208559611 4140156713 5361004844 1875024569 3690026656 3702623113 3412485417 7807375141 5341435147 2355946110 3090496776 5626636202 4729664767 2207592767 572868833 4759005973 2944749369 2538044586 3083947956 5757497518 1421427135 3971435093 1197051728 396588615 251138097 221986...
result:
wrong answer 30th numbers differ - expected: '5401379163', found: '5626505466'