Pagini recente » Cod sursa (job #1809173) | Cod sursa (job #3126723) | Cod sursa (job #84296) | Cod sursa (job #2326687) | Cod sursa (job #2652352)
#include <iostream>
#include <fstream>
#include <vector>
#define NMAX 100003
using namespace std;
ifstream f("cerere.in");
ofstream g("cerere.out");
int n, ancestorNrK[NMAX], answer[NMAX], head;
vector <int> nodes[NMAX];
int currentAnswer[NMAX];
void read(){
int a,b;
f >> n;
long long s = n * (n+1) / 2;
for (int i = 0; i < n; i++){
f >> ancestorNrK[i+1];
}
for (int i = 1; i < n; i++){
f >> a >> b;
s -= b;
nodes[a].push_back(b);
}
head = s;
}
void DFS(int node, int currentLvl){
int lvl = ancestorNrK[node];
if (lvl == 0){
answer[node] = 0;
currentAnswer[currentLvl] = 0;
}
else{
answer[node] = 1 + currentAnswer[currentLvl - lvl];
currentAnswer[currentLvl] = answer[node];
}
for (int i = 0; i < nodes[node].size(); i++)
DFS(nodes[node][i], currentLvl + 1);
}
int main()
{
read();
DFS(head,1);
for (int i = 1; i <= n; i++)
g << answer[i] << " ";
return 0;
}