2162021-02-25 09:58:42kicsiboglarSzínes facpp11Accepted 50/50215ms61348 KiB
#include <iostream>
#include "vector"

using namespace std;

long long n,i,j,db,mini;
struct adat
{
    int lep,apa,szin;
    vector<int> fiu;
};
vector <adat> x;
void melysegi (int csp,int lep)
{
    x[csp].lep=lep;
    for (auto e:x[csp].fiu) melysegi(e,lep+1);
}
void szinezes (int csp, int s)
{
    x[csp].szin=s;
    if (s!=1) --s;
    for (auto e:x[csp].fiu) szinezes(e,s);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>n;
    x.resize(n+1);
    for (i=2;i<=n;++i)
    {
        cin>>j;
        x[j].fiu.push_back(i);
        x[i].apa=j;
    }

    melysegi(1,1);

    int mini=999999;
    for (i=1;i<=n;++i)
    {
        if (x[i].fiu.empty())
        {
            if (x[i].lep<mini) mini=x[i].lep;
        }
    }
    cout<<mini<<"\n";
    szinezes(1,mini);
    for (i=1;i<=n;++i) cout<<x[i].szin<<" ";
    cout<<"\n";
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/02ms1820 KiB
2Accepted0/04ms3216 KiB
3Accepted1/11ms1968 KiB
4Accepted4/41ms1972 KiB
5Accepted5/579ms43796 KiB
6Accepted2/2168ms28060 KiB
7Accepted3/3194ms29336 KiB
8Accepted2/2171ms30056 KiB
9Accepted2/2152ms30292 KiB
10Accepted2/2157ms32168 KiB
11Accepted2/2188ms35900 KiB
12Accepted2/2180ms38740 KiB
13Accepted2/2184ms40500 KiB
14Accepted2/2195ms41928 KiB
15Accepted2/2179ms43600 KiB
16Accepted2/2201ms44960 KiB
17Accepted2/2180ms46280 KiB
18Accepted2/2187ms47556 KiB
19Accepted2/2187ms49116 KiB
20Accepted2/2186ms50424 KiB
21Accepted2/2209ms51800 KiB
22Accepted2/2187ms53480 KiB
23Accepted2/2190ms57052 KiB
24Accepted3/3215ms61348 KiB