Cod sursa(job #2542593)

Utilizator lucianul31Moisii Lucian lucianul31 Data 10 februarie 2020 11:31:44
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <iostream>
#include<bits/stdc++.h>

using namespace std;

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

const int NMAX=1e5+10;
int K[NMAX], Father[NMAX], n, D[NMAX];
vector < int > Edge[NMAX], Depth;

void Read()
{
    int x, y;
    in>>n;
    for(int i=1; i<=n; i++)
        in>>K[i];
    for(int i=1; i<n; i++)
        in>>x>>y, Father[y]=x, Edge[x].push_back(y);
}

void DFS(int x)
{
    Depth.push_back(x);
    if(K[x]>0)
        D[x]=D[Depth[Depth.size()-K[x]-1]] + 1;
    for(int i : Edge[x])
        DFS(i);
    Depth.pop_back();
}

int main()
{
    Read();
    int Root;
    for(int i=1; i<=n; i++)
        if(Father[i]==0)
            Root=i;
    DFS(Root);
    for(int i=1; i<=n; i++)
        out<<D[i]<<' ';
    return 0;
}