Pagini recente » Cod sursa (job #2871043) | Cod sursa (job #1212415) | Cod sursa (job #757392) | Cod sursa (job #1153065) | Cod sursa (job #2568197)
#include <bits/stdc++.h>
#define FILE_NAME "cerere"
#define fast ios_base :: sync_with_stdio(0); cin.tie(0);
#pragma GCC optimize("O3")
#define NMAX 101000
using namespace std;
ifstream f(FILE_NAME ".in");
ofstream g(FILE_NAME ".out");
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pi;
typedef pair<ld,ld> pct;
const ll inf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;
void add(ll &a , ll b)
{
a += b;
a %= mod;
}
void sub(ll &a, ll b)
{
a = (a - b + mod) % mod;
}
int x, y, n, k[NMAX], ans[NMAX], root, stiva[NMAX], nstiva;
vector <int> G[NMAX];
bitset <NMAX> notRoot;
void DFS(int nod)
{
if(k[nod])
ans[nod] = ans[ stiva[ nstiva - k[nod] ] ] + 1;
for(auto it : G[nod])
{
stiva[ ++nstiva ] = it;
DFS(it);
nstiva--;
}
}
int main()
{
f >> n;
for(int i = 1; i <= n; ++i)
f >> k[i];
for(int i = 1; i < n; ++i)
{
f >> x >> y;
G[x].push_back(y);
notRoot[y] = 1;
}
for(int i = 1; i <= n; ++i)
if(!notRoot[i])
{
root = i;
break;
}
stiva[ ++nstiva ] = root;
DFS(root);
for(int i = 1; i <= n; ++i)
g << ans[i] << ' ';
f.close();
g.close();
return 0;
}