結果

問題 No.1637 Easy Tree Query
ユーザー ぷらぷら
提出日時 2021-08-06 21:24:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 713 bytes
コンパイル時間 2,536 ms
コンパイル使用メモリ 203,668 KB
実行使用メモリ 17,332 KB
最終ジャッジ日時 2023-10-17 04:41:37
合計ジャッジ時間 9,592 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,380 KB
testcase_01 AC 3 ms
6,384 KB
testcase_02 AC 226 ms
9,524 KB
testcase_03 AC 3 ms
6,316 KB
testcase_04 AC 51 ms
8,040 KB
testcase_05 AC 191 ms
7,572 KB
testcase_06 AC 122 ms
7,264 KB
testcase_07 AC 67 ms
6,452 KB
testcase_08 AC 41 ms
7,684 KB
testcase_09 AC 139 ms
8,816 KB
testcase_10 AC 83 ms
7,084 KB
testcase_11 AC 189 ms
9,076 KB
testcase_12 AC 195 ms
8,460 KB
testcase_13 AC 30 ms
7,128 KB
testcase_14 AC 83 ms
8,872 KB
testcase_15 AC 175 ms
9,192 KB
testcase_16 AC 129 ms
9,500 KB
testcase_17 AC 54 ms
7,124 KB
testcase_18 AC 172 ms
7,572 KB
testcase_19 AC 167 ms
7,812 KB
testcase_20 AC 203 ms
8,400 KB
testcase_21 AC 100 ms
8,084 KB
testcase_22 AC 205 ms
9,572 KB
testcase_23 AC 52 ms
7,292 KB
testcase_24 AC 78 ms
8,092 KB
testcase_25 AC 191 ms
7,476 KB
testcase_26 AC 209 ms
8,576 KB
testcase_27 AC 228 ms
9,420 KB
testcase_28 AC 145 ms
7,400 KB
testcase_29 AC 157 ms
8,300 KB
testcase_30 AC 53 ms
8,280 KB
testcase_31 AC 153 ms
6,392 KB
testcase_32 AC 83 ms
7,516 KB
testcase_33 AC 177 ms
7,104 KB
testcase_34 AC 67 ms
17,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int dp[100005];
vector<int>ki[100005];

int dfs(int n,int p) {
    int res = 0;
    for(int i = 0; i < ki[n].size(); i++) {
        if(ki[n][i] == p) {
            continue;
        }
        res += dfs(ki[n][i],n);
    }
    res++;
    return dp[n] = res;
}

int main() {
    int N,Q;
    cin >> N >> Q;
    for(int i = 0; i < N-1; i++) {
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        ki[a].push_back(b);
        ki[b].push_back(a);
    }
    dfs(0,-1);
    long long sum = 0;
    for(int i = 0; i < Q; i++) {
        int p,x;
        cin >> p >> x;
        p--;
        sum += (long long)(x)*dp[p];
        cout << sum << endl;
    }
}
0