結果

問題 No.1637 Easy Tree Query
ユーザー nawawannawawan
提出日時 2021-08-06 21:29:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 740 bytes
コンパイル時間 2,297 ms
コンパイル使用メモリ 174,220 KB
実行使用メモリ 13,628 KB
最終ジャッジ日時 2023-10-17 04:55:29
合計ジャッジ時間 9,363 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 263 ms
9,668 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 63 ms
6,500 KB
testcase_05 AC 221 ms
5,708 KB
testcase_06 AC 145 ms
5,180 KB
testcase_07 AC 77 ms
4,348 KB
testcase_08 AC 50 ms
5,972 KB
testcase_09 AC 171 ms
8,084 KB
testcase_10 AC 100 ms
4,688 KB
testcase_11 AC 234 ms
8,612 KB
testcase_12 AC 235 ms
7,556 KB
testcase_13 AC 37 ms
4,916 KB
testcase_14 AC 107 ms
8,348 KB
testcase_15 AC 218 ms
8,876 KB
testcase_16 AC 159 ms
9,404 KB
testcase_17 AC 64 ms
4,916 KB
testcase_18 AC 203 ms
5,708 KB
testcase_19 AC 199 ms
6,236 KB
testcase_20 AC 246 ms
7,292 KB
testcase_21 AC 120 ms
6,764 KB
testcase_22 AC 255 ms
9,668 KB
testcase_23 AC 62 ms
5,180 KB
testcase_24 AC 97 ms
6,764 KB
testcase_25 AC 224 ms
5,444 KB
testcase_26 AC 252 ms
7,556 KB
testcase_27 AC 280 ms
9,404 KB
testcase_28 AC 162 ms
5,444 KB
testcase_29 AC 192 ms
7,028 KB
testcase_30 AC 57 ms
7,028 KB
testcase_31 AC 176 ms
4,348 KB
testcase_32 AC 98 ms
5,444 KB
testcase_33 AC 210 ms
4,720 KB
testcase_34 AC 74 ms
13,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
    }
}
0