Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- int n,m;
- cin>>n>>m;
- set<int> s;
- for(int i=1; i<=n; i++)
- s.insert(i);
- vector<int> c(n+1);
- for(int i=1; i<=n; i++)
- {
- cin>>c[i];
- //if(c[i]==1)
- //s.erase(i);
- }
- vector<vector<int>> l;
- for(int i=0; i<=n; i++)
- {
- vector<int> t;
- l.push_back(t);
- }
- for(int i=1; i<n; i++)
- {
- int u,v;
- cin>>u>>v;
- l[u].push_back(v);
- s.erase(u);
- }
- int d[n+1];
- int v[n+1];
- for(int i=1; i<=n; i++)
- {
- d[i] = INT_MAX;
- v[i] = 0;
- }
- if(c[1]==0)
- d[1] = 0;
- else
- d[1] = 1;
- queue<int> q;
- q.push(1);
- while(q.size()!=0)
- {
- int x = q.front();
- q.pop();
- v[x] = 1;
- for(int i=0; i<l[x].size(); i++)
- {
- int y = l[x][i];
- if(v[y]==0)
- {
- q.push(y);
- if(d[x]==m+1)
- {
- d[y] = m+1;
- continue;
- }
- if(c[x]!=0)
- d[y] = d[x] + c[y];
- else
- d[y] = c[y];
- }
- }
- }
- int count = 0;
- for(auto i=s.begin(); i!=s.end(); i++)
- {
- int l = *i;
- if(d[l]<=m)
- count++;
- }
- cout<<count<<endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment