Pagini recente » Cod sursa (job #660835) | Cod sursa (job #1795962) | Cod sursa (job #2209494) | Cod sursa (job #710516) | Cod sursa (job #1470099)
#include <cstdio>
#include <vector>
using namespace std;
#define N 100001
vector<int> a[N];
int parent[N];
int st[N];
int n;
int g[N];
int root = 0;
bool used[N];
int nst;
int sol[N];
void dfs(int u) {
used[u] = true;
st[++nst] = u;
if (nst - parent[u] >= 1 && parent[u] != 0)
sol[u] = sol[ st[ nst - parent[u] ] ] + 1;
for (vector<int>::iterator it = a[u].begin(); it != a[u].end(); ++it)
if (!used[*it]) {
dfs(*it);
}
--nst;
}
int main() {
freopen ("cerere.in", "r", stdin);
freopen ("cerere.out", "w", stdout);
scanf ("%d\n", &n);
int i;
for (i = 1; i <= n; ++i)
scanf ("%d ", &parent[i]);
int p, q;
for (i = 1; i < n; ++i) {
scanf ("%d %d\n", &p, &q);
a[p].push_back(q);
//a[q].push_back(p);
g[q]++;
}
for (i =1 ;i <= n; ++i)
if (!g[i])
root = i;
dfs(root);
for (i = 1; i <= n; ++i)
printf ("%d ", sol[i]);
printf ("\n");
}