QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#621467#7736. Red Black TreeSCAU_xyjkanadeRE 0ms0kbC++141.3kb2024-10-08 14:42:112024-10-08 14:42:31

Judging History

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

  • [2024-10-08 14:42:31]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-10-08 14:42:11]
  • 提交

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];
vector <int> cf;
pair<int, vector<int> > dfs(int sn) 
{
	cf.clear();
    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);
        for (int i = 1; i <= n; i++) 
		{
            e[i].clear();
        }
		int x;
        for (int i = 2; i <= n; i++) 
		{
            cin >> x;
            e[x].push_back(i);
        }
        dfs(1);
        for (int i = 1; i <= n; i++) 
		{
            cout << ans[i];
        }
        cout << endl;
    }
    return 0;
}

詳細信息

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: