QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#113553#6638. TreelectionAvavaAvaWA 6ms34428kbC++141.4kb2023-06-18 15:34:142023-06-18 15:34:14

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-18 15:34:14]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:34428kb
  • [2023-06-18 15:34:14]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;
vector<int> e[1000005];
int sz[1000005],fa[1000005],f[1000005],g[1000005],ok[1000005];
int k,n;
void dfs2(int u)
{
	int sum=0;
	for(auto v:e[u]) sum+=f[v]-min(f[v],k)+1;
	for(auto v:e[u])
	{
		if((sum-(f[v]-min(f[v],k)))>g[u]) g[v]=-1;
		else g[v]=k-(sum-(f[v]-min(f[v],k)))+k;
	}
	for(auto v:e[u]) dfs2(v);
}
void dfs1(int u)
{
	f[u]=0;
	for(auto v:e[u]) dfs1(v),f[u]+=f[v]-min(f[v],k)+1;
}
bool check(int S)
{
	k=S-2;
	if(k<0) return 0;
	dfs1(1),g[1]=k,dfs2(1);
	int flag=0;
	for(int i=1;i<=n;i++)
	{
		if(g[i]>=0&&sz[i]==S)
		{
			ok[i]=1;
			flag=1;
		}
	}
	return flag;
}
void solve()
{
	for(int i=1;i<=n;i++) sz[i]=0,e[i].clear(),ok[i]=0;
	cin >> n;
	vector<int> S;
	for(int i=2;i<=n;i++)
	{
		cin >> fa[i];
		e[fa[i]].push_back(i);
	}
	for(int i=n;i>=1;i--) sz[fa[i]]+=++sz[i],S.push_back(sz[i]);
	sort(S.begin(),S.end());
	int l=0,r=n-1,p=n;
	while(l<=r)
	{
		int mid=(l+r)/2;
		if(check(S[mid])) p=S[mid],r=mid-1;
		else l=mid+1;
	}
	cout << check(3) << "!\n";
	for(int i=1;i<=n;i++) if(sz[i]>p) ok[i]=1;
	for(int i=1;i<=n;i++) cout << ok[i];
	cout << "\n";
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T;
	cin >> T;
	while(T--) solve();
	return 0;
}
/*
2
7
1 2 2 1 5 5
7
1 2 2 1 5 6


5
4
1 2 3
5
1 1 2 2
8
1 2 3 4 5 6 7
7
1 2 2 1 4 5
7
1 2 2 1 5 6
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 6ms
memory: 34428kb

input:

2
4
1 2 3
5
1 1 2 2

output:

1!
1100
0!
10000

result:

wrong answer 1st lines differ - expected: '1100', found: '1!'