QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#115147 | #5418. Color the Tree | UrgantTeam# | WA | 32ms | 73628kb | C++14 | 2.5kb | 2023-06-24 17:53:32 | 2023-06-24 17:53:35 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
int const maxn = 1e5 + 5;
int a[maxn], cmp[maxn], inf = 1e18, h[maxn];
vector < int > g[maxn];
deque < pair < int, pair < int, int > > > dp[maxn];
int mn[(1 << 18)], n;
void build(int i, int l, int r) {
if (r - l == 1) {
mn[i] = a[l];
return;
}
int m = (r + l) / 2;
build(2 * i + 1, l, m);
build(2 * i + 2, m, r);
mn[i] = min(mn[2 * i + 1], mn[2 * i + 2]);
}
int get(int i, int l, int r, int lq, int rq) {
if (lq >= r || l >= rq) return inf;
if (lq <= l && r <= rq) return mn[i];
int m = (r + l) / 2;
return min(get(2 * i + 1, l, m, lq, rq), get(2 * i + 2, m, r, lq, rq));
}
void dfs(int v, int p) {
h[v] = h[p] + 1;
dp[v] = {}, cmp[v] = v;
for (auto u : g[v]) {
if (u != p) {
dfs(u, v);
if (dp[cmp[u]].size() > dp[cmp[v]].size()) cmp[v] = cmp[u];
}
}
int R = 0;
for (auto u : g[v]) {
if (u == p || cmp[u] == cmp[v]) continue;
R = max(R, (int)dp[cmp[u]].size());
}
if (p == 0) R = dp[cmp[v]].size();
for (int i = 0; i < R; i++) {
dp[cmp[v]][i].first = min(dp[cmp[v]][i].first, get(0, 0, n, dp[cmp[v]][i].second.first, dp[cmp[v]][i].second.first + dp[cmp[v]][i].second.second - h[v]));
}
for (auto u : g[v]) {
if (u == p || cmp[u] == cmp[v]) continue;
for (int mx = 0; mx < dp[cmp[u]].size(); mx++) {
dp[cmp[v]][mx].first += min(dp[cmp[u]][mx].first, get(0, 0, n, dp[cmp[u]][mx].second.first, dp[cmp[u]][mx].second.first + dp[cmp[u]][mx].second.first - h[v]));
}
}
dp[cmp[v]].push_front({a[0], {0, h[v]}});
for (int i = 1; i <= R; i++) {
dp[cmp[v]][i].first = min(dp[cmp[v]][i].first, a[i]);
dp[cmp[v]][i].second = {i, h[v]};
}
}
main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#endif // HOME
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int u, v;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i - 1];
g[i] = {};
}
build(0, 0, n);
for (int i = 1; i < n; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
int ans = 0;
for (auto key : dp[cmp[1]]) {
ans += key.first;
}
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 73628kb
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: 32ms
memory: 73528kb
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 177 222 230 156 255 225 126 100 81 172 73 173 127 160 149 228 230 132 217 155 171 78 282 195 286 204 211 119 198 211 235 88 252 239 236 175 180 197 145 109 148 180 175 226 210 182 109 199 59 56 31 115 220 203 172 155 226 53 158 189 170 173 137 232 107 163 273 80 350 156 133 153 159 252 269 148 2...
result:
wrong answer 2nd numbers differ - expected: '168', found: '177'