結果

問題 No.1637 Easy Tree Query
ユーザー kappybarkappybar
提出日時 2021-08-08 20:29:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 1,094 bytes
コンパイル時間 2,170 ms
コンパイル使用メモリ 172,804 KB
実行使用メモリ 12,524 KB
最終ジャッジ日時 2023-10-19 21:07:09
合計ジャッジ時間 10,126 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 259 ms
9,092 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 62 ms
6,452 KB
testcase_05 AC 217 ms
5,480 KB
testcase_06 AC 141 ms
4,868 KB
testcase_07 AC 76 ms
4,348 KB
testcase_08 AC 50 ms
5,672 KB
testcase_09 AC 168 ms
7,772 KB
testcase_10 AC 96 ms
4,604 KB
testcase_11 AC 228 ms
8,300 KB
testcase_12 AC 229 ms
7,244 KB
testcase_13 AC 36 ms
4,604 KB
testcase_14 AC 106 ms
7,772 KB
testcase_15 AC 213 ms
8,564 KB
testcase_16 AC 160 ms
9,092 KB
testcase_17 AC 64 ms
4,604 KB
testcase_18 AC 199 ms
5,480 KB
testcase_19 AC 193 ms
5,924 KB
testcase_20 AC 249 ms
6,980 KB
testcase_21 AC 123 ms
6,452 KB
testcase_22 AC 255 ms
9,092 KB
testcase_23 AC 60 ms
4,868 KB
testcase_24 AC 95 ms
6,452 KB
testcase_25 AC 220 ms
5,396 KB
testcase_26 AC 249 ms
7,244 KB
testcase_27 AC 279 ms
8,828 KB
testcase_28 AC 163 ms
5,132 KB
testcase_29 AC 194 ms
6,736 KB
testcase_30 AC 63 ms
6,716 KB
testcase_31 AC 175 ms
4,348 KB
testcase_32 AC 99 ms
5,396 KB
testcase_33 AC 211 ms
4,604 KB
testcase_34 AC 73 ms
12,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y))
#define chmax(x,y) x = max((x),(y))
#define popcount(x) __builtin_popcount(x)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e17;
const int MOD = 1000000007;
//const int MOD = 998244353;
const double PI = 3.14159265358979323846;

int main(){
    int n,q;
    cin >> n >> q;
    vector<vector<int>> tree(n);
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        --a;--b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }

    vector<int> sz(n,0);
    auto dfs = [&](auto&& dfs,int i,int p) -> int {
        sz[i] = 1;
        for(int j:tree[i]){
            if(j == p) continue;
            sz[i] += dfs(dfs,j,i);
        }
        return sz[i];
    };
    dfs(dfs,0,-1);

    ll ans = 0;
    for(int i=0;i<q;i++){
        ll p;
        ll x;
        cin >> p >> x;
        --p;
        ans += x * sz[p];
        cout << ans << endl;
    }

    return 0;
}
0