結果

問題 No.1637 Easy Tree Query
ユーザー hanyuhanyu
提出日時 2021-08-08 20:43:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 719 bytes
コンパイル時間 2,290 ms
コンパイル使用メモリ 206,516 KB
実行使用メモリ 18,908 KB
最終ジャッジ日時 2023-10-19 21:21:49
合計ジャッジ時間 10,072 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 260 ms
9,668 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 66 ms
6,500 KB
testcase_05 AC 222 ms
5,708 KB
testcase_06 AC 144 ms
5,180 KB
testcase_07 AC 75 ms
4,348 KB
testcase_08 AC 52 ms
5,972 KB
testcase_09 AC 173 ms
8,084 KB
testcase_10 AC 99 ms
4,688 KB
testcase_11 AC 242 ms
8,612 KB
testcase_12 AC 233 ms
7,556 KB
testcase_13 AC 37 ms
4,916 KB
testcase_14 AC 111 ms
8,348 KB
testcase_15 AC 222 ms
8,876 KB
testcase_16 AC 170 ms
9,404 KB
testcase_17 AC 66 ms
4,916 KB
testcase_18 AC 202 ms
5,708 KB
testcase_19 AC 198 ms
6,236 KB
testcase_20 AC 247 ms
7,292 KB
testcase_21 AC 123 ms
6,764 KB
testcase_22 AC 258 ms
9,668 KB
testcase_23 AC 63 ms
5,180 KB
testcase_24 AC 98 ms
6,764 KB
testcase_25 AC 219 ms
5,444 KB
testcase_26 AC 254 ms
7,556 KB
testcase_27 AC 292 ms
9,404 KB
testcase_28 AC 159 ms
5,444 KB
testcase_29 AC 194 ms
7,028 KB
testcase_30 AC 62 ms
7,028 KB
testcase_31 AC 175 ms
4,348 KB
testcase_32 AC 101 ms
5,444 KB
testcase_33 AC 207 ms
4,720 KB
testcase_34 AC 77 ms
18,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;

void dfs(vector<vector<int>> &G, vector<ll> &dp, int v) {
  ll now = 1;
  dp[v] = 1;
  for (int i = 0; i < G[v].size(); i++) {
    if (dp[G[v][i]] == -1) {
      dfs(G, dp, G[v][i]);
      now += dp[G[v][i]];
    }
  }
  dp[v] = now;
  return;
}

int main() {
  int n, q;
  cin >> n >> q;

  vector<vector<int>> G(n);
  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);
  }

  vector<ll> dp(n, -1);
  dp[0] = n;
  dfs(G, dp, 0);

  ll ans = 0;

  while (q--) {
    int p;
    ll x;
    cin >> p >> x;
    p--;
    ans += x * dp[p];
    cout << ans << '\n';
  }
}
0