64622023-12-02 16:30:17horvathabelSzínes facpp17Accepted 50/50195ms59308 KiB
#include <bits/stdc++.h>
using namespace std;
vector<int> g[200001];
vector<int> tav;
vector<bool> vangyereke;
int bfsminmely(){
    queue<int> q;
    int mntav=20000001;
    tav[1]=1;
    q.push(1);
    while (!q.empty()){
        int v=q.front();
        q.pop();
        for (int edge:g[v]){
            tav[edge]=tav[v]+1;
            q.push(edge);
        }
        if(!vangyereke[v]) mntav=min(mntav,tav[v]);
    }
    return mntav;
}
vector<int> color;
void dfs(int x,int clm){
    color[x]=clm;
    if (clm>1) clm--;
    for (int edge:g[x]) dfs(edge,clm);
}
int main()
{
    int n;
    cin>>n;
    tav.resize(n+1,0);
    color.resize(n+1,-1);
    vangyereke.resize(n+1,false);
    for (int i=2; i<=n;i++){
        int p;
        cin>>p;
        g[p].push_back(i);
        vangyereke[p]=true;
    }
    int colordb=bfsminmely();
    cout<<colordb<<endl;
    dfs(1,colordb);
    for (int i=1; i<=n;i++) cout<<color[i]<<" ";
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/06ms11236 KiB
2Accepted0/010ms12224 KiB
3Accepted1/17ms11856 KiB
4Accepted4/46ms11780 KiB
5Accepted5/5120ms37968 KiB
6Accepted2/2142ms25804 KiB
7Accepted3/3140ms27132 KiB
8Accepted2/2167ms28096 KiB
9Accepted2/2142ms28984 KiB
10Accepted2/2145ms31072 KiB
11Accepted2/2144ms34968 KiB
12Accepted2/2152ms37368 KiB
13Accepted2/2156ms39160 KiB
14Accepted2/2177ms40552 KiB
15Accepted2/2173ms42456 KiB
16Accepted2/2179ms43976 KiB
17Accepted2/2179ms45464 KiB
18Accepted2/2178ms46844 KiB
19Accepted2/2164ms48252 KiB
20Accepted2/2178ms49492 KiB
21Accepted2/2177ms51116 KiB
22Accepted2/2179ms52368 KiB
23Accepted2/2195ms55700 KiB
24Accepted3/3172ms59308 KiB