PAT 1090 Highest Price in Supply Chain (25 分)

#include
#include
#include 
using namespace std;

int n, maxdepth=0, maxnum=0, temp, root;
vector <int> Next[100010];
void dfs(int index,int depth){
    if(Next[index].size()==0){      //访问到叶结点
        if(depth==maxdepth) maxnum++;
        if(depth>maxdepth){
            maxdepth=depth;
            maxnum=1;
        }
        return;
    }
    for(int i=0;i<Next[index].size();i++){
        dfs(Next[index][i],depth+1);
    }
}
int main(){
    double p,r;
    cin>>n>>p>>r;
    for(int i=0;i<n;i++){
        scanf("%d",&temp);
        if(temp==-1) {root=i;}
        else{Next[temp].push_back(i);}
    }
    dfs(root,0);
    printf("%.2f %d", p * pow(1 + r/100, maxdepth), maxnum);
    system("pause");
    return 0;
}

你可能感兴趣的:(pta,深度优先,c语言,算法)