QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#864905 | #5418. Color the Tree | lc20110802 | WA | 50ms | 15712kb | C++14 | 2.6kb | 2025-01-21 11:01:27 | 2025-01-21 11:01:27 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, dfncnt, rt;
int a[N], dfn[N], dep[N], lg2[N], fa[N][18];
long long dp[N], stc[N][18];
vector<int> g[N], h[N], vtp, vt[N], imp;
void init(){
dfncnt = 0;
vtp.clear();
for(int i = 0; i <= n; i++){
g[i].clear(), h[i].clear(), vt[i].clear();
dep[i] = dfn[i] = dp[i] = a[i] = 0;
for(int j = 0; j < 18; j++) {
stc[i][j] = 1e18;
fa[i][j] = 0;
}
}
n = rt = 0;
return ;
}
inline long long query_min(int l, int r){
l++, r++;
int T = lg2[r - l + 1];
return min(stc[l][T], stc[r - (1 << T) + 1][T]);
}
void dfs(int u, int pre){
dfn[u] = ++dfncnt;
dep[u] = dep[pre] + 1; h[dep[u]].push_back(u);
fa[u][0] = pre; for(int i = 1; i <= 20; i++)
fa[u][i] = fa[fa[u][i - 1]][i - 1];
for(int v : g[u]) if(v != pre)
dfs(v, u);
return ;
}
inline int lca(int x, int y){
if(dep[x] < dep[y]) swap(x, y);
for(int i = 17; i >= 0; i--)
if(dep[fa[x][i]] >= dep[y])
x = fa[x][i];
if(x == y) return x;
for(int i = 17; i >= 0; i--)
if(fa[x][i] != fa[y][i])
x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
inline void build_virtual_tree(){
sort(imp.begin(), imp.end(), [](int _x, int _y){return dfn[_x] < dfn[_y];});
vtp.clear();
for(int i = 0; i < imp.size() - 1; i++)
vtp.push_back(imp[i]), vtp.push_back(lca(imp[i], imp[i + 1]));
vtp.push_back(imp.back());
sort(vtp.begin(), vtp.end(), [](int _x, int _y){return dfn[_x] < dfn[_y];});
int len = unique(vtp.begin(), vtp.end()) - vtp.begin();
for(int i : vtp) vt[i].clear();
for(int i = 0, lc; i < len - 1; i++){
lc = lca(vtp[i], vtp[i + 1]);
vt[lc].push_back(vtp[i + 1]);
}
rt = vtp[0];
return ;
}
void dfs_on_vt(int u, int pre, int D){
dp[u] = (!vt[u].size()) * 1e18;
for(int v : vt[u]){
dfs_on_vt(v, u, D);
dp[u] += dp[v];
}
dp[u] = min(dp[u], query_min(D - dep[u], D - dep[pre] - 1));
return ;
}
void work(){
init();
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i], stc[i][0] = a[i];
for(int i = 2; i <= n; i++) lg2[i] = lg2[i >> 1] + 1;
for(int i = 1; i <= 17; i++)
for(int j = 1; j + (1 << i) - 1 <= n; j++)
stc[j][i] = min(stc[j][i - 1], stc[j + (1 << (i - 1))][i - 1]);
for(int i = 1; i < n; i++){
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1, 0);
long long ans = 0;
for(int i = 1; i <= n; i++) if(h[i].size()){
imp = h[i];
build_virtual_tree();
dfs_on_vt(rt, 0, i);
ans += dp[rt];
}
cout << ans << "\n";
return ;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T; cin >> T;
while(T--) work();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 13844kb
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: 50ms
memory: 15712kb
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:
143 127 185 208 145 254 201 133 127 87 181 88 147 162 182 117 217 198 113 194 153 135 80 218 184 258 175 201 115 158 165 215 69 240 232 197 157 184 174 146 107 187 176 194 222 173 151 121 211 59 67 31 123 187 187 149 125 189 53 107 179 171 159 136 177 96 129 196 80 320 147 98 190 132 179 266 124 213...
result:
wrong answer 1st numbers differ - expected: '180', found: '143'