给一颗销售供应的树。在树根初货物价格为P,从树根每往子节点走一层,价格会上涨R%。给出叶节点的货物量,求出价格之和。
思路:dfs树遍历。
我的树写的过于麻烦了,其实只需要一个vector<int>child够了
参考另一题相似的https://blog.csdn.net/kl782636177/article/details/105167675
#include <iostream>
#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define scl(x) scanf("%lld",&x)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define prs(x) printf("%s\n",(x))
#define prl(x) printf("%lld\n",x)
#define ll long long
#define PII pair<int,int>
#define eps 1e-6
#define inf 1e17
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=1e6+5;
struct node{
vector<int>child;
int level;
int cnt;
}tree[maxn];
int n;
double p,r;
int m,cnt,son;
int flag[maxn];
double ans;
void dfs(int root){
if(tree[root].child.size()==0){
double price=p*pow((1+r*0.01),tree[root].level);
ans+=tree[root].cnt*price;
return;
}
rep(i,0,tree[root].child.size()){
tree[tree[root].child[i]].level=tree[root].level+1;
dfs(tree[root].child[i]);
}
}
int main(){
cin>>n>>p>>r;
rep(t,0,n){
cin>>m;
if(m==0){
cin>>cnt;
tree[t].cnt=cnt;
}
else{
rep(i,0,m){
cin>>son;
tree[t].child.push_back(son);
flag[son]++;
}
}
}
int root=0;
while(flag[root]) root++;
dfs(root);
printf("%.1lf\n",ans);
}