fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define endl "\n"
  4. #define int long long
  5. #define faster() ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  6. #define mod 1000000007
  7.  
  8. int n , m , res = 0;
  9. vector<int> adj[1005];
  10. bool visited[1005];
  11. int d[1005];// chi luu 0 , 1 dai dien khong hoac co con gian
  12. void DFS(int u, int cnt){
  13. if(cnt > m) return ;
  14. bool check = false ;
  15. visited[u] = true ;
  16. for(int v : adj[u]){
  17. if(!visited[v]){
  18. check = true ; // day chua phai la node la
  19. if(!d[v]) DFS(v , 0); // dem so 0 lien tuc neu gap so 0 thi reset lai
  20. else DFS(v , cnt + 1);
  21. }
  22.  
  23. }
  24. if(check == false) res++ ; // kiem tra xem co bao nhieu node la(khong di tiep dc)
  25. }
  26.  
  27. void solve() {
  28. cin >> n >> m ;
  29. for(int i = 1 ; i <= n ; i++){
  30. cin >> d[i];
  31. }
  32. for(int i = 1 ; i <= n - 1 ; i++){
  33. int x , y ; cin >> x >> y ;
  34. adj[x].push_back(y);
  35. adj[y].push_back(x);
  36. }
  37. DFS(1 , d[1]);// d[1] = 0 || d[1] = 1
  38. cout << res << endl;
  39.  
  40. }
  41.  
  42. signed main(){
  43. faster();
  44. int test = 1 ;
  45. // cin >> test ;
  46. while(test--) solve();
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
1