【1090 25 树的深度】 Highest Price in Supply Chain

传送门

题意

给定\(n\)个点标号为\(0\sim n-1\),初始值\(p\),增长率\(r\),在树中每经过一层增长的形式为\(p=p\times \% r\),给定\(n\)个点,\(a_{i}\)表示的是第\(i\)个点的父节点,求出最大的值和达到最大值的点的数量

数据范围

\(n\leq 10^{5}\)

题解

  • 一定是叶节点,dfs即可

Code

#include<bits/stdc++.h>
using namespace std;
#define db double
const int N=1e5+10;

struct node{
    int to,ne;
}e[N*2];
int h[N],id,cnt;
int mx;
bool st[N];
void add(int u,int v){
    e[++id].to = v;
    e[id].ne=h[u];
    h[u]=id;
}
void dfs(int u,int deep){
    if(deep>mx){
        cnt=1; 
        mx=deep;
    } 
    else if(deep == mx) ++cnt;

    for(int i=h[u];i;i=e[i].ne){
        int to=e[i].to;
        dfs(to,deep+1);
    }
    return;
}

int main(){
    int n; db p,r; cin>>n>>p>>r;
    r=r/100+1;
    int root;
    for(int i=0;i<n;i++){
        int x; cin>>x;
        if(x == -1) {root=i; continue;}
        add(x,i);
    }
    dfs(root,0);
    printf("%.2lf %d",p*pow(r,mx),cnt);
}
posted @ 2021-03-09 22:21  Hyx'  阅读(59)  评论(0)    收藏  举报