結果

問題 No.1637 Easy Tree Query
ユーザー carrot46carrot46
提出日時 2021-08-06 21:25:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 1,544 bytes
コンパイル時間 4,392 ms
コンパイル使用メモリ 209,036 KB
実行使用メモリ 13,224 KB
最終ジャッジ日時 2023-10-17 04:45:10
合計ジャッジ時間 8,418 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 179 ms
10,320 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 38 ms
6,624 KB
testcase_05 AC 160 ms
5,832 KB
testcase_06 AC 102 ms
5,068 KB
testcase_07 AC 58 ms
4,348 KB
testcase_08 AC 30 ms
6,096 KB
testcase_09 AC 114 ms
8,472 KB
testcase_10 AC 71 ms
4,776 KB
testcase_11 AC 161 ms
9,000 KB
testcase_12 AC 162 ms
7,680 KB
testcase_13 AC 24 ms
4,812 KB
testcase_14 AC 64 ms
8,472 KB
testcase_15 AC 146 ms
9,264 KB
testcase_16 AC 99 ms
9,792 KB
testcase_17 AC 45 ms
4,808 KB
testcase_18 AC 143 ms
5,832 KB
testcase_19 AC 138 ms
6,360 KB
testcase_20 AC 169 ms
7,416 KB
testcase_21 AC 81 ms
6,888 KB
testcase_22 AC 170 ms
10,056 KB
testcase_23 AC 42 ms
5,128 KB
testcase_24 AC 62 ms
6,888 KB
testcase_25 AC 159 ms
5,568 KB
testcase_26 AC 174 ms
7,944 KB
testcase_27 AC 195 ms
9,792 KB
testcase_28 AC 114 ms
5,324 KB
testcase_29 AC 135 ms
7,416 KB
testcase_30 AC 34 ms
7,152 KB
testcase_31 AC 134 ms
4,348 KB
testcase_32 AC 68 ms
5,568 KB
testcase_33 AC 156 ms
4,776 KB
testcase_34 AC 35 ms
13,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/modint>
using namespace std;
using namespace atcoder;
//#pragma GCC optimize("Ofast")
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
using tp = tuple<ll, ll, ll>;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T b){
    if(a < b) {a = b; return true;}
    else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
    if(!v.empty()){
        for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
        std::cout<<v.back();
    }
    if(endline) std::cout<<std::endl;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>N>>Q;
    mat G(N);
    vec subsz(N, 0);
    rep(i, N - 1){
        int a, b;
        cin>>a>>b;
        --a; --b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    auto dfs = [&](auto &&self, int p, int v) -> int{
        subsz[v] = 1;
        for(int to : G[v]) if(to != p) subsz[v] += self(self, v, to);
        return subsz[v];
    };
    dfs(dfs, -1, 0);
    ll res{};
    while(Q--){
        ll p, x;
        cin>>p>>x;
        res += subsz[p - 1] * x;
        cout<<res<<endl;
    }
}
0