結果

問題 No.1637 Easy Tree Query
ユーザー sk461
提出日時 2021-08-07 02:16:39
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 268 ms / 2,000 ms
コード長 766 bytes
コンパイル時間 834 ms
コンパイル使用メモリ 76,140 KB
最終ジャッジ日時 2025-01-23 16:29:02
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
vector<int> G[100100];
vector<int> cnt(100100, 1);
vector<bool> check(100100, 0);
ll dfs(int x) {
    check[x] = 1;
    for (int i = 0; i < G[x].size(); i++) {
        if (!check[G[x][i]]) {
            cnt[x] += dfs(G[x][i]);
        }
    }
    return cnt[x];
}
int main() {
    int n, q;
    cin >> n >> q;
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    dfs(0);
    ll ans = 0;
    for (int i = 0; i < q; i++) {
        int p;
        ll x;
        cin >> p >> x;
        p--;
        ans += x * cnt[p];
        cout << ans << endl;
    }
    return 0;
}
0