結果

問題 No.1637 Easy Tree Query
ユーザー trineutron
提出日時 2021-08-06 21:27:12
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 243 ms / 2,000 ms
コード長 861 bytes
コンパイル時間 2,949 ms
コンパイル使用メモリ 250,396 KB
実行使用メモリ 17,408 KB
最終ジャッジ日時 2024-09-17 03:36:51
合計ジャッジ時間 9,527 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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