QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#267166#7736. Red Black Treeucup-team228TL 1811ms33388kbC++203.9kb2023-11-27 00:16:022023-11-27 00:16:02

Judging History

你现在查看的是最新测评结果

  • [2024-02-19 10:14:05]
  • hack成功,自动添加数据
  • (/hack/538)
  • [2023-11-27 00:16:02]
  • 评测
  • 测评结果:TL
  • 用时:1811ms
  • 内存:33388kb
  • [2023-11-27 00:16:02]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf = 1e9 + 10;

void add(int& x, int y) {
    x = min(inf, x + y);
}

const int N = 1e5 + 10;
vector<int> g[N];
bool is_black[N];
int ans[N];
vector<int> dp[N];
int reds[N], blacks[N];
int dist_leaf[N];

void init(int n) {
    for (int i = 1; i <= n; i++) {
        g[i].clear();
        dp[i].clear();
        reds[i] = 0;
        blacks[i] = 0;
        dist_leaf[i] = 0;
    }
}

void recalc(vector<int>& cur, int red_cnt, int black_cnt, int max_len) {
    cur.resize(cur.size() + red_cnt, inf);
    for (int i = int(cur.size()) - 1; i >= 0; i--) {
        for (int j = 1; j <= red_cnt && j <= i; j++) {
            cur[i] = min(cur[i], cur[i - j] + j);
        }
    }
    cur.resize(cur.size() + black_cnt, inf);
    for (int i = int(cur.size()) - 1; i >= 0; i--) {
        if (i >= black_cnt) {
            cur[i] = cur[i - black_cnt];
        } else {
            cur[i] = inf;
        }
    }
    for (int i = 0; i < cur.size(); i++) {
        for (int j = 1; j <= black_cnt && i + j < cur.size(); j++) {
            cur[i] = min(cur[i], cur[i + j] + j);
        }
    }
    cur.resize(max_len);
//    for (int i = 0; i < cur.size(); i++) {
//        cout << cur[i] << " ";
//    }
//    cout << "\n";
}

void dfs(int v, int p) {
    if (g[v].empty()) {
        ans[v] = 0;
        reds[v] = 0;
        blacks[v] = 0;
        dp[v] = {is_black[v], !is_black[v]};
        dist_leaf[v] = 0;
    } else if (g[v].size() == 1) {
        int to = g[v].front();
        dfs(to, v);
        dist_leaf[v] = dist_leaf[to] + 1;
        ans[v] = ans[to];
        reds[v] = reds[to] + !is_black[v];
        blacks[v] = blacks[to] + is_black[v];
        swap(dp[v], dp[to]);
        dp[to].clear();
    } else {
        dist_leaf[v] = inf;
        for (int to : g[v]) {
            dfs(to, v);
            dist_leaf[v] = min(dist_leaf[v], dist_leaf[to] + 1);
        }

        dp[v].assign(dist_leaf[v] + 2, 0);
        for (int to : g[v]) {
            assert(dp[to].size() + reds[to] + blacks[to] == dist_leaf[to] + 2);

            recalc(dp[to], reds[to], blacks[to], dp[v].size());
            for (int i = 0; i < dp[v].size(); i++) {
                add(dp[v][i], dp[to][i]);
            }
            dp[to].clear();
        }

        if (is_black[v]) {
            for (int i = int(dp[v].size()) - 1; i >= 1; i--) {
                dp[v][i] = dp[v][i - 1];
            }
            dp[v][0] = inf;
            for (int i = 0; i + 1 < dp[v].size(); i++) {
                dp[v][i] = min(dp[v][i], dp[v][i + 1] + 1);
            }
        } else {
            dp[v].back() = inf;
            for (int i = int(dp[v].size()) - 1; i >= 1; i--) {
                dp[v][i] = min(dp[v][i], dp[v][i - 1] + 1);
            }
        }

        reds[v] = 0;
        blacks[v] = 0;
        ans[v] = inf;
        for (int i = 0; i < dp[v].size(); i++) {
            ans[v] = ans[v] = min(ans[v], dp[v][i]);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        init(n);
        string cols;
        cin >> cols;
        for (int i = 1; i <= n; i++) {
            is_black[i] = cols[i - 1] == '1';
        }
        for (int i = 2; i <= n; i++) {
            int par;
            cin >> par;
            g[par].push_back(i);
        }
        dfs(1, 1);
        for (int i = 1; i <= n; i++) {
            cout << ans[i];
            if (i != n) {
                cout << " ";
            }
        }
        if (t >= 1) {
            cout << "\n";
        }
    }

#ifdef LOCAL
    cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 8600kb

input:

2
9
101011110
1 1 3 3 3 6 2 2
4
1011
1 1 3

output:

4 1 2 0 0 0 0 0 0
2 0 0 0

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 1811ms
memory: 33388kb

input:

6107
12
000000001000
1 2 3 2 5 4 4 7 3 8 11
19
1100111101111011110
1 2 1 1 4 5 2 4 3 2 2 7 10 2 11 3 15 5
7
0111110
1 1 2 2 1 5
3
000
1 1
7
1000011
1 2 3 3 5 4
7
0001001
1 1 1 3 5 3
8
00111000
1 1 3 2 5 2 7
11
11111110111
1 1 1 4 5 4 5 2 5 1
15
110101101000010
1 2 3 2 1 5 2 5 6 5 8 7 9 14
10
0101000...

output:

1 1 1 1 0 0 0 0 0 0 0 0
6 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0
0 0 0
0 0 0 0 0 0 0
2 0 1 0 0 0 0
2 1 0 0 0 0 0 0
4 0 0 2 1 0 0 0 0 0 0
4 3 0 0 2 0 0 0 0 0 0 0 0 0 0
2 0 1 0 0 0 0 0 0 0
6 5 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
1 1 0 0 0
5 1 0 1 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0
5 3 ...

result:

ok 6107 lines

Test #3:

score: 0
Accepted
time: 182ms
memory: 16512kb

input:

10
100000
10001010000001100001000100001000010100010101100001001110110001000010000110001000000010000011000001000001010001110100000000000000000010011011100000000000001000000000100100100110000000100001010011000000110000000111010100100001100000100100101001000000010000000011100000000000000010001100011100...

output:

22440 21414 19422 13454 5328 2719 1421 1168 1478 661 4662 5037 418 183 2304 501 2008 1643 692 2211 570 1003 967 950 504 124 894 333 775 523 905 197 12 337 195 310 1325 1016 638 50 907 361 179 336 313 102 309 555 733 871 490 414 375 318 66 625 336 267 276 162 203 25 112 216 252 146 42 233 232 333 27 ...

result:

ok 10 lines

Test #4:

score: 0
Accepted
time: 83ms
memory: 13344kb

input:

10
100000
01010111111101011100011111111010011001111111110001100111111101011111110011101111110110111011010111011011010011111110101111111011111111011101011111011001110101111011011010110100011111001101001011111101111101111111111100101101000111111110111101111111111011111100111011101110110101111010101101...

output:

25019 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 10 lines

Test #5:

score: 0
Accepted
time: 98ms
memory: 28832kb

input:

10
100000
00111110110011111111111010011111011111101010110111111110011110111111111111000110101110110111111101011111111111010101111111011001110110011101111001110111101101110110101000011111110100101110110100111110001111011100111101011010111111011011100011111011110111111110011110111111001111111010011100...

output:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 10 lines

Test #6:

score: 0
Accepted
time: 125ms
memory: 26292kb

input:

10
100000
00111100100100001111011000100000000000111001100000000000100000101001001010010000001000010010111000001011010000000000001000000000010100000010010010000001000010001000000100000001010000000000000000000001000110000010100100000010000011000000000010010000100010100000000100000100100011000000001000...

output:

4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 ...

result:

ok 10 lines

Test #7:

score: -100
Time Limit Exceeded

input:

10
100000
00111101111001101110101101111110100001010100011011100001011100000110000100100010111010011001111011100010010011111100000011111011001001000110000101101001011110000000011100001010100001000110110101111010000100000111001110001100001001001000101110100111111000101101100000011001110111001111101011...

output:

210 210 210 210 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result: