Pagini recente » Cod sursa (job #36490) | Cod sursa (job #2835719) | Cod sursa (job #2349045) | Cod sursa (job #521799) | Cod sursa (job #2485036)
#include <fstream>
#include <cstdio>
#include <vector>
#include <cctype>
int sol[100001], destepti[100001];
std::vector<int> edges[100001];
bool viz[100001], are_parinte[100001];
int st[100000], top;
// fmm limite idioate
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;
}
};
void dfs(int i) {
viz[i] = true;
st[top++] = i;
if (destepti[i] > 0) {
sol[i] = sol[st[top - destepti[i] - 1]] + 1;
}
for (const auto fiu : edges[i]) {
dfs(fiu);
}
top--;
}
int main() {
InParser fin("cerere.in");
std::ofstream fout("cerere.out");
int n;
fin >> n;
for (int i = 1; i <= n; i++) {
fin >> destepti[i];
}
for (int i = 0; i < n - 1; i++) {
int a, b;
fin >> a >> b;
edges[a].push_back(b);
are_parinte[b] = true;
}
int tati = 1;
while (tati <= n && are_parinte[tati] == true) {
tati++;
}
dfs(tati);
for (int i = 1; i <= n; i++) {
fout << sol[i] << ' ';
}
return 0;
}