結果

問題 No.1637 Easy Tree Query
ユーザー zezerozezero
提出日時 2021-08-06 22:00:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 247 ms / 2,000 ms
コード長 836 bytes
コンパイル時間 892 ms
コンパイル使用メモリ 102,212 KB
実行使用メモリ 15,512 KB
最終ジャッジ日時 2023-10-17 05:09:51
合計ジャッジ時間 7,550 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 230 ms
9,176 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 50 ms
6,536 KB
testcase_05 AC 195 ms
5,564 KB
testcase_06 AC 124 ms
4,952 KB
testcase_07 AC 69 ms
4,348 KB
testcase_08 AC 42 ms
5,756 KB
testcase_09 AC 143 ms
7,856 KB
testcase_10 AC 86 ms
4,688 KB
testcase_11 AC 194 ms
8,384 KB
testcase_12 AC 200 ms
7,328 KB
testcase_13 AC 31 ms
4,688 KB
testcase_14 AC 85 ms
7,856 KB
testcase_15 AC 178 ms
8,648 KB
testcase_16 AC 135 ms
9,176 KB
testcase_17 AC 56 ms
4,688 KB
testcase_18 AC 177 ms
5,564 KB
testcase_19 AC 175 ms
6,008 KB
testcase_20 AC 210 ms
7,064 KB
testcase_21 AC 102 ms
6,536 KB
testcase_22 AC 221 ms
9,176 KB
testcase_23 AC 54 ms
4,952 KB
testcase_24 AC 85 ms
6,536 KB
testcase_25 AC 198 ms
5,480 KB
testcase_26 AC 214 ms
7,328 KB
testcase_27 AC 247 ms
8,912 KB
testcase_28 AC 137 ms
5,216 KB
testcase_29 AC 165 ms
6,820 KB
testcase_30 AC 46 ms
6,800 KB
testcase_31 AC 159 ms
4,348 KB
testcase_32 AC 85 ms
5,480 KB
testcase_33 AC 190 ms
4,688 KB
testcase_34 AC 70 ms
15,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i=0;i < (int)(n);i++)

vector<vector<int>> g;
vector<int> subsize;

int dfs(int now,int pre){
    int res = 1;
    for (auto nx:g[now]){
        if (nx == pre) continue;
        res += dfs(nx,now);
    }
    return subsize[now] = res;
}

int main(){
    int n,q;
    cin >> n >> q;
    g.resize(n);
    subsize.resize(n);
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        a--;b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    ll ans = 0;
    dfs(0,-1);
    while(q--){
        int p;
        ll x;
        cin >> p >> x;
        ans += subsize[p-1] * x;
        cout << ans << endl;
    }
    return 0;
}
0