Pagini recente » Cod sursa (job #1480079) | Cod sursa (job #182147) | Cod sursa (job #1489963) | Cod sursa (job #1781764) | Cod sursa (job #1661849)
#include <fstream>
#include <cstdlib>
#define NM 100005
using namespace std;
ifstream fin("cerere.in");
ofstream fout("cerere.out");
int n, V[NM], rad, Stiva[NM], fi;
int * A[NM];
bool Valid[NM];
int d[NM];
void DFS(int x)
{
int i, p;
for (i=1; i<=A[x][0]; i++)
{
p = A[x][i];
if (Valid[p]==0)
{
Valid[p]=1;
Stiva[++fi]=p;
if (V[p]==0)
d[p] = 0;
else
d[p] = d[Stiva[fi-V[p]]]+1;
DFS(p);
fi--;
}
}
}
int main()
{
int i, x, y;
fin>>n;
for (i=1; i<=n; i++)
{
fin>>V[i];
}
for (i=1; i<=n; i++)
{
if (V[i]==0)
{
rad=i;
break;
}
}
for (i=1; i<=n; i++)
{
A[i] = (int *) realloc(A[i], sizeof(int));
A[i][0]=0;
}
for (i=1; i<n; i++)
{
fin>>x>>y;
A[x][0]++;
A[x] = (int *) realloc (A[x], (A[x][0]+1)*sizeof(int));
A[x][A[x][0]] = y;
A[y][0]++;
A[y] = (int *) realloc (A[y], (A[y][0]+1)*sizeof(int));
A[y][A[y][0]] = x;
}
Stiva[++fi]=rad;
Valid[rad]=1;
DFS(rad);
for (i=1; i<=n; i++)
fout<<d[i]<<" ";
return 0;
}