QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#478088 | #5418. Color the Tree | duckindog | WA | 16ms | 5688kb | C++23 | 1.2kb | 2024-07-14 16:39:56 | 2024-07-14 16:39:57 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 100'000 + 10;
int n;
int a[N];
vector<int> ad[N];
int d[N], f[N];
void dfs0(int u, int p = -1) {
for (const auto& v : ad[u]) {
if (v == p) continue;
f[v] = d[v] = d[u] + 1;
dfs0(v, u);
f[u] = max(f[u], f[v]);
}
}
vector<int> vt[N];
int totalSize[N];
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
int test; cin >> test;
while (test--) {
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
ad[u].push_back(v);
ad[v].push_back(u);
}
dfs0(1);
for (int i = 1; i <= n; ++i) vt[f[i]].push_back(i);
int maxDepth = f[1];
int answer = 0;
for (int i = maxDepth; i >= 0; --i) {
for (const auto& x : vt[i]) totalSize[d[x]] += 1;
int ret = 1'000'000'000'000'000;
for (int j = 0; j <= i; ++j) ret = min(ret, totalSize[j] * a[i - j]);
answer += ret;
}
cout << answer << "\n";
for (int i = 1; i <= n; ++i) {
ad[i].clear();
totalSize[d[i]] = 0;
vt[d[i]].clear();
}
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3580kb
input:
3 4 10 15 40 1 1 2 2 3 2 4 5 10 5 1 100 1000 1 2 2 3 2 4 4 5 4 1000 200 10 8 1 2 2 3 3 4
output:
35 17 1218
result:
ok 3 number(s): "35 17 1218"
Test #2:
score: -100
Wrong Answer
time: 16ms
memory: 5688kb
input:
3000 54 43 44 11 49 17 14 7 30 35 12 34 14 15 8 29 47 30 31 39 17 26 23 26 45 34 50 49 13 35 18 29 15 13 44 47 5 22 20 19 46 30 22 26 13 47 46 43 27 48 48 13 14 30 44 1 2 1 3 2 4 3 5 4 6 2 7 4 8 1 9 3 10 1 11 8 12 2 13 9 14 1 15 1 16 15 17 15 18 7 19 1 20 19 21 13 22 10 23 17 24 9 25 9 26 24 27 8 28...
output:
180 172 222 230 239 240 225 167 227 236 337 262 610 559 908 490 695 864 770 1009 153 287 203 690 586 623 564 794 165 617 638 865 339 823 239 370 471 292 456 532 221 291 1216 762 1110 1137 946 662 527 516 350 191 914 879 940 428 1204 1728 300 1408 792 548 1293 637 2140 977 1514 1629 1739 2679 2197 46...
result:
wrong answer 2nd numbers differ - expected: '168', found: '172'