Cod sursa(job #2146210)

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

using namespace std;

ifstream fin("cerere.in");
ofstream fout("cerere.out");
int N,DP[100010],K[100010],viz[100010],sr;
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);
        viz[y] = x;
        //V[y].push_back(x);
    }
    for (int i = 1; i <= N;i++) if (viz[i] == 0) sr = i;
    //cout << sr<<endl;
    P.push_back(sr);
    dfs(sr,0,1);
    //cout << endl;
    for (int i = 1; i <= N;i++) fout << DP[i] << " ";
    return 0;
}