QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#108040 | #5418. Color the Tree | HaccerKat | WA | 103ms | 6628kb | C++20 | 7.8kb | 2023-05-23 14:55:44 | 2023-05-23 14:56:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
template<typename T>
int SIZE(T (&t)){
return t.size();
}
template<typename T, size_t N>
int SIZE(T (&t)[N]){
return N;
}
string to_string(char t){
return "'" + string({t}) + "'";
}
string to_string(bool t){
return t ? "true" : "false";
}
string to_string(const string &t, int x1=0, int x2=1e9){
string ret = "";
for(int i = min(x1,SIZE(t)), _i = min(x2,SIZE(t)-1); i <= _i; ++i){
ret += t[i];
}
return '"' + ret + '"';
}
string to_string(const char* t){
string ret(t);
return to_string(ret);
}
template<size_t N>
string to_string(const bitset<N> &t, int x1=0, int x2=1e9){
string ret = "";
for(int i = min(x1,SIZE(t)); i <= min(x2,SIZE(t)-1); ++i){
ret += t[i] + '0';
}
return to_string(ret);
}
template<typename T, typename... Coords>
string to_string(const T (&t), int x1=0, int x2=1e9, Coords... C);
template<typename T, typename S>
string to_string(const pair<T, S> &t){
return "(" + to_string(t.first) + ", " + to_string(t.second) + ")";
}
template<typename T, typename... Coords>
string to_string(const T (&t), int x1, int x2, Coords... C){
string ret = "[";
x1 = min(x1, SIZE(t));
auto e = begin(t);
advance(e,x1);
for(int i = x1, _i = min(x2,SIZE(t)-1); i <= _i; ++i){
ret += to_string(*e, C...) + (i != _i ? ", " : "");
e = next(e);
}
return ret + "]";
}
template<int Index, typename... Ts>
struct print_tuple{
string operator() (const tuple<Ts...>& t) {
string ret = print_tuple<Index - 1, Ts...>{}(t);
ret += (Index ? ", " : "");
return ret + to_string(get<Index>(t));
}
};
template<typename... Ts>
struct print_tuple<0, Ts...> {
string operator() (const tuple<Ts...>& t) {
return to_string(get<0>(t));
}
};
template<typename... Ts>
string to_string(const tuple<Ts...>& t) {
const auto Size = tuple_size<tuple<Ts...>>::value;
return print_tuple<Size - 1, Ts...>{}(t);
}
void dbgr(){;}
template<typename Heads, typename... Tails>
void dbgr(Heads H, Tails... T){
cout << to_string(H) << " | ";
dbgr(T...);
}
void dbgs(){;}
template<typename Heads, typename... Tails>
void dbgs(Heads H, Tails... T){
cout << H << " ";
dbgs(T...);
}
/*
formatted functions:
*/
/*
consider __VA_ARGS__ as a whole:
dbgv() prints values only
dbg() prints name and values
*/
#define dbgv(...) cout << to_string(__VA_ARGS__) << endl;
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgv(__VA_ARGS__);
//#define dbg(...)
/*
consider __VA_ARGS__ as a sequence of arguments:
dbgr() prints values only
dbgm() prints names and values
*/
#define dbgr(...) dbgr(__VA_ARGS__); cout << endl;
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgr(__VA_ARGS__);
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
// using u128 = __uint128_t;
// using i128 = __int128;
const int mod = 1000000007;
const int N = 200005;
const int LOG = 17;
const int inf = 1e9;
const double eps = 1e-11;
string s;
int n, m, k, qq;
void solve() {
cin >> n;
vector<int> a(n), logA(n + 1);
vector<vector<int>> adj(n), deps(n), rmq(n, vector<int>(LOG)), up(n, vector<int>(LOG));
for (int i = 0; i < n; i++) {
cin >> a[i];
rmq[i][0] = a[i];
}
for (int i = 2; i <= n; i++) {
logA[i] = logA[i / 2] + 1;
}
for (int j = 1; j < LOG; j++) {
for (int i = 0; i < n - (1 << j) + 1; i++) {
rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
}
}
auto querymn = [&](int l, int r) {
int sz = r - l + 1;
int lg = logA[sz];
return min(rmq[l][lg], rmq[r - (1 << lg) + 1][lg]);
};
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<bool> vis(n);
vector<int> dep(n), tin(n), tout(n);
up[0][0] = -1;
int t = 0;
auto dfs = [&](int u, auto dfs) -> void {
vis[u] = true, tin[u] = t++;
deps[dep[u]].push_back(u);
for (int v : adj[u]) {
if (!vis[v]) {
dep[v] = dep[u] + 1, up[v][0] = u;
dfs(v, dfs);
}
}
tout[u] = t++;
};
dfs(0, dfs);
for (int j = 1; j < LOG; j++) {
for (int i = 0; i < n; i++) {
if (up[i][j - 1] == -1) up[i][j] = -1;
else up[i][j] = up[up[i][j - 1]][j - 1];
}
}
auto isancestor = [&](int u, int v) {
return (tin[u] <= tin[v] && tout[u] >= tout[v]);
};
auto query = [&](int u, int v) {
if (dep[u] > dep[v]) swap(u, v);
if (isancestor(u, v)) return u;
for (int i = LOG - 1; i >= 0; i--) {
int w = up[u][i];
if (w != -1 && !isancestor(w, v)) u = w;
}
return up[u][0];
};
vector<vector<int>> viradj(n);
vector<int> dp(n);
ll out = a[0];
// dbg(deps);
// dbg(tin);
// dbg(tout);
// dbg(rmq);
// dbg(up);
for (int i = 1; i < n; i++) {
int sz = deps[i].size();
if (sz == 0) break;
vector<pi> nodes(sz);
for (int j = 0; j < sz; j++) {
nodes[j] = {tin[deps[i][j]], deps[i][j]};
}
sort(nodes.begin(), nodes.end());
stack<int> stk;
stk.push(0);
vector<pi> used;
used.push_back({0, 0});
// dbg(nodes);
for (int j = 0; j < sz; j++) {
auto [temp, u] = nodes[j];
used.push_back({dep[u], u});
while (stk.size() > 1) {
int v = stk.top();
int lca = query(u, v);
if (isancestor(v, u)) break;
stk.pop();
int vv = stk.top();
if (dep[vv] <= dep[lca]) {
if (vv != lca) stk.push(lca);
viradj[lca].push_back(v);
used.push_back({dep[lca], lca});
break;
}
viradj[vv].push_back(v);
}
stk.push(u);
}
// dbg(viradj);
// dbg(stk.size());
while (stk.size() > 1) {
int v = stk.top();
stk.pop();
int vv = stk.top();
viradj[vv].push_back(v);
}
sort(used.rbegin(), used.rend());
for (auto [d, u] : used) {
int sum = 0, cnt = 0;
for (int v : viradj[u]) {
sum += dp[v], cnt++;
}
dp[u] = min((cnt == 0 ? inf : sum), querymn(i - d, i));
}
// dbg(dp);
// dbg(viradj);
// dbg(dp[0]);
out += dp[0];
for (auto [d, u] : used) {
viradj[u].clear();
dp[u] = 0;
}
}
cout << out << "\n";
}
int32_t main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
int tt;
cin >> tt;
while (tt--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3316kb
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: 0
Accepted
time: 103ms
memory: 3544kb
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 168 222 230 156 240 225 126 100 81 155 73 154 127 149 124 228 230 132 187 153 170 78 282 195 286 191 211 119 197 211 233 88 252 239 233 173 180 195 121 109 148 180 175 226 210 182 97 199 59 56 31 115 204 203 172 139 208 53 140 189 170 173 137 233 94 163 273 80 350 156 133 146 159 240 269 137 222...
result:
ok 3000 numbers
Test #3:
score: 0
Accepted
time: 97ms
memory: 3832kb
input:
300 474 5 24 21 41 15 23 43 48 32 19 27 40 10 49 40 6 18 41 43 31 45 18 35 36 12 10 23 45 28 23 14 43 37 45 12 16 20 17 49 13 22 8 30 19 27 40 22 14 30 47 16 39 25 48 21 26 50 8 14 26 9 30 41 15 44 24 16 46 50 39 25 47 24 45 21 18 26 21 5 39 15 10 47 48 11 44 44 33 23 14 35 39 35 30 38 9 13 15 39 5 ...
output:
329 183 264 219 323 220 348 342 410 395 80 201 299 144 207 408 360 215 283 104 320 394 277 210 273 285 242 253 265 379 360 322 202 351 195 196 266 270 171 342 239 283 286 300 331 317 345 268 173 296 275 224 480 330 264 162 199 378 254 214 231 293 229 259 241 268 380 419 233 185 364 341 328 237 320 3...
result:
ok 300 numbers
Test #4:
score: 0
Accepted
time: 96ms
memory: 6628kb
input:
30 4926 18 13 47 7 21 39 28 48 21 44 14 18 39 13 46 33 6 49 9 7 10 29 29 25 38 15 16 42 41 41 40 14 26 13 6 19 17 31 24 18 30 24 48 46 38 21 28 42 29 50 33 28 18 26 18 42 13 23 35 20 32 6 17 25 44 46 35 36 50 24 7 29 34 14 41 41 8 33 22 46 7 6 22 40 24 8 44 36 38 13 37 37 25 22 7 43 50 33 19 44 24 4...
output:
427 520 443 359 427 408 371 415 482 436 269 530 478 485 435 453 418 503 443 453 405 425 347 564 297 435 559 453 213 395
result:
ok 30 numbers
Test #5:
score: -100
Wrong Answer
time: 103ms
memory: 3540kb
input:
3000 74 555233197 518812085 998593787 821753058 967306600 663279435 696954042 885219300 489323226 146136486 447383587 785317885 838306349 708275482 715157265 298848995 400280904 374077023 673881523 207667786 885945020 459791675 992519779 327830583 821713695 253985403 926395863 419409783 138881726 80...
output:
2861015772 5749321898 -775664606 4707144715 2761702533 1043082801 2742954885 2999820393 -3509249398 304171369 1643824224 -354021737 2104643636 905258598 -1830961072 2287799528 -243178347 3699192818 3888960419 1644911775 2766114996 1734720583 -854275687 1955540148 2584133805 3177069662 3705913835 284...
result:
wrong answer 1st numbers differ - expected: '6742611216', found: '2861015772'