Cod sursa(job #2147255)

Utilizator CryshanaGanea Carina Cryshana Data 28 februarie 2018 16:36:47
Problema Cerere Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

ifstream fin  ("cerere.in" );
ofstream fout ("cerere.out");

const int N = 100001;
int a[N];
int  n;
int v[N], c;
bool m[N];

void citire()
{
    fin >> n;
    int x, y;
    for ( int i = 1 ; i <= n ; i++ )
    {
        fin >> v[i];
    }
    for ( int i = 1 ; i <= n ; i++ )
    {
        fin >> x >> y;
        a[y] = x;
    }
}

void dfs ( int x )
{
    if ( v[x] != 0 )
    {
        int i = v[x] - 1, y = a[x];
        while ( i > 0 )
        {
            y = a[y];
            i--;
        }
        c++;
        dfs(y);
    }
}

int main()
{
    citire();
    for ( int i = 1 ; i <= n ; i ++ )
    {
        c = 0;
        dfs(i);
        fout << c << " ";
    }
    return 0;
}