QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#691205 | #5418. Color the Tree | GQLZH | RE | 0ms | 17248kb | C++20 | 2.7kb | 2024-10-31 10:23:43 | 2024-10-31 10:23:50 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read() {
int x = 0, f = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -f; c = getchar();}
while (c >= '0' && c <= '9') {x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}
return x * f;
}
#define pii pair<int, int>
#define mk make_pair
#define pb push_back
const int N = 3e5 + 10; const ll inf = 1e18;
int n, lg[N];
int st[19][N], w[N];
vector<int> e[N];
inline ll queryMin(int l, int r) {
if (l > r) return inf;
int d = lg[r-l+1];
return min(st[d][l], st[d][r-(1<<d)+1]);
}
int fa[19][N], dep[N]; vector<int> vec[N];
inline void dfs(int u, int f, int d) {
fa[u][0] = f; dep[u] = d;
for (int i = 1; i < 18; ++ i) fa[u][i] = fa[fa[u][i-1]][i-1];
vec[d].pb(u);
for (auto v : e[u]) if (v ^ f) dfs(v, u, d+1);
}
inline int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = 18; i >= 0; -- i) if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
if (u == v) return u;
for (int i = 18; i >= 0; -- i) if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
vector<int> E[N];
int top, stc[N];
inline ll dp(int u, int fd, int d) {
ll ret = 0;
if (E[u].empty()) ret = inf;
else {
for (auto v : E[u])
ret += dp(v, dep[u], d);
} ret = min(ret, queryMin(d - dep[u], d - fd - 1));
return ret;
}
inline ll calc(int d) {
stc[top = 1] = 1; E[1].clear();
for (auto u : vec[d]) {
E[u].clear(); int c = lca(u, stc[top]);
if (c == stc[top]) {
stc[++ top] = u;
} else {
while (dep[stc[top-1]] > dep[c])
E[stc[top-1]].pb(stc[top]), top --;
if (c == stc[top-1]) {
E[c].pb(stc[top]); top --;
} else {
E[c].clear(); E[c].pb(stc[top]); stc[top] = c;
} stc[++ top] = u;
}
} while (top > 1) {
E[stc[top-1]].pb(stc[top]); top --;
} return dp(1, 0, d);
}
inline void solve() {
n = read();
for (int i = 0; i < n; ++ i) w[i] = read(), st[0][i] = w[i];
for (int i = 1; i <= n; ++ i) e[i].clear();
for (int i = 1; i <= 18; ++ i)
for (int j = 0; j + (1 << i) - 1 < n; ++ j)
st[i][j] = min(st[i-1][j], st[i-1][j+(1<<i-1)]);
for (int i = 1, u, v; i < n; ++ i)
u = read(), v = read(), e[u].pb(v), e[v].pb(u);
for (int i = 1; i <= n; ++ i) vec[i].clear();
dfs(1, 0, 1);
ll ans = w[0];
for (int i = 2; i <= n; ++ i) {
if (vec[i].empty()) break;
ans += calc(i);
} printf("%lld\n", ans);
}
int main () {
for (int i = 2; i < N; ++ i) lg[i] = lg[i >> 1] + 1;
int T = read();
while (T --) solve();
return 0;
}
/*
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17248kb
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
Runtime Error
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...