問題鏈接:https://pintia.cn/problem-sets/994805342720868352/problems/994805388447170560
與A1090做比較,本題出錯點在一開始錯誤將P乘到了每個葉結點上,導致乘了多個P。
實際計算出葉結點貨物價格之和後,只需乘一個p根結點處貨物價格。
#include<cstdio>
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int n;
double p, r;
double ans = 0;
struct node{
int data;
vector<int> child;
}Node[100010];
void DFS(int index, int depth){
int v = Node[index].data;
if(v != 0){//到達葉子結點
ans += v * pow(1+r, depth);
return;
}
for(int i = 0; i < Node[index].child.size(); i++)
DFS(Node[index].child[i], depth + 1);
}
int main(){
int k, x;
cin >> n >> p >> r;
r /= 100;
for(int i = 0; i < n; i++)
{
cin >> k;
if(k == 0){
cin >> x;
Node[i].data = x;//是葉子結點,輸入貨物量x
}
else{
Node[i].data = 0;//非葉子結點,輸入孩子結點
for(int j = 0; j < k; j++){
cin >> x;
Node[i].child.push_back(x);
}
}
}
DFS(0, 0);
printf("%.1f",p * ans);
return 0;
}