13772022-08-30 10:40:36Valaki2Színes facpp14Accepted 50/50108ms36400 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second

const int maxn = 2e5;

int n;
vector<int> g[maxn + 1];
int dep[maxn + 1];

void dfs(int x) {
    for(int y : g[x]) {
        dep[y] = dep[x] + 1;
        dfs(y);
    }
}

void solve() {
    cin >> n;
    for(int i = 2; i <= n; i++) {
        int p;
        cin >> p;
        g[p].pb(i);
    }
    dep[1] = 1;
    dfs(1);
    int ans = maxn + 1;
    for(int i = 1; i <= n; i++) {
        if(g[i].size() == 0) {
            ans = min(ans, dep[i]);
        }
    }
    cout << ans << "\n";
    for(int i = 1; i <= n; i++) {
        cout << min(ans, dep[i]) << " ";
    }
    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/06ms11324 KiB
2Accepted0/08ms12052 KiB
3Accepted1/16ms11720 KiB
4Accepted4/46ms11992 KiB
5Accepted5/568ms36400 KiB
6Accepted2/283ms21660 KiB
7Accepted3/383ms21580 KiB
8Accepted2/282ms21248 KiB
9Accepted2/275ms20256 KiB
10Accepted2/279ms20944 KiB
11Accepted2/286ms23320 KiB
12Accepted2/296ms24788 KiB
13Accepted2/294ms25292 KiB
14Accepted2/296ms25580 KiB
15Accepted2/298ms25880 KiB
16Accepted2/2101ms25960 KiB
17Accepted2/2101ms26380 KiB
18Accepted2/297ms26624 KiB
19Accepted2/298ms26704 KiB
20Accepted2/298ms26764 KiB
21Accepted2/2100ms27004 KiB
22Accepted2/2101ms27196 KiB
23Accepted2/2107ms29580 KiB
24Accepted3/3108ms32180 KiB