题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805362341822464
还是老套路,用了DFS把depth参数初始化了,就是calculate函数有几个参数写错了,搞得调试了几遍都没过。。也是服了自己了

代码

  1. #include<algorithm>
  2. #include<vector>
  3. #include<iostream>
  4. #include<cmath>
  5. using namespace std;
  6. const int maxn = 100010;
  7. struct Node{
  8. int depth;
  9. vector<int> child;
  10. }node[maxn];
  11. int hash_table[maxn] = {0};
  12. int n;
  13. double p, r;
  14. void DFS(int root, int depth){
  15. if(node[root].child.size() == 0) hash_table[depth]++;
  16. for(int i = 0; i < node[root].child.size(); i++){
  17. int child = node[root].child[i];
  18. DFS(child, depth + 1);
  19. }
  20. }
  21. void caculate(){
  22. int min_index = 0;
  23. for(int i = 0; i < n; i++){
  24. if(hash_table[i] != 0){
  25. min_index = i;
  26. break;
  27. }
  28. }
  29. printf("%.4f %d", p * pow(1 + r, min_index), hash_table[min_index]);
  30. }
  31. int main(){
  32. int child_num, child_temp;
  33. scanf("%d%lf%lf", &n, &p, &r);
  34. r /= 100;
  35. for(int i = 0; i < n; i++){
  36. scanf("%d", &child_num);
  37. for(int j = 0; j < child_num; j++){
  38. scanf("%d", &child_temp);
  39. node[i].child.push_back(child_temp);
  40. }
  41. }
  42. DFS(0, 0);
  43. caculate();
  44. return 0;
  45. }