Cod sursa(job #2313712)

Utilizator ezioconnorVlad - Gabriel Iftimescu ezioconnor Data 7 ianuarie 2019 13:08:20
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream in("cerere.in");
ofstream out("cerere.out");

const int N = 100001;
vector<int> a[N];
int cost[N], d[N], v[N];
bool viz[N];
bool c[N];
int n, m;

void afisare()
{
    for (int i = 1;i <= n; ++i)
        out << cost[i] << " ";
}

void dfs(int x, int niv)
{
    d[niv] = x;
    cost[x] = 1 + cost[d[niv - v[x]]];
    viz[x] = true;
    for (int i = 0; i < a[x].size(); ++i)
    {
        int y = a[x][i];
        if (!viz[y])
            dfs(y, niv + 1);
    }
}

int main()
{
    int i, x, y, poz;
    in >> n;
    for (i = 1; i <= n; ++i)
        in >> v[i];
    for (i = 1; i <= n - 1; ++i)
    {
        in >> x >> y;
        c[y] = true;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    for (i = 1; i <= n; ++i)
    {
        if (c[i] == false)
            poz = i;
    }
    dfs(poz, 1);
    for (i = 1; i <= n; ++i)
        --cost[i];
    afisare();
    return 0;
}