結果
| 問題 | 
                            No.1637 Easy Tree Query
                             | 
                    
| コンテスト | |
| ユーザー | 
                             eve__fuyuki
                         | 
                    
| 提出日時 | 2024-05-21 14:20:44 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 67 ms / 2,000 ms | 
| コード長 | 755 bytes | 
| コンパイル時間 | 1,927 ms | 
| コンパイル使用メモリ | 198,720 KB | 
| 最終ジャッジ日時 | 2025-02-21 16:23:39 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 33 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
void fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
}
int n;
vector<vector<int>> g;
vector<int> sz;
void dfs(int u, int pre) {
    sz[u] = 1;
    for (int v : g[u]) {
        if (v == pre) continue;
        dfs(v, u);
        sz[u] += sz[v];
    }
}
int main() {
    fast_io();
    int q;
    cin >> n >> q;
    g.resize(n);
    sz.resize(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);
    }
    dfs(0, -1);
    long long ans = 0;
    for (; q--;) {
        int p;
        long long x;
        cin >> p >> x;
        ans += x * sz[p - 1];
        cout << ans << "\n";
    }
}
            
            
            
        
            
eve__fuyuki