結果
| 問題 | 
                            No.1637 Easy Tree Query
                             | 
                    
| コンテスト | |
| ユーザー | 
                             a01sa01to
                         | 
                    
| 提出日時 | 2025-04-20 00:23:38 | 
| 言語 | C++23  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 59 ms / 2,000 ms | 
| コード長 | 836 bytes | 
| コンパイル時間 | 3,228 ms | 
| コンパイル使用メモリ | 281,848 KB | 
| 実行使用メモリ | 18,432 KB | 
| 最終ジャッジ日時 | 2025-04-20 00:23:47 | 
| 合計ジャッジ時間 | 8,224 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 33 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
  #include "settings/debug.cpp"
#else
  #define Debug(...) void(0)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;
int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vector Graph(n, vector<int>(0));
  rep(_, n - 1) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    Graph[u].push_back(v);
    Graph[v].push_back(u);
  }
  vector<int> dp(n, 0);
  function<void(int, int)> dfs = [&](int u, int p) {
    for (int v : Graph[u]) {
      if (v == p) continue;
      dfs(v, u);
      dp[u] += dp[v];
    }
    dp[u] += 1;
  };
  dfs(0, -1);
  ll ans = 0;
  while (q--) {
    int p, x;
    cin >> p >> x;
    --p;
    ans += (ll) dp[p] * x;
    cout << ans << '\n';
  }
  return 0;
}
            
            
            
        
            
a01sa01to