QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#621473#7736. Red Black TreeSCAU_xyjkanadeRE 0ms0kbC++141.4kb2024-10-08 14:45:012024-10-08 14:45:12

Judging History

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

  • [2024-10-08 14:45:12]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-10-08 14:45:01]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
int T;
int ans[N];
char s[N];
vector<int> e[N];

pair<int, vector<int>> dfs(int sn) {
    vector<int> cf;
    int v = 0;
    
    for (int i : e[sn]) {
        auto [val, depth] = dfs(i);
        v += val;
        if (cf.empty()) {
            cf = depth;
        } else {
            int sz = min(cf.size(), depth.size());
            for (int i = 0; i < sz; i++) {
                cf[i] += depth[i];
            }
        }
    }
    
    v += s[sn] - '0';
    if (s[sn] == '0') {
        cf.push_back(1);
    } else {
        cf.push_back(-1);
        for (int& d : cf) d--;
    }
    
    ans[sn] = v + accumulate(begin(cf), end(cf), 0);
    return {v, cf};
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    cin >> T;
    while (T--) {
        cin >> n;
        scanf("%s", s + 1); // 使用 scanf 读取字符串,因为可能包含空格
        for (int i = 1; i <= n; i++) {
            e[i].clear();
        }
        
        for (int i = 2; i <= n; i++) {
            int x;
            cin >> x;
            e[x].push_back(i);
        }
        
        dfs(1);
        
        for (int i = 1; i <= n; i++) {
            cout << ans[i] << (i < n ? " " : "\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

output:


result: