Cod sursa(job #2147290)

Utilizator CryshanaGanea Carina Cryshana Data 28 februarie 2018 16:56:40
Problema Cerere Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 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], d[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];
        if ( d[x] == 0 )
        {
            while ( i > 0 )
            {
                y = a[y];
                i--;
            }
            c++;
            dfs(y);
            d[x] = c;
        }
        else
        {
            c +=d[x];
        }
    }
}

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