結果

問題 No.1637 Easy Tree Query
ユーザー trineutrontrineutron
提出日時 2021-08-06 21:27:12
言語 C++23(draft)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 861 bytes
コンパイル時間 3,531 ms
コンパイル使用メモリ 251,428 KB
実行使用メモリ 17,300 KB
最終ジャッジ日時 2023-10-17 04:53:19
合計ジャッジ時間 11,083 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 261 ms
9,644 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 62 ms
6,476 KB
testcase_05 AC 218 ms
5,684 KB
testcase_06 AC 143 ms
5,156 KB
testcase_07 AC 75 ms
4,348 KB
testcase_08 AC 48 ms
5,948 KB
testcase_09 AC 164 ms
8,060 KB
testcase_10 AC 98 ms
4,664 KB
testcase_11 AC 230 ms
8,588 KB
testcase_12 AC 227 ms
7,532 KB
testcase_13 AC 35 ms
4,892 KB
testcase_14 AC 105 ms
8,324 KB
testcase_15 AC 212 ms
8,852 KB
testcase_16 AC 158 ms
9,380 KB
testcase_17 AC 63 ms
4,892 KB
testcase_18 AC 200 ms
5,684 KB
testcase_19 AC 195 ms
6,212 KB
testcase_20 AC 241 ms
7,268 KB
testcase_21 AC 119 ms
6,740 KB
testcase_22 AC 254 ms
9,644 KB
testcase_23 AC 61 ms
5,156 KB
testcase_24 AC 97 ms
6,740 KB
testcase_25 AC 223 ms
5,420 KB
testcase_26 AC 249 ms
7,532 KB
testcase_27 AC 281 ms
9,380 KB
testcase_28 AC 157 ms
5,420 KB
testcase_29 AC 188 ms
7,004 KB
testcase_30 AC 57 ms
7,004 KB
testcase_31 AC 173 ms
4,348 KB
testcase_32 AC 96 ms
5,420 KB
testcase_33 AC 205 ms
4,696 KB
testcase_34 AC 77 ms
17,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using graph = vector<vector<int>>;

void dfs(const graph &to, vector<int64_t> &children, int v, int p)
{
    children.at(v)++;
    for (auto &&c : to.at(v))
    {
        if (c == p)
        {
            continue;
        }
        dfs(to, children, c, v);
        children.at(v) += children.at(c);
    }
}

int main()
{
    int n, q;
    cin >> n >> q;
    graph to(n);
    for (int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        to.at(a).push_back(b);
        to.at(b).push_back(a);
    }
    vector<int64_t> children(n);
    dfs(to, children, 0, -1);
    int64_t cost = 0;
    for (int i = 0; i < q; i++)
    {
        int p, x;
        cin >> p >> x;
        p--;
        cost += x * children.at(p);
        cout << cost << endl;
    }
    return 0;
}
0