1090 Highest Price in Supply Chain

重点在于理解这一句:
each number Si is the index of the supplier for the i-th member.
Si是第i个节点的父节点编号。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <bits/stdc++.h>
using namespace std;

int n, maxdepth = 0, maxnum = 0, root, temp;
double p, r;
vector<int> v[100010];

void DFS(int root, int depth) {
if (v[root].size() == 0) {
if (maxdepth == depth) {
maxnum++;
}
if (maxdepth < depth) {
maxdepth = depth;
maxnum = 1;
}
return; // 最好加上
}
for (int i = 0; i < v[root].size(); i++) {
DFS(v[root][i], depth + 1);
}
}

int main() {
scanf("%d %lf %lf", &n, &p, &r);
for (int i = 0; i < n; i++) {
scanf("%d", &temp);
if (temp == -1) {
root = i;
}
else {
v[temp].push_back(i);
}
}
DFS(root, 0);
printf("%.2f %d", p * pow(1 + r / 100, maxdepth), maxnum);

return 0;
}