Pagini recente » Cod sursa (job #2493526) | Cod sursa (job #2282796) | Cod sursa (job #1812765) | Cod sursa (job #1735474) | Cod sursa (job #2778529)
#include <bits/stdc++.h>
using namespace std;
ofstream fout("cerere.out");
int n, k[100100];
bool root[100100];
int stiva[100100];
vector<int> v[100100];
int dp[100100];
void dfs(int nod, int nivel)
{
stiva[nivel] = nod;
if(k[nod] != 0)
{
dp[nod] = 1 + dp[stiva[nivel - k[nod]]];
}
for(auto it : v[nod])
{
dfs(it, nivel + 1);
}
}
class InParser
{
private:
FILE *fin;
char *buff;
int sp;
char read_ch()
{
++sp;
if (sp == 4096)
{
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume)
{
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n)
{
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-')
{
n = 0;
sgn = -1;
}
else
{
n = c - '0';
}
while (isdigit(c = read_ch()))
{
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n)
{
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-')
{
n = 0;
sgn = -1;
}
else
{
n = c - '0';
}
while (isdigit(c = read_ch()))
{
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
int main()
{
InParser fin("cerere.in");
fin >> n;
for(int i = 1; i <= n; i ++)
{
fin >> k[i];
}
for(int i = 1; i < n; i ++)
{
int x, y;
fin >> x >> y;
v[x].push_back(y);
root[y] = true;
}
int radacina = 0;
for(int i = 1; i <= n; i ++)
{
if(!root[i])
{
radacina = i;
break;
}
}
dfs(radacina, 0);
for(int i = 1; i <= n; i ++)
{
fout << dp[i] << ' ';
}
fout << '\n';
return 0;
}