Cod sursa(job #1326749)

Utilizator MacWonkMihai Alexandru Cosmin MacWonk Data 25 ianuarie 2015 22:05:13
Problema Cerere Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream f("cerere.in");
ofstream g("cerere.out");
int n,i,x,y,rad,pos,fiu;
int rez[100005];
int st[100005];
bool gr[100005];
int k[100005];
vector <int> A[100005];
int main()
{
    f>>n;
    for(i=1;i<=n;++i) f>>k[i];
    for(i=1;i<n;++i)
    {
        f>>x>>y;
        A[x].push_back(y);
        ++gr[y];
    }
    rad=0;
    for(i=1;i<=n && rad==0;++i) if(gr[i]==0) rad=i;
    rez[0]=-1;
    pos=1; st[pos]=rad;
    while(pos>0)
    {
        x=st[pos];
        if(A[x].size()>0) //un fel de coada, dar am folosit vectori
        {
            ++pos;
            fiu=A[x][0];
            st[pos]=fiu;
            if(k[fiu]==0) rez[fiu]=0;
            else rez[fiu]=rez[st[pos-k[fiu]]]+1;
            A[x].erase(A[x].begin());
        }
        else --pos;
    }
    for(i=1;i<=n;++i) g<<rez[i]<<" ";
    return 0;
}