Cod sursa(job #2542096)

Utilizator CiboAndreiAndrei Cibo CiboAndrei Data 9 februarie 2020 14:38:17
Problema Cerere Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <bits/stdc++.h>

using namespace std;

//ifstream f("memory001.in");
//ofstream g("memory001.out");
ifstream f("cerere.in");
ofstream g("cerere.out");

const int dim = 101;

int n;
int k[dim];

vector <int> sons[dim];

int parent[dim];
int dist[dim];

struct str{
    deque <int> lst;
};

str anch[dim];

bool mark[dim];

void dfs(int node){
    mark[node] = 1;

    for(int it: sons[node]){
        if(!mark[it]){
            anch[it].lst = anch[node].lst;
            anch[it].lst.push_front(node);

            if(k[it] > 0){
                dist[it] = dist[anch[it].lst[k[it] - 1]] + 1;
            }

            dfs(it);
        }
    }
}

int main()
{
    int i, j, a, b;

    f >> n;

    for(i = 1; i <= n; ++i){
        f >> k[i];
    }

    for(i = 1; i < n; ++i){
        f >> a >> b;
        sons[a].push_back(b);
        parent[b] = a;
    }

    int root;

    for(i = 1; i <= n; ++i)
        if(parent[i] == 0){
            root = i;
            break;
        }

    anch[root].lst.push_front(0);
    dfs(root);

    for(i = 1; i <= n; ++i)
        g << dist[i] << " ";

    return 0;
}