Cod sursa(job #2146198)

Utilizator cyber_ghSoltan Gheorghe cyber_gh Data 27 februarie 2018 21:00:45
Problema Cerere Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("cerere.in");
ofstream fout("cerere.out");
int N,DP[100010],K[100010];
vector<int> V[100010];
vector<int> P;
void dfs(int nod,int parent,int depth){
    DP[nod] = (K[nod] == 0?0:DP[P[P.size() - K[nod] - 1]] + 1);
//    cout << nod << " " << parent<<" "<<depth<<endl<<"path: ";
//    for (auto it:P) cout << it << " ";
//    cout <<endl;
    for (auto it:V[nod]){
        if (it == parent) continue;
        P.push_back(it);
        dfs(it,nod,depth + 1);
        P.pop_back();
    }
}

int main(){
    fin >> N;
    for (int i = 1; i <= N;i++) fin >> K[i];
    for (int i = 1; i <= N - 1;i++){
        int x,y;
        fin >> x >> y;
        V[x].push_back(y);
        V[y].push_back(x);
    }
    P.push_back(1);
    dfs(1,0,1);
    //cout << endl;
    for (int i = 1; i <= N;i++) fout << DP[i] << " ";
    return 0;
}