Advertisement
thouxanbanuno

Kepa

Nov 12th, 2019
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. include <bits/stdc++.h>
  2. #define pb push_back
  3. using namespace std;
  4.     vector<vector<int> >g;
  5.     vector<bool> v;
  6.     int ans=0;
  7.     int n,m;
  8.     vector<int> d;
  9. bool bfs(int x){
  10.     queue<int> q;
  11.     q.push(x);
  12.     if(v[x])
  13.     d[x]=1;
  14.     else d[x]=0;
  15.     while(!q.empty()){
  16.         int to=q.front();
  17.         q.pop();
  18.         if(d[to]>m){
  19.             continue;
  20.         }
  21.         for(int i=0; i<g[to].size(); i++){
  22.             int nt=g[to][i];
  23.             if(v[nt]){
  24.                 d[nt]=d[to]+1;
  25.             }
  26.             else{
  27.                 d[nt]=0;
  28.             }
  29.             q.push(nt);
  30.         }
  31.     }
  32.     return 0;
  33. }
  34. int main(){
  35.     cin>>n>>m;
  36.     bool a;
  37.     for(int i=0; i<n; i++){
  38.         cin>>a;
  39.         v.pb(a);
  40.     }
  41.     d.resize(n,-1);
  42.     g.resize(n);
  43.     for(int i=0; i<n-1; i++){
  44.         int x,y;
  45.         cin>>x>>y;
  46.         g[--x].pb(--y);
  47.     }
  48.     /*for(int i=0; i<g.size(); i++)
  49.         if(g[i].size()==0)
  50.             g[i].pb(-1);*/
  51.     bfs(0);
  52.     for(int i=0; i<g.size(); i++){
  53.         if(!g[i].size()){
  54.             if(d[i]!=-1 && d[i]<=m)
  55.                 ans++;
  56.         }
  57.     }
  58.    /* for(int i=0; i<d.size(); i++)
  59.         cout<<d[i]<<' ';
  60. */
  61. cout<<ans;
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement