結果

問題 No.1637 Easy Tree Query
ユーザー simansiman
提出日時 2021-11-26 15:57:40
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 859 bytes
コンパイル時間 6,167 ms
コンパイル使用メモリ 104,448 KB
実行使用メモリ 14,476 KB
最終ジャッジ日時 2023-09-11 23:04:27
合計ジャッジ時間 14,766 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,836 KB
testcase_01 AC 4 ms
5,784 KB
testcase_02 AC 259 ms
9,816 KB
testcase_03 AC 3 ms
5,820 KB
testcase_04 AC 59 ms
8,272 KB
testcase_05 AC 220 ms
7,656 KB
testcase_06 AC 143 ms
6,924 KB
testcase_07 AC 77 ms
5,900 KB
testcase_08 AC 49 ms
7,440 KB
testcase_09 AC 166 ms
8,804 KB
testcase_10 AC 99 ms
6,716 KB
testcase_11 AC 231 ms
9,128 KB
testcase_12 AC 232 ms
8,444 KB
testcase_13 AC 37 ms
6,732 KB
testcase_14 AC 103 ms
8,924 KB
testcase_15 AC 215 ms
9,324 KB
testcase_16 AC 158 ms
9,792 KB
testcase_17 AC 65 ms
6,744 KB
testcase_18 AC 205 ms
7,432 KB
testcase_19 AC 195 ms
7,664 KB
testcase_20 AC 250 ms
8,632 KB
testcase_21 AC 118 ms
8,072 KB
testcase_22 AC 249 ms
9,816 KB
testcase_23 AC 64 ms
7,048 KB
testcase_24 AC 95 ms
7,952 KB
testcase_25 AC 222 ms
7,196 KB
testcase_26 AC 257 ms
8,552 KB
testcase_27 AC 280 ms
9,556 KB
testcase_28 AC 157 ms
7,100 KB
testcase_29 AC 195 ms
8,208 KB
testcase_30 AC 57 ms
8,148 KB
testcase_31 AC 179 ms
5,844 KB
testcase_32 AC 99 ms
7,216 KB
testcase_33 AC 212 ms
6,812 KB
testcase_34 AC 72 ms
14,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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