結果

問題 No.1637 Easy Tree Query
ユーザー bit_kyopro
提出日時 2021-08-06 21:34:59
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 285 ms / 2,000 ms
コード長 1,363 bytes
コンパイル時間 1,899 ms
コンパイル使用メモリ 173,696 KB
実行使用メモリ 15,744 KB
最終ジャッジ日時 2024-09-17 03:45:02
合計ジャッジ時間 9,176 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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