Pagini recente » Cod sursa (job #1911475) | Cod sursa (job #2408354) | Cod sursa (job #339365) | Cod sursa (job #1141310) | Cod sursa (job #2949994)
#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] << " ";
}
}