forked from hughbzhang/Competitive_Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bottleneckslow.cpp
46 lines (38 loc) · 911 Bytes
/
bottleneckslow.cpp
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
40
41
42
43
44
45
46
#include <cstdio>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int>::iterator vi;
int num, queries;
int par[100100];
vector<int> child[100100];
ll gate[100100];
ll cows[100100];
ll DFS(int cur, int mul){
ll total = cows[cur];
for(vi it = child[cur].begin();it!=child[cur].end();++it){
total+=DFS(*it,mul);
}
//printf("%lld\n",total);
return min(mul*gate[cur],total);
}
int main(){
freopen("bottleneck.in","r",stdin);
freopen("bottleneck.out","w",stdout);
scanf("%d%d",&num,&queries);
int a,b,c;
for(int x = 1;x<num;x++){
scanf("%d%d%d",&a,&b,&c);
a--;
par[x] = a;
child[a].push_back(x);
cows[x] = b;
gate[x] = c;
}
gate[0] = 9999999;
for(int x = 0;x<queries;x++){
scanf("%d",&a);
ll ans = DFS(0,a);
printf("%lld\n",ans);
}
}