13792022-08-30 10:49:20HVMarciSzínes facpp14Accepted 50/50171ms42908 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long


int n, minmelyseg = LLONG_MAX/2;
vector<int> parent, melyseg;
vector<vector<int> > g;

void dfs(int v, int s) {
    melyseg[v] = s;
    if (g[v].size() == 0) {
        if (s < minmelyseg) {
            minmelyseg = s;
        }
    }
    for (int u : g[v]) {
        dfs(u, s + 1);
    }
}

main() {
    cin >> n;
    parent.resize(n + 1, 0);
    g.resize(n + 1);
    melyseg.resize(n + 1);

    for (int i = 2; i <= n; i++) {
        cin >> parent[i];
        g[parent[i]].push_back(i);
    }

    dfs(1, 1);

    cout << minmelyseg << endl;

    for (int i = 1; i <= n; i++) {
        cout << (melyseg[i] <= minmelyseg ? melyseg[i] : 1) << " ";
    }
    cout << endl;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1752 KiB
2Accepted0/08ms3132 KiB
3Accepted1/12ms1988 KiB
4Accepted4/42ms2116 KiB
5Accepted5/5123ms42908 KiB
6Accepted2/2135ms26616 KiB
7Accepted3/3146ms26820 KiB
8Accepted2/2143ms26376 KiB
9Accepted2/2136ms26084 KiB
10Accepted2/2140ms26544 KiB
11Accepted2/2137ms28492 KiB
12Accepted2/2151ms30140 KiB
13Accepted2/2152ms30608 KiB
14Accepted2/2142ms30880 KiB
15Accepted2/2160ms31572 KiB
16Accepted2/2156ms31652 KiB
17Accepted2/2157ms31652 KiB
18Accepted2/2157ms31780 KiB
19Accepted2/2145ms31680 KiB
20Accepted2/2143ms32204 KiB
21Accepted2/2143ms32280 KiB
22Accepted2/2145ms32928 KiB
23Accepted2/2157ms35296 KiB
24Accepted3/3171ms38572 KiB