902021-01-10 21:27:36varikakasandorSzínes facpp11Accepted 50/50201ms60056 KiB
#include <bits/stdc++.h>
using namespace std;

int n,m;
int col[200001];
vector<int> g[200001];

int dfs(int x,int depth)
{
    col[x]=depth;
    if(g[x].empty()) return 1;
    int res=n+1;
    for(auto s:g[x]) res=min(res,dfs(s,depth+1)+1);
    return res;
}

int main () {
    cin>>n;
    for(int i=2;i<=n;i++)
    {
        int x;
        cin>>x;
        g[x].push_back(i);
    }
    int min_depth=dfs(1,1);
    cout<<min_depth<<endl;
    for(int i=1;i<=n;i++) cout<<((col[i]<=min_depth) ? col[i] : 1)<<" ";
    cout<<endl;
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/06ms11104 KiB
2Accepted0/010ms11784 KiB
3Accepted1/16ms11244 KiB
4Accepted4/46ms11220 KiB
5Accepted5/5126ms45388 KiB
6Accepted2/2173ms23448 KiB
7Accepted3/3180ms24728 KiB
8Accepted2/2162ms25444 KiB
9Accepted2/2150ms25708 KiB
10Accepted2/2153ms27644 KiB
11Accepted2/2187ms31212 KiB
12Accepted2/2188ms33936 KiB
13Accepted2/2174ms35764 KiB
14Accepted2/2175ms37332 KiB
15Accepted2/2184ms38900 KiB
16Accepted2/2177ms40232 KiB
17Accepted2/2201ms41740 KiB
18Accepted2/2168ms43064 KiB
19Accepted2/2177ms44388 KiB
20Accepted2/2175ms45760 KiB
21Accepted2/2178ms47332 KiB
22Accepted2/2167ms49080 KiB
23Accepted2/2168ms54096 KiB
24Accepted3/3168ms60056 KiB