fork download
  1. #pragma warning (disable : 4996)
  2. #include<iostream>
  3. #include<string>
  4. #include<stack>
  5. #include<vector>
  6. #include<cmath>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<sstream>
  10. #include<ctype.h>
  11. #include<memory.h>
  12. #include<stdlib.h>
  13. #include<fstream>
  14. #include<set>
  15. #include<stdio.h>
  16. #include<bitset>
  17. #include<map>
  18. using namespace std;
  19.  
  20.  
  21. int vis[111111];
  22. int d[111111];
  23. int power[111111];
  24. vector<int> v[111111];
  25. int cnt;
  26. int n, m;
  27.  
  28. void bfs(int x){
  29. queue<int> q;
  30. q.push(x);
  31. vis[x] = 1;
  32. if (power[x] == 1)
  33. d[x] = 1;
  34. while (!q.empty()){
  35. int n = q.front();
  36. q.pop();
  37. for (int i = 0; i < (int)v[n].size(); i++){
  38. int x = v[n][i];
  39. if (!vis[x]){
  40. if (power[x] && power[n])
  41. d[x] = d[n] + 1;else
  42. if (power[x])d[x]=1;
  43. vis[x] = 1;
  44. if (d[x] <= m){
  45. q.push(x);
  46. if (v[x].size() == 1)
  47. cnt++;
  48. }
  49. }
  50. }
  51. }
  52. }
  53.  
  54. int main(){
  55. // freopen("input.in", "r", stdin);
  56. scanf("%d%d", &n, &m);
  57.  
  58. for (int i = 0; i < n; i++)
  59. scanf("%d", &power[i]);
  60. for (int i = 0; i <n-1; i++){
  61. int a, b;
  62. scanf("%d%d", &a, &b);
  63. --a, --b;
  64. v[a].push_back(b);
  65. v[b].push_back(a);
  66. }
  67. bfs(0);
  68. printf("%d", cnt);
  69.  
  70. return 0;
  71. }
  72.  
Success #stdin #stdout 0s 6072KB
stdin
Standard input is empty
stdout
Standard output is empty