Cod sursa(job #2763088)

Utilizator EusebiudistrugatorulLionel Messi Eusebiudistrugatorul Data 11 iulie 2021 15:50:29
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <bits/stdc++.h>
#define Intro ios::sync_with_stdio(0), cin.tie(0)
#define ll long long
using namespace std;
ifstream fin("cerere.in");
ofstream fout("cerere.out");
int const N = 1e5 + 5;
int nr_nodes;
int next_to_solve[N], degree[N];
vector<int> nodes_passed(N, -1);
vector<int> neighbours[N];
vector<int> nodes_stored;

void find_passes(int node) {
    for (int neighbour : neighbours[node]) {
        int length = nodes_stored.size();
        nodes_stored.push_back(neighbour);
        nodes_passed[neighbour] = nodes_passed[nodes_stored[length - next_to_solve[neighbour]]] + 1;
        find_passes(neighbour);
        nodes_stored.pop_back();
    }
}

int main() {
    fin >> nr_nodes;
    for (int i = 1; i <= nr_nodes; ++i) {
        fin >> next_to_solve[i];
    }
    for (int i = 1; i < nr_nodes; ++i) {
        int father, son;
        fin >> father >> son;
        neighbours[father].push_back(son);
        degree[son]++;
    }
    int root = 1;
    for (int i = 1; i <= nr_nodes; ++i) {
        if (degree[i] == 0) {
            root = i;
            break;
        }
    }
    nodes_stored.push_back(root);
    nodes_passed[root] = 0;
    find_passes(root);
    for (int i = 1; i <= nr_nodes; ++i) {
        fout << nodes_passed[i] << ' ';
    }
    //read the stuff below
    return 0;
}
/* Plan
- read the statement carefully
- write stuff down: observations, ideas
- consider the methods
- create the steps on paper
- overflow (values, arrays)
- testing (edge cases)
- remake the implementation more clearly
- final revision
*/