14152022-09-09 09:35:31kicsiboglarSzínes facpp11Accepted 50/50194ms49140 KiB
#include <iostream>
#include <vector>
#include <deque>

#define ll long long 

using namespace std;

ll n,i,a,mini=9999999;
struct element
{
    ll step,father;
    vector <ll> sz;
    bool leaf=false;
    ll color;
};
vector <element> x;

void BFS (ll start)
{
    deque<ll> v;
    v.push_back(start);
    x[start].step=1;
    ll curr;
    while (!v.empty())
    {
        curr=v[0];
        v.pop_front();
        if (x[curr].sz.empty())
        {
            x[curr].leaf=true;
            if (x[curr].step<mini) mini=x[curr].step;
            return;
        }
        for (auto &e:x[curr].sz)
        {
            x[e].step=x[curr].step+1;
            v.push_back(e);
        }
    }
}

void paint(ll curr)
{
    ll one=1;
    x[curr].color=max(one,x[x[curr].father].color-1);
    for (auto &e:x[curr].sz) paint(e);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin>>n;
    x.resize(n+1);

    for (i=2;i<=n;++i)
    {
        cin>>a;
        x[a].sz.push_back(i);
        x[i].father=a;
    }
    BFS(1);
    x[0].color=mini+1;
    cout<<mini<<"\n";
    paint(1);
    for (i=1;i<=n;++i) cout<<x[i].color<<" ";
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1828 KiB
2Accepted0/06ms3496 KiB
3Accepted1/12ms2240 KiB
4Accepted4/42ms2312 KiB
5Accepted5/575ms49140 KiB
6Accepted2/2100ms32948 KiB
7Accepted3/3100ms33152 KiB
8Accepted2/2112ms32968 KiB
9Accepted2/2103ms32792 KiB
10Accepted2/2108ms33160 KiB
11Accepted2/2123ms35816 KiB
12Accepted2/2167ms36544 KiB
13Accepted2/2137ms37324 KiB
14Accepted2/2140ms37244 KiB
15Accepted2/2142ms37816 KiB
16Accepted2/2140ms37836 KiB
17Accepted2/2173ms37916 KiB
18Accepted2/2160ms37956 KiB
19Accepted2/2174ms37960 KiB
20Accepted2/2192ms38384 KiB
21Accepted2/2194ms38580 KiB
22Accepted2/2165ms39104 KiB
23Accepted2/2172ms41304 KiB
24Accepted3/3179ms44540 KiB