Codeforces Round #321 (Div. 2) 580C. Kefa and Park Solution
Problem Link: https://codeforces.com/problemset/problem/580/C
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1e5 + 9;
- std::vector<int> adj[N];
- int vis[N], ar[N], ans = 0, cnt = 0, m;
- void dfs(int n, int cnt) {
- vis[n] = 1;
- if (ar[n] == 1)
- cnt++;
- else if (ar[n] == 0 && cnt <= m)
- cnt = 0;
- if (adj[n].size() == 1 && cnt <= m && n != 1) ans++;
- for (auto v : adj[n]) {
- if (!vis[v]) dfs(v, cnt);
- }
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int n, cnt = 0;
- cin >> n >> m;
- for (int i = 1; i <= n; i++) cin >> ar[i];
- int t;
- t = n - 1;
- while (t--) {
- int u, v;
- cin >> u >> v;
- adj[u].push_back(v);
- adj[v].push_back(u);
- }
- dfs(1, cnt);
- cout << ans << endl;
- }
No comments