結果

問題 No.1637 Easy Tree Query
ユーザー erbowlerbowl
提出日時 2022-09-01 22:43:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 295 ms / 2,000 ms
コード長 796 bytes
コンパイル時間 2,306 ms
コンパイル使用メモリ 200,344 KB
実行使用メモリ 18,816 KB
最終ジャッジ日時 2024-04-26 22:43:03
合計ジャッジ時間 11,285 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 275 ms
10,368 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 68 ms
6,912 KB
testcase_05 AC 231 ms
5,888 KB
testcase_06 AC 150 ms
5,376 KB
testcase_07 AC 77 ms
5,376 KB
testcase_08 AC 51 ms
6,144 KB
testcase_09 AC 177 ms
8,448 KB
testcase_10 AC 99 ms
5,376 KB
testcase_11 AC 244 ms
9,088 KB
testcase_12 AC 240 ms
7,936 KB
testcase_13 AC 37 ms
5,376 KB
testcase_14 AC 109 ms
8,448 KB
testcase_15 AC 224 ms
9,216 KB
testcase_16 AC 171 ms
9,856 KB
testcase_17 AC 67 ms
5,376 KB
testcase_18 AC 215 ms
5,760 KB
testcase_19 AC 208 ms
6,528 KB
testcase_20 AC 261 ms
7,680 KB
testcase_21 AC 127 ms
6,784 KB
testcase_22 AC 265 ms
9,984 KB
testcase_23 AC 64 ms
5,376 KB
testcase_24 AC 101 ms
6,912 KB
testcase_25 AC 230 ms
5,632 KB
testcase_26 AC 260 ms
8,064 KB
testcase_27 AC 295 ms
9,856 KB
testcase_28 AC 167 ms
5,376 KB
testcase_29 AC 197 ms
7,424 KB
testcase_30 AC 65 ms
7,424 KB
testcase_31 AC 181 ms
5,376 KB
testcase_32 AC 100 ms
5,888 KB
testcase_33 AC 211 ms
5,376 KB
testcase_34 AC 78 ms
18,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main(){
    ll n,q;
    std::cin >> n>>q;
    vector<vector<ll>> edges(n);
    for (int i = 0; i < n-1; i++) {
        ll a,b;
        std::cin >> a>>b;
        a--;b--;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    vector<ll> size(n);
    function<ll(ll,ll)> dfs = [&](ll now, ll par){
        ll sum = 1;
        for (auto e : edges[now]) {
            if(e==par)continue;
            sum += dfs(e, now);
        }
        return size[now] = sum;
    };
    dfs(0,-1);
    ll ans = 0;
    for (int i = 0; i < q; i++) {
        ll p,x;
        std::cin >> p>>x;
        p--;
        ans += x*size[p];
        std::cout << ans << std::endl;
    }
}
0