fork download
  1. import java.util.*;
  2.  
  3. import java.io.*;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[]args){
  8.  
  9. FastReader in=new FastReader();
  10.  
  11. int n=in.nextInt();
  12.  
  13. int m=in.nextInt();
  14.  
  15. int[]x=new int[n];
  16.  
  17. for(int i=0;i<x.length;i++){
  18.  
  19. x[i]=in.nextInt();
  20.  
  21. }
  22.  
  23. List <Integer> [] g=new List[n];
  24.  
  25. for(int i=0;i<g.length;i++){
  26.  
  27. g[i]=new ArrayList();
  28.  
  29. }
  30.  
  31. boolean [] vis=new boolean[n];
  32.  
  33. int e=n-1;
  34.  
  35. while(e-->0){
  36.  
  37. int a=in.nextInt()-1;
  38.  
  39. int b=in.nextInt()-1;
  40.  
  41. g[a].add(b);
  42.  
  43. g[b].add(a);
  44.  
  45. }
  46.  
  47. Stack<Integer> s=new Stack();
  48.  
  49. s.push(0);
  50.  
  51. vis[0]=true;
  52.  
  53. int count=0;
  54.  
  55. while(!s.isEmpty()){
  56.  
  57. int p=s.pop();
  58.  
  59. boolean t=true;
  60.  
  61. for(int y=0;y<g[p].size();y++){
  62.  
  63. int c=g[p].get(y);
  64.  
  65. if(!vis[c]){
  66.  
  67. t=false;
  68.  
  69. if(x[c]>0){
  70.  
  71. if(!(x[p]+1>m)){
  72.  
  73. s.push(c); x[c]=x[p]+1;
  74.  
  75. }
  76.  
  77. }
  78.  
  79. else
  80. s.push(c);
  81.  
  82. vis[c]=true;
  83.  
  84. }
  85.  
  86. }
  87.  
  88. if(t){
  89.  
  90. count++;
  91.  
  92. }
  93.  
  94. }
  95.  
  96.  
  97. System.out.println(count);
  98.  
  99. }
  100.  
  101.  
  102. }
  103.  
  104.  
  105. class FastReader
  106. {
  107.  
  108. public FastReader()
  109. {
  110. br = new BufferedReader(new
  111. }
  112.  
  113. String next()
  114. {
  115. while (st == null || !st.hasMoreElements())
  116. {
  117. try
  118. {
  119. st = new StringTokenizer(br.readLine());
  120. }
  121. catch (IOException e)
  122. {
  123. e.printStackTrace();
  124. }
  125. }
  126. return st.nextToken();
  127. }
  128.  
  129. int nextInt()
  130. {
  131. return Integer.parseInt(next());
  132. }
  133.  
  134. long nextLong()
  135. {
  136. return Long.parseLong(next());
  137. }
  138.  
  139. double nextDouble()
  140. {
  141. return Double.parseDouble(next());
  142. }
  143.  
  144. String nextLine()
  145. {
  146. String str = "";
  147. try
  148. {
  149. str = br.readLine();
  150. }
  151. catch (IOException e)
  152. {
  153. e.printStackTrace();
  154. }
  155. return str;
  156. }
  157. }
Runtime error #stdin #stdout #stderr 0.08s 51268KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.NullPointerException
	at java.base/java.util.StringTokenizer.<init>(StringTokenizer.java:199)
	at java.base/java.util.StringTokenizer.<init>(StringTokenizer.java:236)
	at FastReader.next(Main.java:122)
	at FastReader.nextInt(Main.java:134)
	at Main.main(Main.java:11)