Cod sursa(job #2543030)

Utilizator CosminMorarMorar Cosmin Andrei CosminMorar Data 10 februarie 2020 19:46:02
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
ifstream fin("cerere.in");
ofstream fout("cerere.out");
int n, rad;
vector<int> nod[100001];
int k[100001], sol[100001], chain[100001];
queue<int> c;
bitset<100001> f, areTata;

void readAndSet() {
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> k[i];
    for (int i = 1; i <= n; i++) {
        int a, b;
        fin >> a >> b;
        nod[a].push_back(b);
        areTata[b] = true;
    }
}

int findRoot() {
    for (int i = 1; i <= n; i++)
        if (!areTata[i])
            return i;
}

void dfs(int i, int depth) {
    chain[depth] = i;
    if (k[i])
        sol[i] = sol[chain[depth - k[i]]] + 1;

    for (int j : nod[i])
        if (!f[j]) {
            f[j] = true;
            dfs(j, depth + 1);
        }
}

void print() {
    for (int i = 1; i <= n; i++)
        fout << sol[i] << ' ';
}

int main() {
    readAndSet();
    dfs(findRoot(), 1);
    print();
    return 0;
}