23422023-01-10 18:20:50nmarciSzínes facpp11Accepted 50/50243ms38684 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long int;
const ll inf = 1e9;

const ll mod = 20210108;

vector<int> g[200100];

int dfs(int node){
  if(g[node].size() == 0) return 1;
  int min_d = numeric_limits<int>::max();
  for(auto i : g[node]){
    min_d = min(dfs(i), min_d);
  }
  return min_d + 1;
}

int sol[200100];

void color(int node, int min_d, int level = 0){
  sol[node] = level % min_d + 1;
  for(auto i : g[node]){
    color(i, min_d, level + 1);
  }
}

int main()
{
  int n;
  cin >> n;
  for(int i = 2; i <= n; ++i){
    int a;
    cin >> a;
    g[a].push_back(i);
  }
  int min_depth = dfs(1);
  cout << min_depth << endl;
  color(1, min_depth);
  for(int i = 1; i <= n; ++i)
    cout << sol[i] << " ";
  return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/06ms11308 KiB
2Accepted0/010ms12172 KiB
3Accepted1/16ms11708 KiB
4Accepted4/46ms11916 KiB
5Accepted5/5125ms38684 KiB
6Accepted2/2200ms21876 KiB
7Accepted3/3194ms21956 KiB
8Accepted2/2158ms21416 KiB
9Accepted2/2141ms20412 KiB
10Accepted2/2150ms21456 KiB
11Accepted2/2143ms23708 KiB
12Accepted2/2159ms25076 KiB
13Accepted2/2151ms25876 KiB
14Accepted2/2153ms26228 KiB
15Accepted2/2157ms26504 KiB
16Accepted2/2157ms26796 KiB
17Accepted2/2162ms26992 KiB
18Accepted2/2178ms27276 KiB
19Accepted2/2178ms27244 KiB
20Accepted2/2243ms27548 KiB
21Accepted2/2180ms27980 KiB
22Accepted2/2187ms28344 KiB
23Accepted2/2202ms30872 KiB
24Accepted3/3173ms34220 KiB