結果

問題 No.1637 Easy Tree Query
ユーザー sk461sk461
提出日時 2021-08-07 02:18:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 750 bytes
コンパイル時間 709 ms
コンパイル使用メモリ 76,872 KB
実行使用メモリ 15,456 KB
最終ジャッジ日時 2023-10-17 07:48:12
合計ジャッジ時間 8,676 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,004 KB
testcase_01 AC 4 ms
6,004 KB
testcase_02 AC 259 ms
9,128 KB
testcase_03 AC 4 ms
6,000 KB
testcase_04 AC 65 ms
7,800 KB
testcase_05 AC 220 ms
7,272 KB
testcase_06 AC 143 ms
7,008 KB
testcase_07 AC 78 ms
6,216 KB
testcase_08 AC 52 ms
7,304 KB
testcase_09 AC 169 ms
8,592 KB
testcase_10 AC 99 ms
6,744 KB
testcase_11 AC 239 ms
8,856 KB
testcase_12 AC 234 ms
8,080 KB
testcase_13 AC 38 ms
6,748 KB
testcase_14 AC 111 ms
8,592 KB
testcase_15 AC 221 ms
8,856 KB
testcase_16 AC 167 ms
9,120 KB
testcase_17 AC 66 ms
6,744 KB
testcase_18 AC 203 ms
7,272 KB
testcase_19 AC 200 ms
7,536 KB
testcase_20 AC 249 ms
8,064 KB
testcase_21 AC 122 ms
7,800 KB
testcase_22 AC 256 ms
9,180 KB
testcase_23 AC 64 ms
7,008 KB
testcase_24 AC 99 ms
7,800 KB
testcase_25 AC 223 ms
7,272 KB
testcase_26 AC 248 ms
8,328 KB
testcase_27 AC 283 ms
9,120 KB
testcase_28 AC 163 ms
7,020 KB
testcase_29 AC 193 ms
8,064 KB
testcase_30 AC 61 ms
8,064 KB
testcase_31 AC 174 ms
6,012 KB
testcase_32 AC 99 ms
7,272 KB
testcase_33 AC 207 ms
6,744 KB
testcase_34 AC 76 ms
15,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
vector<int> G[100100];
vector<int> cnt(100100, 1);
vector<bool> check(100100, 0);
long dfs(int x) {
    check[x] = 1;
    for (int i = 0; i < G[x].size(); i++) {
        if (!check[G[x][i]]) {
            cnt[x] += dfs(G[x][i]);
        }
    }
    return cnt[x];
}
int main() {
    int n, q;
    cin >> n >> q;
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    dfs(0);
    long ans = 0;
    for (int i = 0; i < q; i++) {
        int p;
        long x;
        cin >> p >> x;
        p--;
        ans += x * cnt[p];
        cout << ans << endl;
    }
    return 0;
}
0