QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#266882#7736. Red Black Treeucup-team2307#WA 215ms28812kbC++202.5kb2023-11-26 18:52:332023-11-26 18:52:34

Judging History

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

  • [2024-02-19 10:14:05]
  • hack成功,自动添加数据
  • (/hack/538)
  • [2023-11-26 18:52:34]
  • 评测
  • 测评结果:WA
  • 用时:215ms
  • 内存:28812kb
  • [2023-11-26 18:52:33]
  • 提交

answer

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;

template<typename F>
void multitest(F func) {
	int t;
	cin >> t;
	while(t--) func();
}
void report(int ok) {
	cout << (ok?"Yes":"No") << '\n';
}

struct DP {
	int best = 0, cc = 0, line_r = 0, line_b = 0, dep = 1, col;
	vector<int> vals { 0 };
	DP(int Col = 0) : col(Col) {
		(col ? line_b : line_r)++;
	}
	void attach(DP &ch) {
		if(cc == 0) {
			swap(vals, ch.vals);
			line_r += ch.line_r;
			line_b += ch.line_b;
			best = ch.best;
			dep = 1 + ch.dep;
			cc = 1;
		} else {
			(col ? ch.line_b : ch.line_r)++;
			dep = min(dep, 1 + ch.dep);
			unfuck(dep);
			ch.unfuck(dep);
			best = 1<<30;
			for(int i = 0; i <= dep; i++) {
				vals[i] += ch.vals[i];
				best = min(best, vals[i]);
			}
		}
	}
	
	void unfuck(int S) {
		// vector<int> ndp(S + 1, n + 100);
		// cout << line_r << " " << line_b << endl;
		int sz = max(1 + S, (int)vals.size());
		for(int z = 1; line_r; z *= 2) {
			z = min(z, line_r);
			line_r -= z;
			vector<int> nvals(sz);
			for(int i = 0; i < sz; i++)
				nvals[i] = (i < vals.size() ? vals[i] : 1<<20);
			for(int i = nvals.size(); i-- > z;)
				nvals[i] = min(nvals[i], nvals[i - z] + z);
			vals = nvals;
		}
		for(int z = 1; line_b; z *= 2) {
			z = min(z, line_b);
			line_b -= z;
			// auto nvals = vals;
			// nvals.insert(nvals.begin(), 1<<20);
			vector<int> nvals(sz);
			for(int i = 0; i < sz; i++)
				nvals[i] = (i - z >= 0 && i - z < (int)vals.size() ? vals[i - z] : 1<<20);
			for(int i = 0; i + z < nvals.size(); i++)
				nvals[i] = min(nvals[i], nvals[i + z] + z);
			vals = nvals;
		}
		vals.resize(S + 1, 1<<20);
		// for(int i = 0; i <= S; i++) {
			// if(i - line_b >= 0)
				// ndp[i] = vals[i - line_b];
		// }
		// for(int z = 1<<30; z>>=1;) {
			// if(line_b) {}
		// }
	}
};

signed main() {
	cin.tie(0)->sync_with_stdio(0);
	int T;cin >>T;
	while(T--) {
		int n;
		cin >> n;
		string col;
		cin >> col;
		vector<vector<int>> g(n);
		for(int t, i = 1; i < n; i++) {
			cin >> t;
			g[t - 1].push_back(i);
		}
		vector<DP> dp(n);
		vector<int> ans(n);
		auto dfs = [&](auto self, int v) -> void {
			dp[v] = DP(col[v] - '0');
			for(auto i : g[v]) {
				self(self, i);
				dp[v].attach(dp[i]);
			}
			ans[v] = dp[v].best;
		};
		dfs(dfs, 0);
		for(auto i : ans) cout << i << " ";
		cout << '\n';
	}	
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3496kb

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
Wrong Answer
time: 215ms
memory: 28812kb

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 
7 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
2 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 
5 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 
2 0 0 0 0 0 0 0 0...

result:

wrong answer 2nd lines differ - expected: '6 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0', found: '7 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '