Cod sursa(job #2568197)

Utilizator petrisorvmyVamanu Petru Gabriel petrisorvmy Data 3 martie 2020 21:19:55
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <bits/stdc++.h>
#define FILE_NAME "cerere"
#define fast ios_base :: sync_with_stdio(0); cin.tie(0);
#pragma GCC optimize("O3")
#define NMAX 101000
using namespace std;

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

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pi;
typedef pair<ld,ld> pct;

const ll inf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;


void add(ll &a , ll b)
{
    a += b;
    a %= mod;
}

void sub(ll &a, ll b)
{
    a = (a - b + mod) % mod;
}

int x, y, n, k[NMAX], ans[NMAX], root, stiva[NMAX], nstiva;
vector <int> G[NMAX];
bitset <NMAX> notRoot;

void DFS(int nod)
{
    if(k[nod])
        ans[nod] = ans[ stiva[ nstiva - k[nod] ] ] + 1;
    for(auto it : G[nod])
    {
        stiva[ ++nstiva ] = it;
        DFS(it);
        nstiva--;
    }
}
int main()
{
    f >> n;
    for(int i = 1; i <= n; ++i)
        f >> k[i];

    for(int i = 1; i < n; ++i)
    {
        f >> x >> y;
        G[x].push_back(y);
        notRoot[y] = 1;
    }
    for(int i = 1; i <= n; ++i)
        if(!notRoot[i])
        {
            root = i;
            break;
        }
    stiva[ ++nstiva ] = root;
    DFS(root);
    for(int i = 1; i <= n; ++i)
        g << ans[i] << ' ';
    f.close();
    g.close();
    return 0;
}