QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#589497#7736. Red Black TreeetherealUnicornWA 1ms6208kbC++142.7kb2024-09-25 18:10:232024-09-25 18:10:23

Judging History

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

  • [2024-09-25 18:10:23]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:6208kb
  • [2024-09-25 18:10:23]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;

const int N = 1e5 + 10;

struct Dif
{
    vi pos, neg;
    int cnt_zero, sum_neg, size;

    Dif() : cnt_zero(0), sum_neg(0), size(0) {};
    Dif(vi vec) : cnt_zero(0), sum_neg(0), size(vec.size())
    {
        for (auto i : vec)
        {
            if (i > 0)
                pos.push_back(i);
            else if (i == 0)
                cnt_zero++;
            else
            {
                neg.push_back(i);
                sum_neg += i;
            }
        }
        reverse(pos.begin(), pos.end());
    }
    int at(int no)
    {
        if (no <= neg.size())
            return neg[no - 1];
        no -= neg.size();
        if (no <= cnt_zero)
            return 0;
        no -= cnt_zero;
        assert(no <= pos.size());
        return pos[pos.size() - no];
    }
    void insert(int x)
    {
        if (x == 1)
            pos.push_back(x);
        else if (x == -1)
        {
            neg.push_back(x);
            sum_neg += x;
        }
        else
            assert(0);
    }
};

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

void init(int n);
Dif solve(int u);

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int t, n;
    cin >> t;
    while (t--)
    {
        cin >> n;
        init(n);
        solve(1);
        for (int i = 1; i <= n; i++)
            cout << ans[i] << " ";
        // cout << ans[i] << (i == n ? "\n" : " ");
        cout << endl;
    }
    return 0;
}

void init(int n)
{
    cin >> color;
    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);
    }
}

Dif solve(int u)
{
    Dif dif;
    red[u] = (color[u - 1] == '1');
    for (auto v : G[u])
    {
        Dif son = solve(v);
        red[u] += red[v];
        if (dif.size == 0)
            dif = son;
        else
        {
            vi tmp;
            int sz = min(dif.size, son.size);
            for (int i = 1; i <= sz; i++)
                tmp.push_back(dif.at(i) + son.at(i));
            dif = Dif(tmp);
        }
    }
    dif.insert(color[u - 1] == '0' ? 1 : -1);
    ans[u] = red[u] + dif.sum_neg;
    // cout << "======================" << endl;
    // cout << "dif of vertex " << u << endl;
    // cout << "size = " << dif.size() << endl;
    // cout << "cnt_zero = " << dif.cnt_zero << endl;
    // cout << "sum_neg = " << dif.sum_neg << endl;
    // for (int i = 1; i <= dif.size(); i++)
    //     cout << dif.at(i) << " ";
    // cout << endl;
    // cout << "======================" << endl;
    return dif;
}

详细

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 6208kb

input:

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

output:

2 1 1 0 0 0 0 0 0 
0 0 0 0 

result:

wrong answer 1st lines differ - expected: '4 1 2 0 0 0 0 0 0', found: '2 1 1 0 0 0 0 0 0 '