QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#538#332118#7736. Red Black TreebobbilykingbobbilykingSuccess!2024-02-19 10:13:482024-02-19 10:13:48

詳細信息

Extra Test:

Time Limit Exceeded

input:

10
100000
01010000101101000000110101011111001000111011011011001110111011101110010000100110111110011001011001110100100110110101111101101101110011101101001001101111110010011001011010011001101011011100101010100000110111011111100100100011100000001000100001010110000110101010000100100111101110010011001000...

output:

207 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 51 39 0 ...

result:


ID题目提交者结果用时内存语言文件大小提交时间测评时间
#332118#7736. Red Black TreebobbilykingTL 269ms48476kbC++202.6kb2024-02-19 09:18:162024-02-19 10:16:13

answer

#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")

#include<bits/stdc++.h>
#include<math.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef vector<ll> vl;
#define FD(i, r, l) for(ll i = r; i > (l); --i)

#define K first
#define V second
#define G(x) ll x; cin >> x;
#define GD(x) ld x; cin >> x;
#define GS(s) string s; cin >> s;
#define EX(x) { cout << x << '\n'; exit(0); }
#define A(a) (a).begin(), (a).end()
#define F(i, l, r) for (ll i = l; i < (r); ++i)

#define NN 100010

ll s[NN];
ll ans[NN];
vl adj[NN];

struct info {
    ll c[2] = {}; // reds, blacks
    vl dp; // dp vector, dp[i] = minimum amount to get dp[i] 
};

info dfs(ll i) {
    if (!adj[i].size()) {
        ans[i] = 0;
        info ret;
        ret.dp.assign(2, 0); 
        ret.dp[!s[i]] = 1;
        return ret; 
    } else if (adj[i].size() == 1) {
        ll only = adj[i][0];
        auto ret = move(dfs(only));
        ans[i] = ans[only];
        ret.c[s[i]]++;
        return ret;
    }   
    vector<info> children;
    ll dep = 1e9;
    for (auto x: adj[i]) {
        children.push_back(dfs(x));
        auto &bck = children.back();
        dep = min(dep, bck.c[0] + bck.c[1] + ssize(bck.dp));
    }
    info here;
    here.dp.resize(dep);

    for (const auto &[c, dp]: children) {
        ll potentialOffset = c[0] + c[1];
        vl ndp(dep, 1e9);
        F(i, 0, ssize(dp)) {
            F(j, 0, potentialOffset+1) if (i + j < dep) {
                ndp[i + j] = min(ndp[i + j], dp[i] + abs(c[1] - j));
            } 
        }

        F(i, 0, dep) here.dp[i] += ndp[i];
    }

    here.dp.push_back(1e9); // cost of making everything black has to be included here

    // simulate shifting up/down depending on red/black
    {
        vl move[2] {here.dp, here.dp};
        move[1].insert(move[1].begin(), 1e9); // shift everything up by one 
        for (auto &x: move[!s[i]]) ++x;
        F(i, 0, ssize(here.dp)) here.dp[i] = min(move[0][i], move[1][i]);
    }

    ans[i] = *min_element(A(here.dp));

    return here;
}   

int main(){
//    freopen("a.in", "r", stdin);
//    freopen("a.out", "w", stdout);

    ios_base::sync_with_stdio(false); cin.tie(0);
    cout << fixed << setprecision(20);
    
    G(t) while(t--) {
        G(n)
        GS(str)
        F(i, 0, n) s[i] = str[i] - '0';
        F(i, 0, n) adj[i].clear();
        F(i, 1, n) {
            G(x) adj[x-1].push_back(i);
        }
        dfs(0);
        F(i, 0, n) cout << ans[i] << " "; cout << '\n';
    }
}