結果

問題 No.1637 Easy Tree Query
ユーザー ytsmash
提出日時 2021-08-20 19:30:05
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 315 ms / 2,000 ms
コード長 1,030 bytes
コンパイル時間 2,138 ms
コンパイル使用メモリ 199,412 KB
最終ジャッジ日時 2025-01-23 23:04:06
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *    author:  ytsmash
 *    created: 15.08.2021 10:24:54
 **/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
using P = pair<int, int>;

vector<vector<ll>> d;
vector<ll> cnt;
vector<ll> B;

void f(int i) {
    B[i] = 1;
    for (ll j : d[i]) {
        if (B[j] == 0) {
            f(j);
            cnt[i] += cnt[j];
        }
    }
    return;
}

int main() {
    ll N, Q, a, b, ans = 0, p, x;
    cin >> N >> Q;
    d.resize(N);
    cnt.resize(N);
    B.resize(N);
    for (int i = 0; i < N - 1; i++) {
        cnt[i] = 1;  // 頂点p[i]を根とする部分木に含まれる頂点の数
        cin >> a >> b;
        d[a - 1].push_back(b - 1);
        d[b - 1].push_back(a - 1);
    }
    cnt[N - 1] = 1;
    f(0);
    rep(i, Q) {
        cin >> p >> x;
        ans += cnt[p - 1] * x;
        cout << ans << "\n";
    }
}
0