13822022-08-30 11:29:09ZsofiaKeresztelySzínes facpp14Accepted 50/50151ms27624 KiB
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g;
vector<int> d;
int c = 0;

void bfs(int start){
    queue<int> q;
    q.push(start);
    d[start] = 1;
    int v;
    while (!q.empty()){
        v = q.front();
        q.pop();
        if (!c && !g[v].size()){
            c = d[v];
        }
        for (int x : g[v]){
            d[x] = d[v] + 1;
            q.push(x);
        }
    }
}

int main() {
    int n, a;
    cin >> n;
    g.resize(n+1);
    d.assign(n+1, -1);
    for (int i=2; i<n+1; i++){
        cin >> a;
        g[a].push_back(i);
    }
    bfs(1);
    cout << c << "\n";
    for (int i=1; i<n+1; i++){
        cout << min(d[i], c) << " ";
    }
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1812 KiB
2Accepted0/07ms2884 KiB
3Accepted1/12ms2260 KiB
4Accepted4/42ms2500 KiB
5Accepted5/5115ms26264 KiB
6Accepted2/2128ms21716 KiB
7Accepted3/3136ms21948 KiB
8Accepted2/2135ms21836 KiB
9Accepted2/2129ms21176 KiB
10Accepted2/2134ms22012 KiB
11Accepted2/2137ms24288 KiB
12Accepted2/2143ms25640 KiB
13Accepted2/2143ms26048 KiB
14Accepted2/2146ms26172 KiB
15Accepted2/2137ms26864 KiB
16Accepted2/2137ms26852 KiB
17Accepted2/2149ms26896 KiB
18Accepted2/2149ms26896 KiB
19Accepted2/2140ms26940 KiB
20Accepted2/2137ms27456 KiB
21Accepted2/2151ms27560 KiB
22Accepted2/2142ms27612 KiB
23Accepted2/2146ms27556 KiB
24Accepted3/3151ms27624 KiB