Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class CherryTree{
- static int n, c, k, counter = 0;
- static int[] cherries, weight;
- static ArrayList<LinkedList<Pair>> adj = new ArrayList<LinkedList<Pair>>();
- static void dfs(int n) {
- if(adj.get(n).size() == 0) {
- if(cherries[n] >= c && weight[n] <= k) {
- counter ++;
- }
- return;
- }
- for(Pair p : adj.get(n)) {
- int next = p.a;
- weight[next] += p.b;
- dfs(next);
- cherries[n] += cherries[next];
- weight[n] += weight[next];
- }
- if(n != 1 && cherries[n] >= c && weight[n] <= k) {
- counter ++;
- }
- }
- public static void main(String[] args) throws IOException{
- readLine();
- n = nextInt();
- c = nextInt();
- k = nextInt();
- for(int i = 0; i <= n; i ++) {
- adj.add(new LinkedList<Pair>());
- }
- cherries = new int[n + 1];
- weight = new int[n + 1];
- readLine();
- for(int i = 1; i <= n; i ++) {
- cherries[i] = nextInt();
- }
- for(int i = 0; i < n - 1; i ++) {
- readLine();
- adj.get(nextInt()).add(new Pair(nextInt(), nextInt()));
- }
- dfs(1);
- System.out.println(counter);
- }
- static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- static StringTokenizer st;
- static void readLine() throws IOException{
- st = new StringTokenizer(in.readLine());
- }
- static int nextInt() {
- return Integer.parseInt(st.nextToken());
- }
- static class Pair{
- int a, b;
- Pair(int a, int b){
- this.a = a;
- this.b = b;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment