#include #include using namespace std; namespace mp = boost::multiprecision; struct Graph{ int N;//number of nodes.0-indexed. vector> edges;//Example:{{0,1},{1,2}} Graph(int n,vector> e){ N=n; edges=e; } vector> getConnection(){//各ノードの繋がりを配列化して返す vector> ret(N,vector(0)); for(int i=edges.size()-1;i>=0;--i){ ret[edges[i][0]].push_back(edges[i][1]); ret[edges[i][1]].push_back(edges[i][0]); } return ret; } vector getParent(int root){//rootを根とした時の各ノードの親 vector> connection=getConnection(); vector ret(N); vector used(N); queue que; que.push(root); ret[root]=-1; used[root]=1; while(!que.empty()){ for(int i:connection[que.front()]){ if(!used[i]){ ret[i]=que.front(); used[i]=1; que.push(i); } } que.pop(); } return ret; } vector> getChildren(int root){//rootを根とした時の各ノードの子 vector> connection=getConnection(); vector> ret(N,vector(0)); vector parent=getParent(root); for(int i=0;i bfs(int root){//幅優先探索の順番 vector> children=getChildren(root); queue que; vector ret(0); que.push(root); while(!que.empty()){ ret.push_back(que.front()); for(int i:children[que.front()]){ que.push(i); } que.pop(); } return ret; } }; int main(){ int N,Q; cin>>N>>Q; vector> edges(N-1,vector(2)); for(int i=0;i>edges[i][j]; --edges[i][j]; } } Graph g(N,edges); vector bfs=g.bfs(0); vector> children=g.getChildren(0); vector num(N,1); for(int i=N-1;i>=0;--i){ for(int j:children[bfs[i]]){ num[bfs[i]]+=num[j]; } } for(int i=0;i>P>>x; --P; ans+=num[P]*x; cout<