题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805362341822464
还是老套路,用了DFS把depth参数初始化了,就是calculate函数有几个参数写错了,搞得调试了几遍都没过。。也是服了自己了
代码
#include<algorithm>#include<vector>#include<iostream>#include<cmath>using namespace std;const int maxn = 100010;struct Node{int depth;vector<int> child;}node[maxn];int hash_table[maxn] = {0};int n;double p, r;void DFS(int root, int depth){if(node[root].child.size() == 0) hash_table[depth]++;for(int i = 0; i < node[root].child.size(); i++){int child = node[root].child[i];DFS(child, depth + 1);}}void caculate(){int min_index = 0;for(int i = 0; i < n; i++){if(hash_table[i] != 0){min_index = i;break;}}printf("%.4f %d", p * pow(1 + r, min_index), hash_table[min_index]);}int main(){int child_num, child_temp;scanf("%d%lf%lf", &n, &p, &r);r /= 100;for(int i = 0; i < n; i++){scanf("%d", &child_num);for(int j = 0; j < child_num; j++){scanf("%d", &child_temp);node[i].child.push_back(child_temp);}}DFS(0, 0);caculate();return 0;}
