Cod sursa(job #1452131)

Utilizator stropitoare98stropitoare stropitoare98 Data 19 iunie 2015 22:56:23
Problema Cerere Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <fstream>
#include <vector>
#define dim 100001
using namespace std;
ifstream fin("cerere.in");
ofstream fout("cerere.out");
bool viz[dim];
int lvl[dim],sol[dim],K[dim],n,i,a,b;
vector <int> G[dim];
void dfs(int node,int level)
{
    viz[node]=1;
    if(K[node]==0)
        lvl[level]=0;
    else
        lvl[level]=lvl[level-K[node]]+1;
    sol[node]=lvl[level];
    for(int j=0;j<G[node].size();j++)
    {
        int nxt=G[node][j];
        if(!viz[nxt])
            dfs(nxt,level+1);
    }
}
int main()
{
    fin>>n;
    for(i=1;i<=n;i++)
        fin>>K[i];
    for(i=1;i<n;i++)
    {
        fin>>a>>b;
        G[a].push_back(b);
    }
    dfs(1,1);
    for(i=1;i<=n;i++)
        fout<<sol[i]<<" ";
    return 0;
}