結果

問題 No.1637 Easy Tree Query
コンテスト
ユーザー siman
提出日時 2021-11-26 15:57:40
言語 C++17(clang)
(clang++ 22.1.2 + boost 1.89.0)
コンパイル:
clang++ -O2 -lm -std=c++1z -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 859 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,696 ms
コンパイル使用メモリ 146,816 KB
実行使用メモリ 16,128 KB
最終ジャッジ日時 2026-03-19 00:21:07
合計ジャッジ時間 7,833 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_N = 100010;
ll C[MAX_N];
vector<int> E[MAX_N];

ll dfs(int v, vector<bool> &visited) {
  ll cnt = 1;
  visited[v] = true;

  for (int u : E[v]) {
    if (visited[u]) continue;

    cnt += dfs(u, visited);
  }

  return C[v] = cnt;
}

int main() {
  int N, Q;
  cin >> N >> Q;

  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;

    E[a].push_back(b);
    E[b].push_back(a);
  }

  vector<bool> visited(N + 1, false);
  dfs(1, visited);
  ll ans = 0;

  for (int i = 0; i < Q; ++i) {
    int p, x;
    cin >> p >> x;

    ans += C[p] * x;
    cout << ans << endl;
  }

  return 0;
}
0