Cod sursa(job #1912140)

Utilizator nicu_89Lari Nicolae nicu_89 Data 7 martie 2017 23:31:26
Problema Cerere Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <fstream>

using namespace std;

struct monkey
{
    int father;
    int ancestors;
    int k;
};

int main()
{
    ifstream f("cerere.in");
    ofstream g("cerere.out");

    int N, temp;

    f >> N;

    monkey *M;

    M = new monkey[N];

    for (int i=0; i<N; i++)
    {
        f >> M[i].k;
    }

    int A, B;

    while (f >> A >> B)
    {
        M[B-1].father = A-1;
    }

    M[0].father = 0;

    for (int i=0; i<N; i++)
    {
        M[i].ancestors = 0;

        temp = M[i].father;

        for (int k = 1; k<M[i].k; k++)
            temp = M[temp].father;


        if (M[i].k != 0)
        {
            M[i].ancestors = M[temp].ancestors + 1;
            g << M[i].ancestors << " ";
        }
        else
            g << 0 << " ";

    }


}