PAT A1090 Highest Price in Supply Chain

题目

PAT A1090 Highest Price in Supply Chain_第1张图片

code

#include 
#include 
#include 
#include 
using namespace std;
const int maxn=100100;//开大一点!!!!6位起步
vector<int> child[maxn];
double p,r;
int n,maxDepth=0,num=0;
//DFS 
void DFS(int index,int depth){
    if(child[index].size()==0){// leaf 临界条件
        if(depth>maxDepth){
            maxDepth=depth;
            num=1;
        }else if(depth==maxDepth){
            num++;
        }
        return;
    }
    for(int i= 0; i<child[index].size();i++){
        DFS(child[index][i], depth+1);
    }
}

int main(){
    int father,root;
    scanf("%d%lf%lf",&n,&p,&r);
    r/=100;
    for(int i=0;i<n;i++){
        scanf("%d",&father);
        if(father!=-1){
            child[father].push_back(i);
        }else if (father==-1){
            root=i;
        }
       
    }
    DFS(root,0);
    printf("%.2f %d\n",p* pow(1+r, maxDepth),num);

    return 0;
}

你可能感兴趣的:(PAT)