Cod sursa(job #2778881)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 2 octombrie 2021 12:59:59
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <bits/stdc++.h>
#define DIM 100005

using namespace std;

class InParser
{
    #pragma warning(disable:4996)
    private:
        FILE *f;
        char* buff;
        int sp;
        char read()
        {
            ++sp;
            if(sp == 4096)
            {
                sp = 0;
                fread(buff, 1, 4096, f);
            }
            return buff[sp];
        }
    public:
        InParser(const char* nume)
        {
            sp = 4095;
            buff = new char[4096];
            f = fopen(nume, "r");
        }
        InParser& operator >> (int& n)
        {
            char c;
            while(!isdigit(c = read()));
                n = c - '0';
            while(isdigit(c = read()))
                n = n * 10 + c - '0';
            return *this;
        }
};

InParser f("cerere.in");
ofstream g("cerere.out");

int n, K[DIM], G[DIM], root, node[DIM];
bitset <DIM> v;
vector <int> edges[DIM];

void dfs(int nod, int niv)
{
    node[niv] = nod;
    G[nod] = G[node[niv - K[nod]]] + 1;
    for(auto child : edges[nod])
        dfs(child, niv + 1);
}

int main()
{
    f >> n;
    for(int i = 1; i <= n; i++)
        f >> K[i];

    for(int i = 1; i < n; i++)
    {
        int x, y;
        f >> x >> y;
        edges[x].push_back(y);
        v[y] = 1;
    }

    for(int i = 1; i <= n; i++)
        if(!v[i])
        {
            root = i;
            break;
        }

    dfs(root, 0);

    for(int i = 1; i <= n; i++)
        g << G[i] - 1 << " ";

    return 0;
}