580C. Kefa and Park - Codeforces Solution C++

  Problem Link : 580C. Kefa and Park 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

int ans;

void recur(int i,bool arr[],vector<int> adj[],int v,int n,int m,int parent)
{
	if(arr[i])
		v++;
	else
		v=0;
	if(adj[i].size()==1 && adj[i][0]==parent)
	{
		if(v<=m)
			ans++;

		return;
	}
	if(v>m)
		return;

	for(int j=0;j<adj[i].size();j++)
	{
		if(adj[i][j]!=parent)
			recur(adj[i][j],arr,adj,v,n,m,i);
	}
}

int main()
{
	int n,m;
	cin>>n>>m;
	bool arr[n+1];
	for(int i=1;i<=n;i++)
		cin>>arr[i];
	vector<int> adj[n+1];
	int x,y;
	for(int i=0;i<n-1;i++)
	{
		cin>>x>>y;
		adj[x].push_back(y);
		adj[y].push_back(x);
	}
	ans=0;

	recur(1,arr,adj,0,n,m,-1);
	
	cout<<ans;

}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!