QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#589569#7736. Red Black TreeetherealUnicornTL 1ms5936kbC++142.1kb2024-09-25 18:36:272024-09-25 18:36:27

Judging History

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

  • [2024-09-25 18:36:27]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:5936kb
  • [2024-09-25 18:36:27]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;

const int N = 1e5 + 10;

int ans[N], red[N];
vi G[N];
string color;

struct Magic
{
    vi neg, pos;
    int zero, negSm;

    Magic() : zero(0), negSm(0) {};
    Magic(vi vec) : zero(0), negSm(0)
    {
        for (int x : vec)
        {
            if (x < 0)
                neg.push_back(x), negSm += x;
            else if (x == 0)
                zero++;
            else
                pos.push_back(x);
        }
        reverse(pos.begin(), pos.end());
    }

    int size()
    {
        return neg.size() + pos.size() + zero;
    }
    int at(int idx)
    {
        if (idx < neg.size())
            return neg[idx];
        else if (idx < neg.size() + zero)
            return 0;
        else
        {
            idx -= neg.size() + zero;
            return pos[pos.size() - 1 - idx];
        }
    }
    void insert(int x)
    {
        assert(x == 1 || x == -1);
        if (x == 1)
            pos.push_back(1);
        else
            neg.push_back(-1), negSm--;
    }
};

Magic dfs(int u)
{
    red[u] = color[u - 1] - '0';
    Magic magic;

    for (int v : G[u])
    {
        Magic tmp = dfs(v);
        red[u] += red[v];
        if (magic.size() == 0)
            magic = tmp;
        else
        {
            int sz = min(magic.size(), tmp.size());
            vector<int> vec(sz);
            for (int i = 0; i < sz; i++)
                vec[i] = magic.at(i) + tmp.at(i);
            magic = Magic(vec);
        }
    }
    if (color[u - 1] == '0')
        magic.insert(1);
    else
        magic.insert(-1);

    ans[u] = red[u] + magic.negSm;
    return magic;
}

void solve(int n)
{
    for (int i = 1; i <= n; i++)
        G[i].clear();
    int par;
    for (int i = 2; i <= n; i++)
    {
        cin >> par;
        G[par].push_back(i);
    }
    dfs(1);
}

int main()
{
    int t, n;
    cin >> t;
    while (t--)
    {
        cin >> n >> color;
        solve(n);
        for (int i = 1; i <= n; i++)
            cout << ans[i] << " ";
        cout << endl;
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5936kb

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: -100
Time Limit Exceeded

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...

result: