Cod sursa(job #2794985)

Utilizator CaptnBananaPetcu Tudor CaptnBanana Data 5 noiembrie 2021 19:37:16
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("cerere.in");
ofstream g("cerere.out");

const int N = 1e5 + 1;
int n, k[N], x, y, ans[N], stramos[N];
int vf[N], urm[N], lst[N], nr;
long long s;

void adauga(int x, int y){
    nr++;
    vf[nr] = y;
    urm[nr] = lst[x];
    lst[x] = nr;
}

void dfs(int nod, int distDeLaRadacina){
    stramos[distDeLaRadacina] = nod;
    ans[nod] = ans[stramos[distDeLaRadacina - k[nod]]] + 1;
    for(int i = lst[nod], y = vf[i]; i != 0; i = urm[i], y = vf[i])
        dfs(y, distDeLaRadacina + 1);
}

int main(){
    f >> n;
    for(int i = 1; i <= n; i++){
        ans[i] = -1;
        f >> k[i];
    }

    s = 1LL * n * (n + 1) / 2;
    for(int i = 1; i < n; i++){
        f >> x >> y;
        adauga(x, y);
        s -= y;
    }

    f.close();
    dfs(s, 0);
    for(int i = 1; i <= n; i++)
        g << ans[i] << ' ';

    g.close();
}