13862022-08-30 14:00:44gtgSzínes facpp17Accepted 50/50195ms38464 KiB
#include <bits/stdc++.h>
using namespace std;
int mini = INT_MAX;
vector<vector<int> > g;
vector<int> d;
void dfs(int n, int depth)
{
    d[n] = depth;
    if(g[n].size() == 0) mini = min(mini, depth);
    for(auto x : g[n])
    {
        dfs(x, depth + 1);
    }
}
int main()
{
    int n;
    cin >> n;
    g.resize(n + 1);
    d.resize(n + 1);
    int x;
    for(int i = 2; i <= n; i++)
    {
        cin >> x;
        g[x].push_back(i);
    }
    dfs(1, 1);
    cout << mini << "\n";
    for(int i = 1; i <= n; i++)
    {
        cout << min(mini, d[i]) << " ";
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms2152 KiB
2Accepted0/07ms3112 KiB
3Accepted1/12ms2260 KiB
4Accepted4/42ms2320 KiB
5Accepted5/5123ms38464 KiB
6Accepted2/2140ms21484 KiB
7Accepted3/3140ms21564 KiB
8Accepted2/2137ms21172 KiB
9Accepted2/2131ms20264 KiB
10Accepted2/2135ms20944 KiB
11Accepted2/2143ms23416 KiB
12Accepted2/2150ms24852 KiB
13Accepted2/2152ms25500 KiB
14Accepted2/2141ms25872 KiB
15Accepted2/2172ms26244 KiB
16Accepted2/2143ms26476 KiB
17Accepted2/2143ms26908 KiB
18Accepted2/2141ms27100 KiB
19Accepted2/2187ms27744 KiB
20Accepted2/2195ms27792 KiB
21Accepted2/2144ms27696 KiB
22Accepted2/2143ms28496 KiB
23Accepted2/2163ms30880 KiB
24Accepted3/3168ms34084 KiB