PAT A1090 Highest Price in Supply Chain (25分)(边界问题)

#include<cstdio>
#include<vector>
#include<math.h>
#include<queue>
using namespace std;
const int N = 100010;
struct node{
    bool isretailer=true;
    vector<int> child;
    int deep;
}Node[N];
int n;
double p,r;

void updatedeep(int root){
    queue<int> q;
    if(root==-1) return;
    q.push(root);
    Node[root].deep = 0;
    while(q.empty()==false){
        int front = q.front();
        q.pop();
        for(int i  = 0;i<Node[front].child.size();i++){
            int childid = Node[front].child[i];
            Node[childid].deep = Node[front].deep+1;
            q.push(childid);
        }
    }
    return;
}
int main(){
    scanf("%d %lf %lf",&n,&p,&r);
    int root=-1;
// 9 1.80 1.00
// 1 5 4 4 -1 4 5 3 6
    for(int i = 0;i<n;i++){
        
        int father;
        scanf("%d",&father);
        if(father==-1){
            root = i;
            continue;
        }else{
            Node[father].child.push_back(i);
            Node[father].isretailer  = false;
        }
    }
    updatedeep(root);
    int maxdeep = 0;
    int count = 0;
    for(int i = 0;i<n;i++){
        if(Node[i].isretailer==true){
            if(Node[i].deep>maxdeep){
                maxdeep = Node[i].deep;
                count = 1;
            }else if(Node[i].deep == maxdeep){
                count++;
            }
        }
    }
    double rate = 1 + r/100;
    double max = p*pow(rate,maxdeep);
    printf("%.2f %d",max,count);
    return 0;
}
posted @ 2020-09-08 18:29  是水泵呢  阅读(83)  评论(0编辑  收藏  举报