結果

問題 No.1637 Easy Tree Query
ユーザー bit_kyoprobit_kyopro
提出日時 2021-08-06 21:34:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 272 ms / 2,000 ms
コード長 1,363 bytes
コンパイル時間 2,005 ms
コンパイル使用メモリ 174,680 KB
実行使用メモリ 15,728 KB
最終ジャッジ日時 2023-10-17 05:02:16
合計ジャッジ時間 9,276 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 260 ms
10,448 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 60 ms
6,752 KB
testcase_05 AC 219 ms
5,960 KB
testcase_06 AC 139 ms
5,168 KB
testcase_07 AC 75 ms
4,348 KB
testcase_08 AC 50 ms
5,960 KB
testcase_09 AC 168 ms
8,600 KB
testcase_10 AC 96 ms
4,904 KB
testcase_11 AC 229 ms
9,128 KB
testcase_12 AC 225 ms
7,808 KB
testcase_13 AC 35 ms
4,904 KB
testcase_14 AC 100 ms
8,600 KB
testcase_15 AC 208 ms
9,392 KB
testcase_16 AC 153 ms
9,920 KB
testcase_17 AC 64 ms
4,904 KB
testcase_18 AC 199 ms
5,960 KB
testcase_19 AC 195 ms
6,488 KB
testcase_20 AC 240 ms
7,544 KB
testcase_21 AC 121 ms
7,016 KB
testcase_22 AC 247 ms
10,184 KB
testcase_23 AC 61 ms
5,180 KB
testcase_24 AC 97 ms
7,016 KB
testcase_25 AC 222 ms
5,696 KB
testcase_26 AC 245 ms
8,072 KB
testcase_27 AC 272 ms
9,656 KB
testcase_28 AC 157 ms
5,432 KB
testcase_29 AC 192 ms
7,280 KB
testcase_30 AC 56 ms
7,280 KB
testcase_31 AC 173 ms
4,348 KB
testcase_32 AC 97 ms
5,696 KB
testcase_33 AC 206 ms
4,904 KB
testcase_34 AC 76 ms
15,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const long long MOD = 1000000007;
const long double PI = 3.14159265358979;
const long long INF = 1LL<<60;
template <typename T> bool chmax(T &a, const T& b){if(a < b){a = b;return true;}return false;}
template <typename T> bool chmin(T &a, const T& b){if(a > b){a = b;return true;}return false;}
bool is_prime(long long N){if(N==1) return false;for(long long i=2; i*i <= N; i++){if(N%i==0)return false;}return true;}
long long gcd(long long a, long long b){if (b == 0) return a;else return gcd(b, a % b);}
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define int long long

vector<vector<int>> T;
vector<int> siz;

int rec(int now, int par){
    if(siz[now]!=-1) return siz[now];
    int res = 1;
    for(auto child: T[now]){
        if(par==child) continue;
        res += rec(child, now);
    }

    return siz[now] = res;
}

int32_t main(){
    int N, Q; cin >> N >> Q;
    T.resize(N);
    siz.resize(N,-1);
    for(int i=0; i<N-1; i++){
        int a,b; cin >> a >> b;
        a--; b--;
        T[a].push_back(b);
        T[b].push_back(a);
    }
    rec(0,-1);
    int ans = 0;

    for(int q=0; q<Q; q++){
        int p,x; cin >> p >> x;
        p--;
        ans += siz[p]*x;
        cout << ans << endl;
    }
}
0