13722022-08-28 12:07:58csandrasSzínes facpp11Accepted 50/50103ms35156 KiB
#include <bits/stdc++.h>

using namespace std;

int dist(int p, vector<int>& memo, const vector<int>& parent)
{
    if (memo[p] != -1)
    {
        return memo[p];
    }
    return memo[p] = dist(parent[p], memo, parent) + 1;
}

int main()
{
    int N;
    cin >> N;
    vector<int> parent(N);
    vector<bool> is_leaf(N);
    fill(is_leaf.begin(), is_leaf.end(), true);
    for (int i = 1; i < N; ++i)
    {
        int p;
        cin >> p;
        parent[i] = p - 1;
        is_leaf[p - 1] = false;
    }
    vector<int> memo(N);
    fill(memo.begin(), memo.end(), -1);
    memo[0] = 0;

    int min_dist = N;
    for (int i = 0; i < N; ++i)
    {
        int d = dist(i, memo, parent);
        if (is_leaf[i] && d < min_dist)
        {
            min_dist = d;
        }
    }

    cout << min_dist + 1 << '\n';
    for (int i = 0; i < N; ++i)
    {
        cout << dist(i, memo, parent) % (min_dist + 1) + 1 << " ";
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1816 KiB
2Accepted0/06ms2216 KiB
3Accepted1/12ms2264 KiB
4Accepted4/42ms2492 KiB
5Accepted5/596ms7128 KiB
6Accepted2/297ms8456 KiB
7Accepted3/397ms9696 KiB
8Accepted2/297ms11152 KiB
9Accepted2/297ms12732 KiB
10Accepted2/296ms14020 KiB
11Accepted2/297ms15432 KiB
12Accepted2/298ms16988 KiB
13Accepted2/2100ms18284 KiB
14Accepted2/297ms19600 KiB
15Accepted2/298ms21148 KiB
16Accepted2/297ms22292 KiB
17Accepted2/297ms23548 KiB
18Accepted2/298ms24864 KiB
19Accepted2/297ms26128 KiB
20Accepted2/2100ms27428 KiB
21Accepted2/298ms28684 KiB
22Accepted2/298ms30332 KiB
23Accepted2/2100ms32380 KiB
24Accepted3/3103ms35156 KiB