Cod sursa(job #1991882)

Utilizator DjokValeriu Motroi Djok Data 18 iunie 2017 16:22:31
Problema Cerere Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <bits/stdc++.h>
using namespace std;

const int N = 100005;

int i, j, n, x, y, k[N], tata[N], rs[N];
vector<int> lda[N], v;

void dfs(int x) {
  if(k[x]) rs[x] = rs[v[v.size() - k[x]]] + 1;
  v.push_back(x);

  for(auto to : lda[x]) dfs(to);

  v.pop_back();
}

int main() {
  ifstream cin("cerere.in");
  ofstream cout("cerere.out");
  ios_base::sync_with_stdio(0);

  cin >> n;
  for(i = 1; i <= n; ++i) cin >> k[i];
  for(i = 1; i < n; ++i) {
    cin >> x >> y;
    lda[x].push_back(y);
    tata[y] = x;
  }

  for(i = 1; i <= n; ++i)
    if(!tata[i]) dfs(i);

  for(i = 1; i <= n; ++i) cout << rs[i] << " \n"[i == n];

  return 0;
}