Cod sursa(job #2570629)

Utilizator ciutanpCiuta Andrei Calin ciutanp Data 4 martie 2020 18:10:26
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
//Pentru Vamy

#include<bits/stdc++.h>
using namespace std;

const int NAX = 1e5 + 5;

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

int n, k[ NAX ], start, c[ NAX ], sol[ NAX ], cate;

vector<int>G[NAX];
bitset<NAX>viz;

void DFS(int node )
{
    if(k[ node ])
    {
        sol[ node ] = sol[ c[ cate - k[ node ] + 1] ] + 1;
    }
    c[ ++cate ] = node;
    for(auto it: G[ node ])
    {
        DFS(it);
    }
    --cate;
}

int main()
{
    f >> n;
    for(int i = 1 ; i <= n ; ++i)
    {
        f >> k[ i ];
    }
    for(int x, y, i = 1 ; i < n ; ++i)
    {
        f >> x >> y;
        G[ x ].push_back(y);
        viz[ y ] = 1;
    }
    for(int i = 1 ; i <= n ; ++i)
    {
        if(!viz[ i ])
            {
                start = i; break;
            }
    }
    DFS(start);

    for(int i = 1 ; i <= n ; ++i)
        g << sol[ i ] << ' ';

}