Language: C++

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>

//#include<bits/stdc++.h>

using namespace std; 

class Vertex{
    int id;
    int counter;
    public:
        Vertex(int n): id(n), counter(0){}
        int getCounter(){
            return counter;
        }
        void addCounter(int n){
            counter += n;
        }
};

class Tree{
    protected:
        map<int, vector<int> > subs;
    public:
        // initialize vertices
        map<int, Vertex > vertices;
        Tree(int n) {
            for(int i = 1; i <= n; i++){
                vertices[i] = Vertex(i);
            }
        }
        void addSub(int a, int b){
            subs[a].push_back(b);
        }
        void addSubCounter(int n, int val){
            this->vertices[n].addCounter(val);
            // Check for existing subs
            for(int i = 0; i < subs.count(n); i++){
                // Add counter to subs as well
                int node = subs[n][i];
                addSubCounter(node,val);
            }
        }
};

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, q;
    cin >> n >> q;

    Tree tree(n);

    for(int i=0 ; i < n-1; i++){
        int a, b;
        cin >> a >> b;
        // Initialize subs on tree
        tree.addSub(a,b);
    }
    for(int i = 0 ; i < q; i++){
        int p, q;
        cin >> p >> q;
        tree.addSubCounter(p,q);
    }

    for(int i = 1; i <= n; i++){
        cout << tree.vertices[i].getCounter() << " ";
    }


    

    
    return 0;
}

Compiler output:
In file included from /usr/include/c++/14/bits/memory_resource.h:47,
                 from /usr/include/c++/14/string:68,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/tuple: In instantiation of 'std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {const int&}; long unsigned int ..._Indexes1 = {0}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const int; _T2 = Vertex]':
/usr/include/c++/14/tuple:2879:63:   required from 'std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {const int&}; _Args2 = {}; _T1 = const int; _T2 = Vertex]'
 2879 |              typename _Build_index_tuple<sizeof...(_Args2)>::__type())
      |                                                                      ^
/usr/include/c++/14/bits/new_allocator.h:191:4:   required from 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, Vertex>; _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, Vertex> >]'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/alloc_traits.h:575:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const int, Vertex>; _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, Vertex> >; allocator_type = std::allocator<std::_Rb_tree_node<std::pair<const int, Vertex> > >]'
  575 |           __a.construct(__p, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:593:32:   required from 'void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_construct_node(_Link_type, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, Vertex>; _KeyOfValue = std::_Select1st<std::pair<const int, Vertex> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Vertex> >; _Link_type = std::_Rb_tree_node<std::pair<const int, Vertex> >*]'
  593 |               _Alloc_traits::construct(_M_get_Node_allocator(),
      |               ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
  594 |                                        __node->_M_valptr(),
      |                                        ~~~~~~~~~~~~~~~~~~~~
  595 |                                        std::forward<_Args>(__args)...);
      |                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:610:21:   required from 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, Vertex>; _KeyOfValue = std::_Select1st<std::pair<const int, Vertex> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Vertex> >; _Link_type = std::_Rb_tree_node<std::pair<const int, Vertex> >*]'
  610 |           _M_construct_node(__tmp, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:1633:32:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Auto_node::_Auto_node(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, Vertex>; _KeyOfValue = std::_Select1st<std::pair<const int, Vertex> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Vertex> >]'
 1633 |             _M_node(__t._M_create_node(std::forward<_Args>(__args)...))
      |                     ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:2460:13:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(const_iterator, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, Vertex>; _KeyOfValue = std::_Select1st<std::pair<const int, Vertex> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Vertex> >; iterator = std::_Rb_tree<int, std::pair<const int, Vertex>, std::_Select1st<std::pair<const int, Vertex> >, std::less<int>, std::allocator<std::pair<const int, Vertex> > >::iterator; const_iterator = std::_Rb_tree<int, std::pair<const int, Vertex>, std::_Select1st<std::pair<const int, Vertex> >, std::less<int>, std::allocator<std::pair<const int, Vertex> > >::const_iterator]'
 2460 |         _Auto_node __z(*this, std::forward<_Args>(__args)...);
      |                    ^~~
/usr/include/c++/14/bits/stl_map.h:513:37:   required from 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = int; _Tp = Vertex; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Vertex> >; mapped_type = Vertex; key_type = int]'
  513 |           __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct,
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  514 |                                             std::tuple<const key_type&>(__k),
      |                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  515 |                                             std::tuple<>());
      |                                             ~~~~~~~~~~~~~~~
a.cc:42:27:   required from here
   42 |                 vertices[i] = Vertex(i);
      |                           ^
/usr/include/c++/14/tuple:2890:9: error: no matching function for call to 'Vertex::Vertex()'
 2890 |         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:25:9: note: candidate: 'Vertex::Vertex(int)'
   25 |         Vertex(int n): id(n), counter(0){}
      |         ^~~~~~
a.cc:25:9: note:   candidate expects 1 argument, 0 provided
a.cc:21:7: note: candidate: 'constexpr Vertex::Vertex(const Vertex&)'
   21 | class Vertex{
      |       ^~~~~~
a.cc:21:7: note:   candidate expects 1 argument, 0 provided
a.cc:21:7: note: candidate: 'constexpr Vertex::Vertex(Vertex&&)'
a.cc:21:7: note:   candidate expects 1 argument, 0 provided