-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCherry Tree.java
59 lines (54 loc) · 1.56 KB
/
Cherry Tree.java
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
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.*;
import java.io.*;
public class CherryTree {
static node[] tree = new node[10001];
static int N,C,K,ans = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String in[] = br.readLine().split(" ");
N = Integer.parseInt(in[0]);
C = Integer.parseInt(in[1]);
K = Integer.parseInt(in[2]);
in = br.readLine().split(" ");
for(int i = 1; i <= N;i++){
tree[i] = new node();
tree[i].cherries = Integer.parseInt(in[i-1]);
}
int a,b,k;
for(int i = 1;i < N;i++){
in = br.readLine().split(" ");
a = Integer.parseInt(in[0]);
b = Integer.parseInt(in[1]);
k = Integer.parseInt(in[2]);
tree[a].children.add(new pair(b,k));
tree[b].weight = k;
}
define(1);
//System.out.println(tree[7].weight);
System.out.println(ans);
}//main
static void define(int root){
int chi;
//System.out.println(root);
for(int i = 0;i< tree[root].children.size();i++){
chi = tree[root].children.get(i).num;
define(chi);
tree[root].cherries += tree[chi].cherries;
tree[root].weight += tree[chi].weight;
}
if(tree[root].cherries >= C && tree[root].weight <= K && root != 1) ans++;
}
static class pair{
int num;
int weight;
pair(int num, int weight){
this.num = num;
this.weight = weight;
}
}
static class node{
int cherries;// the total number of cherries should this become the root
int weight;//the total weight
ArrayList<pair> children = new ArrayList<pair>();
}
}