fork download
  1. #include<bits/stdc++.h>
  2. #include<fstream>
  3. #include<chrono>
  4. #define fi(i,a,b) for(int i=(a);i<(b);i++)
  5. #define llu long long
  6. #define no no_nodes_at_level
  7. using namespace::std;
  8. int main()
  9. {
  10. int n, m, i = 0, id;
  11. llu l, y, x, u, v;
  12. // freopen("in.txt", "r", stdin);
  13. ios::sync_with_stdio(0);
  14. cin.tie(0);
  15. vector< pair< llu, set<llu> > > a;
  16. cin>>n>>m;
  17. a = vector< pair< llu, set<llu> > >(n+1);
  18. vector<llu> coins(n+1, 0);
  19. vector<llu> no(n+1, -1);
  20. a[1].first = 0;
  21. a[0].first = -1;
  22. while(++i < n)
  23. {
  24. cin>>u>>v;
  25. a[u].second.insert(v);
  26. a[v].first = a[u].first + 1;
  27. no[a[v].first] = no[a[v].first] + 1;
  28. // par[v] = u;
  29. }
  30. llu ausi;
  31. while(m--)
  32. {
  33. cin>>id;
  34. if(id == 1)
  35. {
  36. cin>>l>>y;
  37. coins[l] = coins[l] + y;
  38.  
  39. }
  40. else
  41. {
  42. ausi = 0;
  43. cin>>x;
  44. set<llu> bag;
  45. bag.insert(x);
  46. while(!bag.empty())
  47. {
  48. auto top = bag.begin();
  49. bag.erase(bag.begin());
  50. ausi += coins[a[*top].first];
  51. for(auto it : a[*top].second)
  52. bag.insert(it);
  53. }
  54. cout<<ausi<<endl;
  55. }
  56. }
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0s 3236KB
stdin
5 4
1 2
1 3
3 4
3 5
1 1 2
1 2 3
2 3
2 1
stdout
8
10