Cod sursa(job #2494653)

Utilizator Dragos1226Dragos Chileban Dragos1226 Data 18 noiembrie 2019 10:58:25
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include<fstream>
#include<iostream>
#include<vector>
using namespace std;
ifstream in("cerere.in");
ofstream out("cerere.out");
const int NMax = 100000;

vector <int> G[NMax+5], Stack;

int N, K[NMax+5], DP[NMax+5];
long long Root;

void Read() {
    in >> N;
    for (int i = 1; i <= N; i++)
        in >> K[i], Root += i;

    for (int i = 1,x,y; i < N; i++) {
        in >> x >> y;
        G[x].push_back(y);
        Root -= y;
    }
}

void DFS(int Node) {
    Stack.push_back(Node);
    if (K[Node])
        DP[Node] = DP[Stack[Stack.size()-1 - K[Node]]] + 1;
    for (auto Vecin : G[Node]) {
        DFS(Vecin);
    }
    Stack.pop_back();
}

void Print() {
    for (int i = 1; i <= N; i++)
        out << DP[i] << ' ';
}

int main() {
    Read();
    DFS(Root);
    Print();
}