Skip to content

Commit f99566c

Browse files
committed
Solved Issue #547: Kefa and Park (Graph DFS)
1 parent 9a1edc4 commit f99566c

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Submission Link:
3+
https://codeforces.com/contest/580/submission/356719898
4+
*/
5+
6+
/*
7+
Problem: Kefa and Park
8+
Link: https://codeforces.com/problemset/problem/580/C
9+
Author: Krishna200608
10+
11+
Problem Summary:
12+
Kefa starts at node 1 and wants to reach any leaf node in the tree.
13+
He avoids paths that contain more than m consecutive nodes with cats.
14+
The task is to count how many leaf nodes are reachable without breaking this rule.
15+
16+
Approach:
17+
18+
1. Treat the given graph as a tree rooted at node 1 and start DFS from it.
19+
2. While traversing, keep track of consecutive nodes having cats.
20+
3. Increment the count if the current node has a cat, otherwise reset it to 0.
21+
4. If at any time the consecutive cat count exceeds m, prune that path.
22+
5. Identify a leaf node as a node having no children other than its parent.
23+
6. Count every leaf node reached through a valid path as a safe destination.
24+
25+
26+
Complexity:
27+
Time: O(N)
28+
Space: O(N)
29+
*/
30+
31+
#include<bits/stdc++.h>
32+
using namespace std;
33+
#define MOD 1000000007
34+
const int M = 1e9+7;
35+
const double PI = acos(-1.0);
36+
#define INF 1e18
37+
#define pb push_back
38+
#define ppb pop_back
39+
#define ll long long
40+
#define no cout << "NO" << endl;
41+
#define yes cout << "YES" << endl;
42+
#define ff first
43+
#define ss second
44+
#define inn(x) int x; std::cin >> x;
45+
#define ill(x) ll x; std::cin >> x;
46+
#define all(x) x.begin(),x.end()
47+
#define in(a) for(int i = 0; i<a.size(); i++) cin>>a[i];
48+
#define out(a) for(int i = 0; i<a.size(); i++) cout<<a[i]<< " " ;
49+
typedef vector<int> vi;
50+
typedef vector<ll> vll;
51+
#define ceil_div(n, x)  ( ((n) % (x) == 0) ? ((n) / (x)) : ((n) / (x) + 1) )
52+
#define debug(x) cout << "x -> " << x << endl;
53+
#define outt(x) cout << x << endl;
54+
#define endl "\n"
55+
 
56+
vi cat;
57+
vector<vi> adj;
58+
int n, m;
59+
int ans = 0;
60+
61+
void dfs(int u, int p, int consec) {
62+
if(cat[u]) consec++;
63+
else consec = 0;
64+
65+
if(consec > m) return;
66+
67+
bool isLeaf = true;
68+
for(int v : adj[u]) {
69+
if(v != p) {
70+
isLeaf = false;
71+
dfs(v, u, consec);
72+
}
73+
}
74+
75+
if(isLeaf) ans++;
76+
}
77+
78+
void solve(){
79+
cin >> n >> m;
80+
cat.resize(n + 1);
81+
adj.resize(n + 1);
82+
83+
for(int i = 1; i <= n; i++) cin >> cat[i];
84+
85+
for(int i = 0; i < n - 1; i++) {
86+
int u, v;
87+
cin >> u >> v;
88+
adj[u].pb(v);
89+
adj[v].pb(u);
90+
}
91+
92+
dfs(1, 0, 0);
93+
outt(ans);
94+
}
95+
96+
signed main(){
97+
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
98+
auto begin = std::chrono::high_resolution_clock::now();
99+
    int t=1;
100+
    while(t--){
101+
        solve();
102+
    }
103+
 
104+
 
105+
 
106+
auto end = std::chrono::high_resolution_clock::now();
107+
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
108+
cerr << "Time measured: " << elapsed.count() * 1e-6 << "ms";
109+
return 0;
110+
}

0 commit comments

Comments
 (0)