Cod sursa(job #2593453)
| Utilizator | Data | 3 aprilie 2020 22:04:25 | |
|---|---|---|---|
| Problema | Cerere | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva de probleme | Marime | 0.83 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream f("cerere.in");
ofstream g("cerere.out");
int v[100001], t[100001], s[100001];
int main(){
int n, i, x, y, k, aux;
f>>n;
for(i=1; i<=n; i++)
f>>v[i];
for(i=1; i<n; i++)
{
f>>x>>y;
t[y]=x;
}
for(i=1; i<=n; i++)
if(v[i])
{
aux=v[i];
k=i;
while(aux)
{
k=t[k];
aux--;
}
s[i]=k;
}
for(i=1; i<=n; i++)
if(s[i]==0)
g<<0<<" ";
else
{
aux=s[i];
k=1;
while(v[aux])
{
k++;
aux=s[v[aux]];
}
g<<k<<" ";
}
}
//Oana
