🔥یلدا با راکت با (( ۷۰ درصد )) تخفیف! یلدا طولانی است، اما این تخفیف نه.
مشاهده دورههاسلام روز همگی بخیر
این سوالیه که توی کوئرا من توی حلش به مشکل خوردم:


که کدی که براش نوشتم این طوریه:
#include
#include <vector>
using namespace std;
struct Node {
int id;
vector<int> children;
};
int find_boss(vector<Node>& nodes, int person) {
for (Node& node : nodes) {
if (node.id == person) {
return person;
}
for (int child : node.children) {
if (find_boss(nodes, child) == person) {
return person;
}
}
}
return -1;
}
int find_boss_fine(vector<Node>& nodes, int n) {
int boss = find_boss(nodes, 1);
int fines = 0;
for (Node& node : nodes) {
if (node.id != boss) {
fines++;
}
}
return fines + 1;
}
int main() {
int n;
cin >> n;
vector<Node> nodes(n);
for (int i = 0; i < n - 1; i++) {
int parent;
cin >> parent;
nodes[parent - 1].children.push_back(i + 2);
}
cout << find_boss_fine(nodes, n) << endl;
}
اما مشکل داره و جواب نمیده، کسی نظری داره؟ ممنونم از راهنماییتون
به مبینا کمک کنید تا مشکل خودش را حل کند؛ اینطور میتوانیم با هم پیشرفت کنیم.
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟