Код: Выделить всё
#include
#include
using namespace std;
vector otm;
vector past;
vector strength;
bool leaves(int n){
for (int i = 0; i < n; i++){
if (otm[i] == 1 && past[i] == 0 && strength[i] < strength[0]){
return true;
}
}
return false;
}
int main()
{
int n;
cin >> n;
otm.resize(n, 1);
for (int i = 0; i < n; i++){
int a;
cin >> a;
strength.push_back(a);
}
int prep[n];
past.resize(n, 0);
for (int i = 0; i < n - 1; i ++){
int a;
int b;
cin >> a >> b;
prep[b - 1] = a - 1;
past[a - 1]++;
}
while (leaves(n)){
for (int i = 0; i < n; i++){
if (otm[i] == 1 && past[i] == 0 && strength[i] < strength[0]){ //this is 48 line
otm[i] = 0;
past[prep[i]]--;
}
}
}
int sum = 0;
for (int i = 0; i < n; i++){
if (otm[i] == 1){
sum++;
}
}
cout strength[0]){Подробнее здесь: https://stackoverflow.com/questions/786 ... inite-loop