13812022-08-30 11:14:18szilSzínes facpp14Accepted 50/50170ms44684 KiB
#include <bits/stdc++.h>

using namespace std;

vector<vector<int>> g;
vector<int> s;
int ans = INT_MAX;


void dfs(int u, int p, int depth) {
    s[u] = depth;
    if (!g[u].size()) ans = min(ans, depth);
    for (int v : g[u]) {
        if (v != p)
            dfs(v, u, depth+1);
    }
}

int main() {
    int n;
    cin >> n;
    g.resize(n+1);
    s.resize(n+1);
    for (int i=2;i<=n;i++) {
        int a;
        cin >> a;
        g[a].push_back(i);
    }

    dfs(1, 1, 1);
    cout << ans << endl;
    for (int i=1;i<=n;i++) {
        cout << min(ans, s[i]) << " ";
    }
    cout << endl;
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1748 KiB
2Accepted0/07ms2740 KiB
3Accepted1/12ms2264 KiB
4Accepted4/42ms2392 KiB
5Accepted5/5126ms44684 KiB
6Accepted2/2135ms21624 KiB
7Accepted3/3135ms21832 KiB
8Accepted2/2130ms21496 KiB
9Accepted2/2123ms20536 KiB
10Accepted2/2125ms21184 KiB
11Accepted2/2129ms23660 KiB
12Accepted2/2135ms25404 KiB
13Accepted2/2151ms26016 KiB
14Accepted2/2138ms26396 KiB
15Accepted2/2143ms26848 KiB
16Accepted2/2140ms26844 KiB
17Accepted2/2140ms26972 KiB
18Accepted2/2140ms27364 KiB
19Accepted2/2142ms27496 KiB
20Accepted2/2141ms27496 KiB
21Accepted2/2142ms27656 KiB
22Accepted2/2144ms28144 KiB
23Accepted2/2149ms32108 KiB
24Accepted3/3170ms36748 KiB