Cod sursa(job #2652352)

Utilizator felix24mihaiPrava Felix Mihai felix24mihai Data 24 septembrie 2020 19:11:36
Problema Cerere Scor 100
Compilator cpp-32 Status done
Runda Arhiva de probleme Marime 1.03 kb
#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;
}