QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#583953 | #5418. Color the Tree | Longvu | WA | 29ms | 25952kb | C++14 | 3.6kb | 2024-09-23 00:20:51 | 2024-09-23 00:20:52 |
Judging History
answer
/**
* author: longvu
* created: 09/22/24 22:35:37
**/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
const int INF = 1e18;
const int nax = (int)(201001);
const int mod = 1e9 + 7;
template<class X, class Y>
bool maximize(X& x, const Y y) {
if (y > x) {x = y; return true;}
return false;
}
template<class X, class Y>
bool minimize(X& x, const Y y) {
if (y < x) {x = y; return true;}
return false;
}
#define Fi first
#define Se second
const int LOG = 18;
pair<int, int> rmq[nax][LOG + 1];
int euler[nax], h[nax], Log2[nax], fir[nax];
vector<int> adj[nax];
int m = 0;
void dfs(int u, int pa) {
euler[++m] = u;
fir[u] = m;
rmq[m][0] = {h[euler[m]], euler[m]};
for (auto v : adj[u]) {
if (v == pa) {
continue;
}
h[v] = 1 + h[u];
dfs(v, u);
euler[++m] = u;
rmq[m][0] = {h[euler[m]], euler[m]};
}
}
void precal() {
m = 0;
dfs(1, -1);
for (int k = 1; k <= LOG; ++k) {
for (int i = 1; i + (1 << k) - 1 <= m; ++i) {
rmq[i][k] = min(rmq[i][k - 1], rmq[i + (1 << (k - 1))][k - 1]);
}
}
}
int find_lca(int u, int v) {
int l = min(fir[u], fir[v]), r = max(fir[u], fir[v])
, k = Log2[r - l + 1];
return min(rmq[l][k], rmq[r - (1 << k) + 1][k]).Se;
}
int a[nax], dp[nax], rmqp[nax][LOG + 1];
vector<int> memo[nax];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
for (int i = 2; i < nax; ++i) {
Log2[i] = 1 + Log2[i >> 1];
}
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
rmqp[i][0] = a[i];
}
for (int k = 1; k <= LOG; ++k) {
for (int i = 0; i + (1 << k) - 1 < n; ++i) {
rmqp[i][k] = min(rmqp[i][k - 1], rmqp[i + (1 << (k - 1))][k - 1]);
}
}
auto get = [&](int l, int r) {
int k = Log2[r - l + 1];
return min(rmqp[l][k], rmqp[r - (1 << k) + 1][k]);
};
for (int i = 0; i <= n; ++i) {
adj[i].clear();
memo[i] = { -1};
}
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
precal();
for (int i = 1; i <= n; ++i) {
memo[h[i]].push_back(i);
}
int ans = 0;
for (int hi = 0; hi < n; ++hi) {
sort(1 + all(memo[hi]), [&](int u, int v) {
return fir[u] < fir[v];
});
for (int i = 0; i < sz(memo[hi]); ++i) {
dp[i] = INF;
}
dp[0] = 0;
int j = 1;
for (int i = 1; i < sz(memo[hi]); ++i) {
if (sz(memo[hi]) == 1) {
continue;
}
int l = 1, r = i;
auto cal = [&](int z) {
return get(hi - h[find_lca(memo[hi][z], memo[hi][i])], hi) + dp[z - 1];
};
while (j < i && cal(j + 1) < cal(j)) {
j++;
}
dp[i] = cal(j);
assert(dp[i - 1] <= dp[i]);
}
// cout << hi << " " << dp[sz(memo[hi]) - 1] << '\n';
ans += dp[sz(memo[hi]) - 1];
}
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 25440kb
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: 29ms
memory: 25952kb
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:
185 177 252 230 156 255 225 126 100 81 155 73 159 157 160 152 228 230 140 205 155 171 78 282 195 649 200 211 125 222 552 257 88 252 239 252 173 180 206 145 109 198 180 188 226 242 182 97 301 59 56 52 115 224 203 279 163 208 53 158 228 199 173 164 233 115 163 273 80 350 156 133 308 159 240 269 167 23...
result:
wrong answer 1st numbers differ - expected: '180', found: '185'