QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#266917 | #7736. Red Black Tree | BoulevardDust | RE | 0ms | 0kb | C++20 | 3.3kb | 2023-11-26 19:39:32 | 2023-11-26 19:39:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define see(x) cerr << x << " "
#define seeln(x) cerr << x << endl
#define out(x) cerr << #x << " = " << x << " "
#define outln(x) cerr << #x << " = " << x << endl
#define re
inline void Rd(int &res){
re char c;res=0;
while(c=getchar(),c<48);
do res=(res<<3)+(res<<1)+(c^48);
while(c=getchar(),c>47);
}
const int N = 100005;
int n;
int col[N];
char str[N];
int fa[N];
vector<int> G[N];
struct node {
int src;
int cnt;
int k;
//node() {}
//node(int _1, int _2, int _3):src(_1), cnt(_2), k(_3) {}
};
const int inf = 1000000000;
int len[N], son[N];
int ans[N];
void dfs(int x) {
if (G[x].empty()) return (void)(len[x] = 1);
len[x] = inf;
for (int y : G[x]) {
dfs(y);
if (len[y] + 1 < len[x]) {
len[x] = len[y] + 1;
son[x] = y;
}
}
}
#define sz(x) (int)(x.size())
vector<int> dp[N];
int other[N], dpson[N], aux[N];
int Dp[N];
node cur[N];
int calcdp(int x, int id) {
out(x); out(id); outln(len[x]); outln(sz(dp[x]));
if (id <= len[x]) return dp[x][id];
else return inf;
}
void calc_top(int x) {
int y = cur[x].src;
int k = cur[x].k;
int cnt = cur[x].cnt;
// out(x); out(y); out(k); outln(cnt);
for (int i = 0; i <= len[x]; ++i) Dp[i] = inf;
for (int i = 0; i <= len[x]; ++i) {
for (int j = 0; j <= k; ++j) {
if (i >= j) {
Dp[i] = min(Dp[i], calcdp(y, i - j) + abs(cnt - j));
}
}
}
}
void clear(int x) {
for (int i = 0; i <= len[x]; ++i) Dp[i] = inf;
}
node run(int x) {
if (sz(G[x]) == 0) {
cur[x] = (node){x, 0, 0};
dp[x] = {0, 0};
if (col[x]) dp[x][0] = 1;
else dp[x][1] = 1;
out(x); outln(cur[x].src);
return cur[x];
}
if (sz(G[x]) == 1) {
node tmp = run(son[x]);
tmp.k += 1;
tmp.cnt += col[x];
ans[x] = ans[son[x]];
cur[x] = tmp;
return tmp;
}
for (int y : G[x]) run(y);
see("run"); outln(x);
dp[x].resize(len[x] + 1);
for (int i = 0; i <= len[x]; ++i) {
other[i] = 0;
dp[x][i] = inf;
}
for (int y : G[x]) {
calc_top(y);
for (int i = 0; i <= len[x]; ++i) {
other[i] = min(other[i] + Dp[i], inf);
}
clear(y);
}
outln(x);
for (int i = 0; i <= len[x]; ++i) {
out(i); outln(other[i]);
}
if (col[x]) {
for (int h = 0; h <= len[x]; ++h) {
int tmp = inf;
tmp = min(tmp, other[h] + 1);
if (h) tmp = min(tmp, other[h - 1]);
dp[x][h] = tmp;
}
} else {
for (int h = 0; h <= len[x]; ++h) {
int tmp = inf;
tmp = min(tmp, other[h]);
if (h) tmp = min(tmp, other[h - 1] + 1);
dp[x][h] = tmp;
}
}
ans[x] = inf;
for (int i = 0; i <= len[x]; ++i) {
ans[x] = min(ans[x], dp[x][i]);
}
for (int i = 0; i <= len[x]; ++i) {
out(x); out(i); outln(dp[x][i]);
}
return (node){x, 0, 0};
}
int main() {
int T; Rd(T); while (T--) {
Rd(n);
scanf("%s", str + 1);
for (int i = 1; i <= n; ++i) col[i] = str[i] - 48;
for (int i = 2; i <= n; ++i) {
int u; scanf("%d", &u);
G[u].push_back(i);
fa[i] = u;
}
}
for (int i = 0; i <= n; ++i) Dp[i] = inf;
dfs(1);
run(1);
for (int i = 1; i <= n; ++i) {
if (dp[i].empty()) continue;
outln(i);
for (int j = 0; j <= len[i]; ++j) {
out(i); out(j); outln(dp[i][j]);
}
}
for (int i = 1; i <= n; ++i) printf("%d%c", ans[i], " \n"[i == n]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 9 101011110 1 1 3 3 3 6 2 2 4 1011 1 1 3