結果

問題 No.1637 Easy Tree Query
ユーザー kimiyukikimiyuki
提出日時 2021-08-06 21:48:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 1,551 bytes
コンパイル時間 2,535 ms
コンパイル使用メモリ 209,032 KB
実行使用メモリ 13,776 KB
最終ジャッジ日時 2023-10-17 05:05:35
合計ジャッジ時間 6,613 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 49 ms
11,928 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 22 ms
6,912 KB
testcase_05 AC 36 ms
7,704 KB
testcase_06 AC 26 ms
6,388 KB
testcase_07 AC 11 ms
4,348 KB
testcase_08 AC 17 ms
6,120 KB
testcase_09 AC 38 ms
9,288 KB
testcase_10 AC 19 ms
5,592 KB
testcase_11 AC 48 ms
10,344 KB
testcase_12 AC 45 ms
9,288 KB
testcase_13 AC 11 ms
5,064 KB
testcase_14 AC 34 ms
8,760 KB
testcase_15 AC 47 ms
10,344 KB
testcase_16 AC 40 ms
10,540 KB
testcase_17 AC 15 ms
5,328 KB
testcase_18 AC 34 ms
7,440 KB
testcase_19 AC 35 ms
7,764 KB
testcase_20 AC 46 ms
9,288 KB
testcase_21 AC 29 ms
7,456 KB
testcase_22 AC 56 ms
11,400 KB
testcase_23 AC 16 ms
5,656 KB
testcase_24 AC 27 ms
7,440 KB
testcase_25 AC 37 ms
7,440 KB
testcase_26 AC 47 ms
9,552 KB
testcase_27 AC 56 ms
11,400 KB
testcase_28 AC 31 ms
6,796 KB
testcase_29 AC 38 ms
8,504 KB
testcase_30 AC 25 ms
7,188 KB
testcase_31 AC 22 ms
5,064 KB
testcase_32 AC 21 ms
6,384 KB
testcase_33 AC 33 ms
6,756 KB
testcase_34 AC 30 ms
13,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;

vector<int64_t> solve(int n, int q, const std::vector<int> &a, const std::vector<int> &b, const std::vector<int> &p, const std::vector<int64_t> &x) {
    vector<vector<int>> g(n);
    REP (i, n - 1) {
        g[a[i]].push_back(b[i]);
        g[b[i]].push_back(a[i]);
    }
    vector<int> cnt(n);
    auto go = [&](auto &&go, int x, int parent) -> void {
        cnt[x] += 1;
        for (int y : g[x]) if (y != parent) {
            go(go, y, x);
            cnt[x] += cnt[y];
        }
    };
    go(go, 0, -1);
    int64_t cur = 0;
    vector<int64_t> ans(q);
    REP (i, q) {
        cur += cnt[p[i]] * x[i];
        ans[i] = cur;
    }
    return ans;
}

// generated by oj-template v4.8.0 (https://github.com/online-judge-tools/template-generator)
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N, Q;
    std::cin >> N;
    std::vector<int> a(N - 1), b(N - 1);
    std::cin >> Q;
    std::vector<int> p(Q);
    std::vector<int64_t> x(Q);
    REP (i, N - 1) { std::cin >> a[i] >> b[i]; -- a[i]; -- b[i]; }
    REP (i, Q) { std::cin >> p[i] >> x[i]; -- p[i]; }
    auto c = solve(N, Q, a, b, p, x);
    REP (i, Q) { std::cout << c[i] << '\n'; }
    return 0;
}
0