Cod sursa(job #2949994)

Utilizator CiuiGinjoveanu Dragos Ciui Data 2 decembrie 2022 15:48:13
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <deque>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <limits.h>
using namespace std;

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

const int MAX_SIZE = 100000;

vector<int> tree[MAX_SIZE + 1];
int ancestorDegree[MAX_SIZE + 1], noHands[MAX_SIZE + 1], level[MAX_SIZE + 1];
bitset<MAX_SIZE + 1> hasParent;

void findNoHands(int act, int actLevel) {
    level[actLevel] = act;
    int actAncestorDegree = ancestorDegree[act];
    if (actAncestorDegree != 0) {
        noHands[act] = noHands[level[actLevel - actAncestorDegree]] + 1;
    }
    for (int next : tree[act]) {
        findNoHands(next, actLevel + 1);
    }
}

int main() {
    int noMonkeys;
    fin >> noMonkeys;
    for (int i = 1; i <= noMonkeys; ++i) {
        fin >> ancestorDegree[i];
    }
    for (int i = 1; i < noMonkeys; ++i) {
        int parent, child;
        fin >> parent >> child;
        tree[parent].push_back(child);
        hasParent[child] = 1;
    }
    int root = -1;
    for (int i = 1; i <= noMonkeys; ++i) {
        if (!hasParent[i]) {
            root = i;
            break;
        }
    }
    findNoHands(root, 1);
    for (int i = 1; i <= noMonkeys; ++i) {
        fout << noHands[i] << " ";
    }
}