码迷,mamicode.com
首页 > 其他好文 > 详细

ZJU PAT 1090

时间:2014-12-01 12:40:15      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   os   sp   for   div   

依题意构造树并遍历,找出最大深度并统计

#include <iostream>
#include <vector>

using namespace std;

struct Node {
    vector<int> children;
    int depth;
};

Node nodes[100005];

int maxDepth = 0;
int total = 1;

void travel(int n, int d) {
    Node& node = nodes[n];
    node.depth = d;

    int size = node.children.size();
    if (size > 0) {
        ++d;
        if (d > maxDepth) {
            maxDepth = d;
            total = size;
        }
        else if (d == maxDepth) {
            total += size;
        }

        for (int i = 0; i != size; ++i) {
            travel(node.children[i], d);
        }
    }
}

int main() {
    int n;
    double price, rate;
    scanf("%d %lf %lf", &n, &price, &rate);
    rate = rate / 100.0 + 1.0;

    int root = -1;
    for (int i = 0; i != n; ++i) {
        int m;
        scanf("%d", &m);
        if (m == -1) {
            root = i;
        }
        else {
            nodes[m].children.push_back(i);
        }

        nodes[i].depth = 0;
    }

    travel(root, 0);

    price *= pow(rate, maxDepth);

    printf("%.2lf %d\n", price, total);

    return 0;
}

 

ZJU PAT 1090

标签:des   style   blog   io   color   os   sp   for   div   

原文地址:http://www.cnblogs.com/stpdmeow/p/4134636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!