结构体+树的BFS就过了
#include#include #include using namespace std;int n;double p, r;double totalSale;typedef struct node{ int id; int cn;//child数量 vector child; int amount; double price;};node nodes[100000+5];queue q;void bfs(){ while(!q.empty()){ node cur = q.front(); q.pop(); if(cur.cn == 0){//叶子 totalSale += cur.amount * cur.price; continue; } double nextPrice = cur.price * (1 + r/100); for(int i = 0; i < cur.cn; i++){ int childId = cur.child[i]; nodes[childId].price = nextPrice; q.push(nodes[childId]);//入队 } } }int main(){ freopen("in.txt","r",stdin); scanf("%d %lf %lf",&n, &p, &r); for(int i = 0; i < n; i++){ node tmp; tmp.id = i; scanf("%d", &tmp.cn); if(tmp.cn > 0){ int child; for(int j = 0; j < tmp.cn; j++){ scanf("%d", &child); tmp.child.push_back(child); } tmp.amount = 0; }else{ scanf("%d", &tmp.amount); } tmp.price = 0.0; nodes[i] = tmp; } totalSale = 0.0; nodes[0].price = p; q.push(nodes[0]); bfs(); printf("%.1lf", totalSale); return 0;}