70332023-12-28 09:41:49BenedekMartonSzínes facpp11Accepted 50/50101ms7640 KiB
#include <iostream>

using namespace std;

int main()
{
    int n, m=0;
    cin >> n;
    int osv[n+1], szin=0, szt[n+1], msz;
    bool vov[n+1];
    for(int i=1; i<=n; i++)
    {
        vov[i]=0;
    }
    for(int i=2; i<n+1; i++)
    {
        cin >> osv[i];
        szt[i]=0;
        if(vov[osv[i]]==0)
        {
            vov[osv[i]]=1;
            m++;
        }

    }
    szt[1]=0;
    m=n-m;
    int akt[m], k=0;
    for(int i=1; k<m; i++)
    {
        if(vov[i]==0)
        {
            akt[k]=i;
            k++;
        }
    }
    bool megye=1;
    while(megye)
    {
        szin++;
        for(int i=0; i<m; i++)
        {
            if(szt[akt[i]]==0)
            {
                szt[akt[i]]=szin;
            }
            if(akt[i]==1)
            {
                megye=0;
            }
            else
            {
                akt[i]=osv[akt[i]];
            }

        }
    }
    cout << szin << endl;
    for(int i=1; i<=n; i++)
    {
        if(szt[i]==0)
        {
            szt[i]=1;
        }
        cout << szt[i] << " ";
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms2060 KiB
2Accepted0/07ms2432 KiB
3Accepted1/13ms2180 KiB
4Accepted4/43ms2412 KiB
5Accepted5/593ms6284 KiB
6Accepted2/293ms6900 KiB
7Accepted3/396ms7000 KiB
8Accepted2/294ms7116 KiB
9Accepted2/2101ms7436 KiB
10Accepted2/297ms7600 KiB
11Accepted2/297ms7400 KiB
12Accepted2/297ms7396 KiB
13Accepted2/296ms7308 KiB
14Accepted2/296ms7284 KiB
15Accepted2/294ms7388 KiB
16Accepted2/296ms7472 KiB
17Accepted2/294ms7332 KiB
18Accepted2/294ms7332 KiB
19Accepted2/296ms7324 KiB
20Accepted2/2101ms7328 KiB
21Accepted2/296ms7320 KiB
22Accepted2/293ms7612 KiB
23Accepted2/296ms7640 KiB
24Accepted3/397ms7624 KiB