QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#235332 | #674. Ascending Tree | ckiseki# | WA | 1ms | 3468kb | C++20 | 3.0kb | 2023-11-02 17:19:11 | 2023-11-02 17:19:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#include <experimental/iterator>
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(const char *s, auto ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int f = 0;
(..., (cerr << (f++ ? ", " : "") << a));
cerr << ")\e[0m\n";
}
void orange_(const char *s, auto L, auto R) {
cerr << "\e[1;33m[ " << s << " ] = [ ";
using namespace experimental;
copy(L, R, make_ostream_joiner(cerr, ", "));
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
using lld = int64_t;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N;
cin >> N;
vector<int> C(N), P(N, -1);
cin >> C[0];
for (int i = 1; i < N; i++) {
cin >> P[i] >> C[i];
--P[i];
}
vector<int> dep(N);
for (int i = 1; i < N; i++)
dep[i] = dep[P[i]] + 1;
for (int i = 0; i < N; i++)
C[i] += dep[i];
orange(all(C));
vector<int> F = C;
vector<lld> dp(N), sub(N);
vector<int> type(N);
vector<vector<int>> g(N);
const auto dc = [&](auto self, vector<int> idx) {
vector<int> u;
for (int i : idx) {
u.push_back(F[i]);
}
sort(all(u));
u.erase(unique(all(u)), u.end());
if (u.size() <= 1) {
return;
}
debug(u.size());
orange(all(u));
const int A = u[u.size() / 2];
const int B = u[u.size() / 2 - 1];
// the closer to root, should be A
assert (A != B);
for (int i : idx) g[i].clear();
for (int i : idx | views::reverse) {
if (binary_search(all(idx), P[i])) {
g[P[i]].emplace_back(i);
}
}
for (int i : idx | views::reverse) {
sub[i] = abs(B - F[i]);
dp[i] = abs(A - F[i]);
for (int j : g[i]) {
dp[i] += dp[j];
sub[i] += sub[j];
}
if (sub[i] < dp[i]) {
type[i] = false;
dp[i] = sub[i];
} else {
type[i] = true;
}
}
vector<int> left;
vector<vector<int>> right;
const auto restore = [&](auto res, int i, bool flag = true) -> void {
if (!flag) {
right.back().emplace_back(i);
type[i] = flag;
for (int j : g[i]) {
res(res, j, flag);
}
} else {
if (type[i]) {
left.push_back(i);
} else {
right.emplace_back();
right.back().emplace_back(i);
}
for (int j : g[i]) {
res(res, j, type[i]);
}
}
};
restore(restore, idx[0]);
for (int i : left) {
F[i] = max(F[i], A);
}
for (auto R : right) {
for (int i : R)
F[i] = min(F[i], B);
}
self(self, left);
for (auto R : right)
self(self, R);
};
vector<int> idx(N);
iota(all(idx), 0);
dc(dc, idx);
orange(all(F));
int64_t cost = 0;
for (int i = 0; i < N; i++) {
cost += abs(F[i] - C[i]);
}
cout << cost << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3468kb
input:
21 -28 1 -21 2 -7 3 -3 4 -44 4 21 4 -39 4 42 2 -49 9 16 8 -22 5 -49 11 -8 1 3 11 28 3 8 8 -38 16 34 4 -45 7 -43 2 7
output:
101
result:
wrong answer 1st lines differ - expected: '290', found: '101'