15452022-11-23 10:16:12TomaSajtSzínes facpp14Accepted 50/50131ms44724 KiB
#include <bits/stdc++.h>
#define speed ios::sync_with_stdio(0);cin.tie(0)
using namespace std;

vector<vector<int>> g;
vector<int> dist;
int m = INT_MAX;
void dfs(int u) {
    if (g[u].empty()) {
        m = min(m, dist[u]);
    }
    for (auto v : g[u]) {
        dist[v] = dist[u] + 1;
        dfs(v);
    }
}

int main() {
    speed;
    int n;
    cin >> n;
    g.resize(n + 1);
    dist.resize(n + 1);
    for (int i = 2; i <= n; i++) {
        int u; cin >> u;
        g[u].push_back(i);
    }
    dist[1] = 1;
    dfs(1);
    cout << m << '\n';
    for (int i = 1; i <= n; i++) {
        cout << min(m, dist[i]) << ' ';
    }
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1768 KiB
2Accepted0/04ms2860 KiB
3Accepted1/12ms2248 KiB
4Accepted4/42ms2368 KiB
5Accepted5/567ms44724 KiB
6Accepted2/279ms21448 KiB
7Accepted3/386ms21656 KiB
8Accepted2/278ms21320 KiB
9Accepted2/272ms20488 KiB
10Accepted2/275ms21564 KiB
11Accepted2/278ms24000 KiB
12Accepted2/286ms25508 KiB
13Accepted2/2131ms26260 KiB
14Accepted2/287ms26600 KiB
15Accepted2/2114ms27020 KiB
16Accepted2/297ms27064 KiB
17Accepted2/2101ms27720 KiB
18Accepted2/289ms27744 KiB
19Accepted2/2101ms27776 KiB
20Accepted2/2101ms27768 KiB
21Accepted2/2101ms28372 KiB
22Accepted2/2104ms28696 KiB
23Accepted2/2112ms32688 KiB
24Accepted3/397ms37320 KiB