title: DFS giving TLE in kefa and park (codeforces 560C)
body: <blockquote>
    <p>Kefa decided to celebrate his first big salary by going to the
    restaurant.</p>
    
    <p>He lives by an unusual park. The park is a rooted tree consisting of n
    vertices with the root at vertex 1. Vertex 1 also contains Kefa's
    house. Unfortunaely for our hero, the park also contains cats. Kefa
    has already found out what are the vertices with cats in them.</p>
    
    <p>The leaf vertices of the park contain restaurants. Kefa wants to
    choose a restaurant where he will go, but unfortunately he is very
    afraid of cats, so there is no way he will go to the restaurant if the
    path from the restaurant to his house contains more than m consecutive
    vertices with cats.</p>
    
    <p>Your task is to help Kefa count the number of restaurants where he can
    go.</p>
    
    <p>Input The first line contains two integers, n and m (2 ≤ n ≤ 105,
    1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number
    of consecutive vertices with cats that is still ok for Kefa.</p>
    
    <p>The second line contains n integers a1, a2, ..., an, where each ai
    either equals to 0 (then vertex i has no cat), or equals to 1 (then
    vertex i has a cat).</p>
    
    <p>Next n - 1 lines contains the edges of the tree in the format "xi yi"
    (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are
    the vertices of the tree, connected by an edge.</p>
    
    <p>It is guaranteed that the given set of edges specifies a tree.</p>
    
    <p>Output A single integer — the number of distinct leaves of a tree the
    path to which from Kefa's home contains at most m consecutive vertices
    with cats.</p>
    
    <p>Examples input</p>
    
    <p>4 1</p>
    
    <p>1 1 0 0</p>
    
    <p>1 2</p>
    
    <p>1 3</p>
    
    <p>1 4</p>
    
    <p>output</p>
    
    <p>2</p>
    
    <p>input</p>
    
    <p>7 1</p>
    
    <p>1 0 1 1 0 0 0</p>
    
    <p>1 2</p>
    
    <p>1 3</p>
    
    <p>2 4</p>
    
    <p>2 5</p>
    
    <p>3 6</p>
    
    <p>3 7</p>
    
    <p>output</p>
    
    <p>2</p>
  </blockquote>
  
  <p>This dfs algo is pretty much similar to many accepted submissions on this question, still getting tle... It's running well in custom cases... I'm unable to find where does it giving TLE</p>
  
  <pre><code>#include&lt;bits/stdc++.h&gt;
  #define ll unsigned long long int 
  #define ld long double
  #define pb push_back
  #define fillarray for(int i=0;i&lt;n;i++) 
  using namespace std;
  
  int give(auto edge,auto node,auto visited,int index,int m,int x)
  {
      if(edge[index].size()==1&amp;&amp;index!=0)
      {
          if(x+node[index]&lt;=m) return 1;
          else return 0;
      }
      if(visited[index]) return 0;
      visited[index]=1;
  
      if(node[index]) x++;
      else x=0;
  
      if(x&gt;m) return 0;
  
      int sum=0;
      for(int i=0;i&lt;edge[index].size();i++)
      {
          sum+=give(edge,node,visited,edge[index][i],m,x);
      }
      return sum;
  }
  int main() 
  {
      int n,m; 
      cin&gt;&gt;n&gt;&gt;m;
      vector&lt;vector&lt;int&gt;&gt; edge(n);
      vector&lt;bool&gt; node(n);
      vector&lt;bool&gt; visited(n,0);
  
      fillarray{
          bool x; cin&gt;&gt;x;
          node[i]=x;
      }
      for(int i=0;i&lt;n-1;i++)
      {
          int x,y;
          cin&gt;&gt;x&gt;&gt;y;
          edge[x-1].pb(y-1);
          edge[y-1].pb(x-1);
      }
      cout&lt;&lt;give(edge,node,visited,0,m,0);
  }
  </code></pre>