QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#376945#7736. Red Black TreeciuimWA 0ms23040kbC++141.9kb2024-04-04 19:27:292024-04-04 19:27:31

Judging History

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

  • [2024-04-04 19:27:31]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:23040kb
  • [2024-04-04 19:27:29]
  • 提交

answer

#include <cstdio>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <cstring>
#include <array>
#include <algorithm>
#include <queue>
#include <vector>
#include <bitset>
#include <ctime>
#include <cstdlib>
#include <random>
#include <set>
#include <ctime>
#include <map>
#include <stack>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define reg register
#define fo(a,b,c) for(reg ll a=b;a<=c;++a)
#define re(a,b,c) for(reg ll a=b;a>=c;--a)
#define pii pair<ll,ll>
#define fi first
#define pb push_back
#define se second
#define ite set<ll> ::iterator
using namespace std;
const ll mod=998244353;
inline ll gi()
{
	ll x = 0, f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
	{
		x = (x<<1) + (x<<3) + (ch^48);
		ch = getchar();
	}
	return x * f;
}
ll _=1;
const ll N=300005,inf=1e17;
priority_queue<ll> q[N];
ll n,zr[N],ans[N],tmp[N];
vector<ll> g[N];
char s[N];
void dfs(ll u)
{
	ll ok=0;
	for(ll v:g[u])
	{
		dfs(v);
		if(ok==0)
		{
			ok=1;
			swap(q[u],q[v]);
			zr[u]=zr[v];
			ans[u]=ans[v];
			continue;
		}
		ll msz=min(q[u].size(),q[v].size());
		fo(i,0,msz-1)
		{
			tmp[i]=q[u].top()+q[v].top();
			q[u].pop();
			q[v].pop();
		}
		zr[u]+=zr[v];
		ans[u]=zr[u];
		while(!q[u].empty()) q[u].pop();
		fo(i,0,msz-1)
		{
			ans[u]=min(ans[u],ans[u]-tmp[i]);
			q[u].push(tmp[i]);
		}
	}
	if(s[u]=='0') q[u].push(-1);
	else
	{
		q[u].push(1);
		zr[u]++;
	}
//	cout<<"DFS "<<u<<" "<<zr[u]<<" \n";
}
void sol()
{
	n=gi();
	cin>>s+1;
	fo(i,2,n)
	{
		ll x=gi();
		g[x].pb(i);
	}
	dfs(1);
	fo(i,1,n)
	{
		cout<<ans[i]<<" ";
	}
}
int main()
{
//	freopen("flip.in","r",stdin);
//	freopen("flip.out","w",stdout);
	_=gi();
	while(_--)
	{
		sol();
		printf("\n");
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 23040kb

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 
21 3 10 0 

result:

wrong answer 2nd lines differ - expected: '2 0 0 0', found: '21 3 10 0 '