Mai intai trebuie sa te autentifici.
Cod sursa(job #812087)
Utilizator | Data | 13 noiembrie 2012 13:52:45 | |
---|---|---|---|
Problema | Cerere | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 0.97 kb |
#include<fstream>
using namespace std;
ifstream f("cerere.in");
ofstream g("cerere.out");
int n,v[100005],r[100005],boss;
struct nod
{
int inf;
nod *adr;
} *x[100005];
struct stiva
{
int m;
int nrs;
} stv[100005];
void citire()
{
int a,b,t[100005];
f>>n;
for(int i=1;i<=n;i++)
f>>v[i];
for(int i=1;i<n;i++)
{
f>>a>>b;
t[b]=a;
nod *p=new nod;
p->inf=b;
p->adr=x[a];
x[a]=p;
}
for(int i=1;i<=n;i++)
if(t[i]==0)
{
boss=i; break;
}
}
void rezolvare(int nd,int poz)
{
stv[poz].m=nd;
if(v[nd]==0)
stv[poz].nrs=0;
else
stv[poz].nrs=stv[poz-v[nd]].nrs+1;
r[nd]=stv[poz].nrs;
for(nod *p=x[nd];p!=NULL;p=p->adr)
{
rezolvare(p->inf,poz+1);
}
}
void afisare()
{
for(int i=1;i<=n;i++)
g<<r[i]<<' ';
}
int main()
{
citire();
rezolvare(boss,1);
afisare();
return 0;
}