Bobjoehi

CherryTrees

Dec 20th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class CherryTree{
  4.    
  5.     static int n, c, k, counter = 0;
  6.     static int[] cherries, weight;
  7.     static ArrayList<LinkedList<Pair>> adj = new ArrayList<LinkedList<Pair>>();
  8.    
  9.     static void dfs(int n) {
  10.         if(adj.get(n).size() == 0) {
  11.             if(cherries[n] >= c && weight[n] <= k) {
  12.                 counter ++;
  13.             }
  14.             return;
  15.         }
  16.         for(Pair p : adj.get(n)) {
  17.             int next = p.a;
  18.             weight[next] += p.b;
  19.             dfs(next);
  20.             cherries[n] += cherries[next];
  21.             weight[n] += weight[next];
  22.         }
  23.         if(n != 1 && cherries[n] >= c && weight[n] <= k) {
  24.             counter ++;
  25.         }
  26.     }
  27.    
  28.     public static void main(String[] args) throws IOException{
  29.         readLine();
  30.         n = nextInt();
  31.         c = nextInt();
  32.         k = nextInt();
  33.        
  34.         for(int i = 0; i <= n; i ++) {
  35.             adj.add(new LinkedList<Pair>());
  36.         }
  37.         cherries = new int[n + 1];
  38.         weight = new int[n + 1];
  39.         readLine();
  40.        
  41.         for(int i = 1; i <= n; i ++) {
  42.             cherries[i] = nextInt();
  43.         }
  44.        
  45.         for(int i = 0; i < n - 1; i ++) {
  46.             readLine();
  47.             adj.get(nextInt()).add(new Pair(nextInt(), nextInt()));
  48.         }
  49.        
  50.         dfs(1);
  51.         System.out.println(counter);
  52.     }
  53.     static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  54.     static StringTokenizer st;
  55.     static void readLine() throws IOException{
  56.         st = new StringTokenizer(in.readLine());
  57.     }
  58.     static int nextInt() {
  59.         return Integer.parseInt(st.nextToken());
  60.     }
  61.    
  62.     static class Pair{
  63.         int a, b;
  64.         Pair(int a, int b){
  65.             this.a = a;
  66.             this.b = b;
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment