danielvitor23

Kefa and Park

Apr 17th, 2023
1,200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, m;
  5. vector<vector<int>> gr;
  6. vector<int> c;
  7.  
  8. int solve(int u, int cnt, int par) {
  9.   if (cnt > m) return 0;
  10.   int subtr_count = (gr[u].size() == 1 and par == gr[u][0] ? 1 : 0);
  11.   for (int to : gr[u]) if (to != par) {
  12.     subtr_count += solve(to, (c[to] ? cnt + c[to] : 0), u);
  13.   }
  14.   return subtr_count;
  15. }
  16.  
  17. int main() {
  18.   cin.tie(0)->sync_with_stdio(0);
  19.  
  20.   cin >> n >> m;
  21.  
  22.   gr.assign(n, vector<int>());
  23.   c.assign(n, -1);
  24.  
  25.   for (int i = 0; i < n; ++i) {
  26.     cin >> c[i];
  27.   }
  28.  
  29.   for (int i = 0; i < n-1; ++i) {
  30.     int a, b; cin >> a >> b, --a, --b;
  31.     gr[a].push_back(b);
  32.     gr[b].push_back(a);
  33.   }
  34.  
  35.   cout << solve(0, c[0], -1) << '\n';
  36. }
Advertisement
Add Comment
Please, Sign In to add comment