結果

問題 No.1637 Easy Tree Query
ユーザー sash0sash0
提出日時 2021-08-22 23:16:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 2,398 bytes
コンパイル時間 3,434 ms
コンパイル使用メモリ 221,316 KB
実行使用メモリ 18,432 KB
最終ジャッジ日時 2024-04-24 14:30:10
合計ジャッジ時間 8,603 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 62 ms
9,216 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 27 ms
6,272 KB
testcase_05 AC 47 ms
5,376 KB
testcase_06 AC 31 ms
5,376 KB
testcase_07 AC 14 ms
5,376 KB
testcase_08 AC 21 ms
5,504 KB
testcase_09 AC 51 ms
7,808 KB
testcase_10 AC 22 ms
5,376 KB
testcase_11 AC 67 ms
8,320 KB
testcase_12 AC 59 ms
7,040 KB
testcase_13 AC 13 ms
5,376 KB
testcase_14 AC 43 ms
7,936 KB
testcase_15 AC 69 ms
8,448 KB
testcase_16 AC 63 ms
9,088 KB
testcase_17 AC 18 ms
5,376 KB
testcase_18 AC 43 ms
5,376 KB
testcase_19 AC 45 ms
5,888 KB
testcase_20 AC 59 ms
7,040 KB
testcase_21 AC 36 ms
6,400 KB
testcase_22 AC 80 ms
9,088 KB
testcase_23 AC 19 ms
5,376 KB
testcase_24 AC 33 ms
6,528 KB
testcase_25 AC 45 ms
5,376 KB
testcase_26 AC 63 ms
7,296 KB
testcase_27 AC 79 ms
8,832 KB
testcase_28 AC 34 ms
5,376 KB
testcase_29 AC 51 ms
6,912 KB
testcase_30 AC 29 ms
6,784 KB
testcase_31 AC 27 ms
5,376 KB
testcase_32 AC 27 ms
5,376 KB
testcase_33 AC 39 ms
5,376 KB
testcase_34 AC 40 ms
18,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// clang-format off
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcountll
#define ffs __builtin_ffsll
#define ctz __builtin_ctzll
#define clz __builtin_clz
#define clzll __builtin_clzll
#define all(a) (a).begin(), (a).end()

using namespace std;
using namespace __gnu_pbds;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
template <typename T> bool getf(T flag, size_t i) { return (flag>>i) & 1; }
template <typename T> T setf(T flag, size_t i) { return flag | (T(1)<<i); }
template <typename T> T unsetf(T flag, size_t i) { return flag & ~(T(1)<<i); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

int main() {
    fastio();

    int n, q;
    cin >> n >> q;
    vec<vi> g(n);
    forn(i, n - 1) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    vi cnt(n);

    function<int(int, int)> dfs = [&](int u, int p) {
        cnt[u] = 1;

        for (auto v : g[u]) {
            if (v != p) cnt[u] += dfs(v, u);
        }

        return cnt[u];
    };

    dfs(0, -1);

    ll ans = 0;
    while (q--) {
        int p, x;
        cin >> p >> x;
        ans += 1LL * cnt[p - 1] * x;
        cout << ans << "\n";
    }

    return 0;
}
0