結果
| 問題 | 
                            No.1637 Easy Tree Query
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2021-08-06 21:29:00 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 291 ms / 2,000 ms | 
| コード長 | 740 bytes | 
| コンパイル時間 | 1,721 ms | 
| コンパイル使用メモリ | 173,900 KB | 
| 実行使用メモリ | 13,568 KB | 
| 最終ジャッジ日時 | 2024-09-17 03:38:33 | 
| 合計ジャッジ時間 | 9,083 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge6 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 33 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
int main(){
    int N, Q;
    cin >> N >> Q;
    vector<vector<int>> G(N);
    for(int i = 0; i < N - 1; i++){
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    vector<long long> cnt(N);
    auto dfs = [&](auto dfs, int now, int par) -> void{
        for(int t: G[now]){
            if(t == par) continue;
            dfs(dfs, t, now);
            cnt[now] += cnt[t];
        }
        cnt[now]++;
    };
    dfs(dfs, 0, -1);
    long long ans = 0;
    while(Q--){
        int p;
        cin >> p;
        long long x;
        cin >> x;
        p--;
        ans += cnt[p] * x;
        cout << ans << endl;
    }
}