結果

問題 No.1637 Easy Tree Query
ユーザー umezo
提出日時 2021-08-06 21:26:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 647 bytes
コンパイル時間 2,001 ms
コンパイル使用メモリ 195,296 KB
最終ジャッジ日時 2025-01-23 14:33:45
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

vector<int> G[100100];
ll A[100100];

void dfs(int v,int p){
  A[v]=1;
  for(auto nv:G[v]){
    if(nv==p) continue;
    dfs(nv,v);
    A[v]+=A[nv];
  }
} 
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n,q;
  cin>>n>>q;
  rep(i,n-1){
    int a,b;
    cin>>a>>b;
    a--,b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  
  dfs(0,-1);
  
  ll ans=0;
  rep(i,q){
    ll p,x;
    cin>>p>>x;
    p--;
    ans+=A[p]*x;
    cout<<ans<<endl;
  }
  
    
  return 0;
}
0