結果

問題 No.1637 Easy Tree Query
ユーザー sk461sk461
提出日時 2021-08-07 02:16:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 290 ms / 2,000 ms
コード長 766 bytes
コンパイル時間 756 ms
コンパイル使用メモリ 76,828 KB
実行使用メモリ 15,472 KB
最終ジャッジ日時 2023-10-17 07:46:42
合計ジャッジ時間 8,671 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,036 KB
testcase_01 AC 4 ms
6,036 KB
testcase_02 AC 265 ms
9,160 KB
testcase_03 AC 4 ms
6,032 KB
testcase_04 AC 66 ms
7,816 KB
testcase_05 AC 224 ms
7,288 KB
testcase_06 AC 146 ms
7,024 KB
testcase_07 AC 78 ms
6,232 KB
testcase_08 AC 52 ms
7,336 KB
testcase_09 AC 172 ms
8,608 KB
testcase_10 AC 101 ms
6,760 KB
testcase_11 AC 239 ms
8,872 KB
testcase_12 AC 235 ms
8,112 KB
testcase_13 AC 39 ms
6,780 KB
testcase_14 AC 110 ms
8,608 KB
testcase_15 AC 223 ms
8,872 KB
testcase_16 AC 168 ms
9,148 KB
testcase_17 AC 67 ms
6,776 KB
testcase_18 AC 204 ms
7,288 KB
testcase_19 AC 201 ms
7,552 KB
testcase_20 AC 251 ms
8,080 KB
testcase_21 AC 125 ms
7,816 KB
testcase_22 AC 259 ms
9,212 KB
testcase_23 AC 64 ms
7,024 KB
testcase_24 AC 99 ms
7,816 KB
testcase_25 AC 225 ms
7,288 KB
testcase_26 AC 256 ms
8,344 KB
testcase_27 AC 290 ms
9,136 KB
testcase_28 AC 162 ms
7,052 KB
testcase_29 AC 195 ms
8,080 KB
testcase_30 AC 61 ms
8,080 KB
testcase_31 AC 176 ms
6,044 KB
testcase_32 AC 100 ms
7,288 KB
testcase_33 AC 211 ms
6,760 KB
testcase_34 AC 75 ms
15,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
vector<int> G[100100];
vector<int> cnt(100100, 1);
vector<bool> check(100100, 0);
ll 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);
    ll ans = 0;
    for (int i = 0; i < q; i++) {
        int p;
        ll x;
        cin >> p >> x;
        p--;
        ans += x * cnt[p];
        cout << ans << endl;
    }
    return 0;
}
0