Cod sursa(job #2494640)

Utilizator Dragos1226Dragos Chileban Dragos1226 Data 18 noiembrie 2019 10:47:42
Problema Cerere Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 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];

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

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

void DFS(int Node) {
    Stack.push_back(Node);
    if (K[Node])
        DP[Node] = DP[Stack[Stack.size()-1 - K[Node]]] + 1,cout << Node << " " <<Stack[Stack.size() - K[Node]] << '\n';
    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(1);
    Print();
}