結果

問題 No.1637 Easy Tree Query
ユーザー SSRSSSRS
提出日時 2021-08-06 21:22:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 303 ms / 2,000 ms
コード長 859 bytes
コンパイル時間 2,408 ms
コンパイル使用メモリ 176,524 KB
実行使用メモリ 15,488 KB
最終ジャッジ日時 2023-10-17 04:36:12
合計ジャッジ時間 10,459 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 276 ms
14,160 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 71 ms
8,712 KB
testcase_05 AC 224 ms
7,216 KB
testcase_06 AC 149 ms
6,220 KB
testcase_07 AC 76 ms
4,348 KB
testcase_08 AC 56 ms
7,712 KB
testcase_09 AC 183 ms
11,296 KB
testcase_10 AC 102 ms
5,740 KB
testcase_11 AC 253 ms
12,248 KB
testcase_12 AC 245 ms
10,044 KB
testcase_13 AC 39 ms
5,780 KB
testcase_14 AC 122 ms
11,444 KB
testcase_15 AC 237 ms
12,588 KB
testcase_16 AC 179 ms
13,644 KB
testcase_17 AC 68 ms
5,776 KB
testcase_18 AC 207 ms
7,216 KB
testcase_19 AC 207 ms
8,068 KB
testcase_20 AC 257 ms
9,888 KB
testcase_21 AC 131 ms
8,848 KB
testcase_22 AC 277 ms
13,796 KB
testcase_23 AC 66 ms
6,424 KB
testcase_24 AC 106 ms
8,868 KB
testcase_25 AC 229 ms
6,952 KB
testcase_26 AC 263 ms
10,640 KB
testcase_27 AC 303 ms
13,456 KB
testcase_28 AC 163 ms
6,688 KB
testcase_29 AC 204 ms
9,624 KB
testcase_30 AC 71 ms
9,568 KB
testcase_31 AC 176 ms
4,348 KB
testcase_32 AC 104 ms
7,036 KB
testcase_33 AC 212 ms
5,708 KB
testcase_34 AC 81 ms
15,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, Q;
  cin >> N >> Q;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++){
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].push_back(b);
    E[b].push_back(a);
  }
  vector<int> pr(N, -1);
  vector<vector<int>> ch(N);
  queue<int> q;
  q.push(0);
  vector<int> bfs;
  while (!q.empty()){
    int v = q.front();
    q.pop();
    bfs.push_back(v);
    for (int w : E[v]){
      if (w != pr[v]){
        pr[w] = v;
        ch[v].push_back(w);
        q.push(w);
      }
    }
  }
  reverse(bfs.begin(), bfs.end());
  vector<int> dp(N, 1);
  for (int v : bfs){
    for (int w : ch[v]){
      dp[v] += dp[w];
    }
  }
  long long ans = 0;
  for (int i = 0; i < Q; i++){
    int p, x;
    cin >> p >> x;
    p--;
    ans += (long long) dp[p] * x;
    cout << ans << endl;
  }
}
0