Codeforces Round #321 (Div. 2) 580C. Kefa and Park Solution

 Problem Link: https://codeforces.com/problemset/problem/580/C

Solution in C++:

  1. /// La ilaha illellahu muhammadur rasulullah
  2. ///******Bismillahir-Rahmanir-Rahim******///
  3. /// Abul Hasnat Tonmoy
  4. /// Department of CSE,23rd batch
  5. /// Islamic University,Bangladesh
  6. ///**********ALLAH IS ALMIGHTY************///
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9. const int N = 1e5 + 9;
  10. std::vector<int> adj[N];
  11. int vis[N], ar[N], ans = 0, cnt = 0, m;
  12. void dfs(int n, int cnt) {
  13. vis[n] = 1;
  14. if (ar[n] == 1)
  15. cnt++;
  16. else if (ar[n] == 0 && cnt <= m)
  17. cnt = 0;
  18. if (adj[n].size() == 1 && cnt <= m && n != 1) ans++;
  19. for (auto v : adj[n]) {
  20. if (!vis[v]) dfs(v, cnt);
  21. }
  22. }
  23. int main() {
  24. ios_base::sync_with_stdio(0);
  25. cin.tie(0);
  26. int n, cnt = 0;
  27. cin >> n >> m;
  28. for (int i = 1; i <= n; i++) cin >> ar[i];
  29. int t;
  30. t = n - 1;
  31. while (t--) {
  32. int u, v;
  33. cin >> u >> v;
  34. adj[u].push_back(v);
  35. adj[v].push_back(u);
  36. }
  37. dfs(1, cnt);
  38. cout << ans << endl;
  39. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.