23702023-01-11 19:56:23sztomiSzínes facpp11Accepted 50/50180ms38284 KiB
#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9+7;

vector<vector<int>> graf;
vector<int> szinek;
int db;

int sekely(int akt){
    if(graf[akt].size() == 0){
        return 1;
    }

    int ki = INF;
    for(int kov : graf[akt]){
        ki = min(ki, sekely(kov) + 1);
    }
    return ki;
}

void szinez(int akt, int mod){
    mod %= db;
    szinek[akt] = mod+1;
    for(int kov : graf[akt]){
        szinez(kov, mod+1);
    }
}


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n;
    graf.assign(n+1, vector<int>());
    szinek.assign(n+1, -1);
    int szulo;
    for(int i = 2; i <= n; i++){
        cin >> szulo;
        graf[szulo].push_back(i);
    }
    db = sekely(1);
    szinez(1, 0);

    cout << db << "\n";
    for(int i = 1; i <= n; i++){
        cout << szinek[i] << " ";
    }
    cout << "\n";
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1696 KiB
2Accepted0/04ms2684 KiB
3Accepted1/12ms1940 KiB
4Accepted4/42ms2196 KiB
5Accepted5/575ms38284 KiB
6Accepted2/290ms21184 KiB
7Accepted3/397ms21428 KiB
8Accepted2/2128ms20996 KiB
9Accepted2/2101ms20044 KiB
10Accepted2/282ms21048 KiB
11Accepted2/289ms23464 KiB
12Accepted2/297ms25196 KiB
13Accepted2/2180ms25740 KiB
14Accepted2/2129ms26080 KiB
15Accepted2/2114ms26560 KiB
16Accepted2/2104ms26600 KiB
17Accepted2/2108ms26724 KiB
18Accepted2/2126ms27208 KiB
19Accepted2/2123ms27496 KiB
20Accepted2/2123ms27448 KiB
21Accepted2/2123ms27732 KiB
22Accepted2/2130ms28152 KiB
23Accepted2/2137ms30732 KiB
24Accepted3/3163ms33864 KiB