13722022-08-28 12:07:58csandrasSzínes facpp11Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms1816 KiB
2Elfogadva0/06ms2216 KiB
3Elfogadva1/12ms2264 KiB
4Elfogadva4/42ms2492 KiB
5Elfogadva5/596ms7128 KiB
6Elfogadva2/297ms8456 KiB
7Elfogadva3/397ms9696 KiB
8Elfogadva2/297ms11152 KiB
9Elfogadva2/297ms12732 KiB
10Elfogadva2/296ms14020 KiB
11Elfogadva2/297ms15432 KiB
12Elfogadva2/298ms16988 KiB
13Elfogadva2/2100ms18284 KiB
14Elfogadva2/297ms19600 KiB
15Elfogadva2/298ms21148 KiB
16Elfogadva2/297ms22292 KiB
17Elfogadva2/297ms23548 KiB
18Elfogadva2/298ms24864 KiB
19Elfogadva2/297ms26128 KiB
20Elfogadva2/2100ms27428 KiB
21Elfogadva2/298ms28684 KiB
22Elfogadva2/298ms30332 KiB
23Elfogadva2/2100ms32380 KiB
24Elfogadva3/3103ms35156 KiB