fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. int main(){
  7. ios_base::sync_with_stdio(false);
  8. cin.tie(NULL);
  9. int n;
  10. cin>>n;
  11. vector<int>t[n+1],value(n+1);
  12. for(int i=1;i<n;i++){
  13. int x,y;
  14. cin>>x>>y;
  15. t[x].push_back(y);
  16. t[y].push_back(x);
  17. }
  18. for(int i=1;i<=n;i++)cin>>value[i];
  19. vector<int>v(n+1,0),ones(n+1,0);
  20. queue<int>q;
  21. q.push(1);
  22. v[1]=1;
  23. ones[1]=value[1];
  24. while(!q.empty()){
  25. int front=q.front();
  26. for(auto it:t[front]){
  27. if(v[it]==0){
  28. v[it]=1;
  29. q.push(it);
  30. ones[it]=ones[front]+value[it];
  31. }
  32. }
  33. q.pop();
  34. }
  35. for(int i=1;i<=n;i++)cout<<"Number of 1's till node "<<i<<" "<<ones[i]<<"\n";
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5316KB
stdin
5
1 2
2 3 
3 4 
4 5 
1 0 0 1 1
stdout
Number of 1's till node 1 1
Number of 1's till node 2 1
Number of 1's till node 3 1
Number of 1's till node 4 2
Number of 1's till node 5 3