Pagini recente » Cod sursa (job #948472) | Cod sursa (job #609857) | Cod sursa (job #908371) | Cod sursa (job #1734916) | Cod sursa (job #3128037)
#include<bits/stdc++.h>
using namespace std;
ifstream f("cerere.in");
ofstream g("cerere.out");
const int NMAX=100005;
int n,x,y,ancestor[NMAX],ans[NMAX];
bitset<NMAX>not_root,used;
vector<int>walk,tree[NMAX];
void dfs(int node,int height)
{
used[node]=1;
walk.push_back(node);
ans[node]=1+ans[walk[height-ancestor[node]]];
for(int next:tree[node])
if(!used[next])
dfs(next,height+1);
walk.resize(walk.size()-1);
}
int main()
{
ios_base::sync_with_stdio(false);
f>>n;
for(int i=1; i<=n; i++)
f>>ancestor[i], ans[i]=-1;
for(int i=1; i<n; i++)
{
f>>x>>y;
not_root[y]=1;
tree[x].push_back(y);
tree[y].push_back(x);
}
for(int i=1; i<=n; i++)
if(!not_root[i])
{
dfs(i,0);
break;
}
for(int i=1; i<=n; i++)
g<<ans[i]<<' ';
return 0;
}