QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#478102#5418. Color the TreeduckindogWA 17ms5776kbC++231.2kb2024-07-14 16:52:592024-07-14 16:53:00

Judging History

你现在查看的是最新测评结果

  • [2024-07-14 16:53:00]
  • 评测
  • 测评结果:WA
  • 用时:17ms
  • 内存:5776kb
  • [2024-07-14 16:52:59]
  • 提交

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 = 0; i <= maxDepth; ++i) {
      vt[i].clear();
      totalSize[i] = 0;
    }

    for (int i = 1; i <= n; ++i) { 
      ad[i].clear();
      d[i] = f[i] = 0;
    }
  }
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 5776kb

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: 17ms
memory: 3620kb

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
156
240
225
126
100
81
155
73
154
129
149
124
228
230
132
187
153
171
78
282
195
286
191
211
119
197
211
233
88
252
239
236
173
180
195
121
109
149
180
175
226
210
182
97
206
59
56
31
115
204
203
172
139
208
53
144
189
171
173
137
233
94
163
273
80
350
156
133
146
159
240
269
138
222...

result:

wrong answer 2nd numbers differ - expected: '168', found: '172'