932021-01-11 20:27:01Babják PéterSzínes facpp11Accepted 50/50144ms28520 KiB
#include <iostream>
#include <vector>
#define N 200001
using namespace std;
int col[N];
vector<int>child[N];
int p=N;
void bfs(int z)
{
	vector<int>sor;
	sor.push_back(z);
	for(int i=0;i<sor.size();i++)
	{
		int v=sor[i];
		for(int u: child[v])
		{
			if(child[u].size()==0 )
			{
				if(p==N)
				{
					col[u]=col[v]+1;
					p=col[u]+1;
				}
				col[u]=(col[v]+1)%p;
				continue;
			}
			sor.push_back(u);
			col[u]=(col[v]+1)%p;
		}
	}
}
int main()
{
	int n;
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>n;
	col[0]=0;
	for(int i=1;i<n;i++)
	{
		int a;cin>>a;a--;
		child[a].push_back(i);
	}
	bfs(0);
	cout<<p<<endl;
	for(int i=0;i<n;i++)
	{
		cout<<col[i]+1<<" ";
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/06ms11084 KiB
2Accepted0/08ms11944 KiB
3Accepted1/14ms11192 KiB
4Accepted4/44ms11196 KiB
5Accepted5/565ms28172 KiB
6Accepted2/2103ms23008 KiB
7Accepted3/3112ms23016 KiB
8Accepted2/2103ms22544 KiB
9Accepted2/297ms21500 KiB
10Accepted2/2103ms22228 KiB
11Accepted2/2122ms25428 KiB
12Accepted2/2122ms26856 KiB
13Accepted2/2126ms27448 KiB
14Accepted2/2128ms27772 KiB
15Accepted2/2126ms28100 KiB
16Accepted2/2128ms28196 KiB
17Accepted2/2144ms28316 KiB
18Accepted2/2127ms28400 KiB
19Accepted2/2130ms28404 KiB
20Accepted2/2127ms28480 KiB
21Accepted2/2126ms28512 KiB
22Accepted2/2128ms28444 KiB
23Accepted2/2136ms28500 KiB
24Accepted3/3135ms28520 KiB