1142021-01-13 08:31:08kidesoSzínes facpp11Accepted 50/50209ms39204 KiB
#include <iostream>
#include <vector>

using namespace std;

//ifstream cin("szin.in");
//ofstream cout("szin.out");

struct adat
{
    int apa, tav = 0;
    vector <int> el;
};

vector <adat> x;
int N, i, a, k;

void mely(int csp)
{
    for(auto e:x[csp].el)
        if (x[e].tav == 0)
        {
            x[e].tav = x[csp].tav + 1;
            mely(e);
        }
}

int main()
{
    cin >> N;

    k = N + 1;
    x.resize(N + 1);
    x[1].apa = -1;
    x[1].tav = 1;
    for (i = 2; i <= N; ++i)
    {
        cin >> a;
        x[a].el.push_back(i);
        x[i].apa = a;
    }

    mely(1);

    for (i = 1; i <= N; ++i)
        if (x[i].el.empty() && x[i].tav < k) k = x[i].tav;

    cout << k << '\n';

    for (i = 1; i <= N; ++i)
        if (x[i].tav <= k) cout << x[i].tav << ' ';
        else cout << 1 << ' ';
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms1804 KiB
2Accepted0/06ms2680 KiB
3Accepted1/11ms1892 KiB
4Accepted4/41ms1784 KiB
5Accepted5/5111ms39204 KiB
6Accepted2/2163ms22244 KiB
7Accepted3/3174ms22284 KiB
8Accepted2/2180ms21752 KiB
9Accepted2/2153ms20728 KiB
10Accepted2/2173ms21320 KiB
11Accepted2/2173ms23816 KiB
12Accepted2/2180ms25336 KiB
13Accepted2/2178ms25880 KiB
14Accepted2/2175ms26020 KiB
15Accepted2/2175ms26436 KiB
16Accepted2/2179ms26480 KiB
17Accepted2/2184ms26588 KiB
18Accepted2/2180ms26612 KiB
19Accepted2/2175ms26636 KiB
20Accepted2/2179ms26956 KiB
21Accepted2/2185ms27048 KiB
22Accepted2/2174ms27484 KiB
23Accepted2/2209ms29792 KiB
24Accepted3/3187ms32956 KiB