結果

問題 No.1637 Easy Tree Query
ユーザー outlineoutline
提出日時 2021-08-06 21:33:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 1,884 bytes
コンパイル時間 2,897 ms
コンパイル使用メモリ 139,756 KB
実行使用メモリ 12,968 KB
最終ジャッジ日時 2023-10-17 05:02:06
合計ジャッジ時間 5,221 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 58 ms
9,008 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 24 ms
6,368 KB
testcase_05 AC 42 ms
5,476 KB
testcase_06 AC 28 ms
5,048 KB
testcase_07 AC 12 ms
4,348 KB
testcase_08 AC 19 ms
5,668 KB
testcase_09 AC 44 ms
7,688 KB
testcase_10 AC 20 ms
4,720 KB
testcase_11 AC 59 ms
8,216 KB
testcase_12 AC 51 ms
7,160 KB
testcase_13 AC 12 ms
4,796 KB
testcase_14 AC 37 ms
7,952 KB
testcase_15 AC 54 ms
8,480 KB
testcase_16 AC 51 ms
9,008 KB
testcase_17 AC 16 ms
4,792 KB
testcase_18 AC 38 ms
5,476 KB
testcase_19 AC 40 ms
5,892 KB
testcase_20 AC 52 ms
6,904 KB
testcase_21 AC 33 ms
6,368 KB
testcase_22 AC 65 ms
9,008 KB
testcase_23 AC 18 ms
5,108 KB
testcase_24 AC 29 ms
6,372 KB
testcase_25 AC 41 ms
5,312 KB
testcase_26 AC 56 ms
7,208 KB
testcase_27 AC 69 ms
8,744 KB
testcase_28 AC 32 ms
5,304 KB
testcase_29 AC 44 ms
6,896 KB
testcase_30 AC 28 ms
6,700 KB
testcase_31 AC 25 ms
4,348 KB
testcase_32 AC 25 ms
5,380 KB
testcase_33 AC 37 ms
4,752 KB
testcase_34 AC 35 ms
12,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, Q;
    cin >> N >> Q;
    vector<vector<int>> g(N);
    for(int i = 1; i < N; ++i){
        int a, b;
        cin >> a >> b;
        --a, --b;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    
    vector<int> subtree_size(N, 1);
    auto dfs = [&](auto&& self, int from = 0, int par = -1) -> int {
        for(int to : g[from]){
            if(to != par)   subtree_size[from] += self(self, to, from);
        }
        return subtree_size[from];
    };
    dfs(dfs);
    
    ll ans = 0;
    for(int q = 0; q < Q; ++q){
        int p, x;
        cin >> p >> x;
        ans += 1LL * x * subtree_size[p - 1];
        cout << ans << '\n';
    }

    return 0;
}
0