結果

問題 No.1637 Easy Tree Query
ユーザー _umbrella_kasa_umbrella_kasa
提出日時 2021-08-08 16:15:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,136 bytes
コンパイル時間 2,860 ms
コンパイル使用メモリ 217,636 KB
実行使用メモリ 17,692 KB
最終ジャッジ日時 2023-10-19 16:41:12
合計ジャッジ時間 20,188 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 602 ms
13,108 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 259 ms
8,424 KB
testcase_05 AC 344 ms
7,104 KB
testcase_06 AC 222 ms
6,048 KB
testcase_07 AC 80 ms
4,348 KB
testcase_08 AC 198 ms
7,368 KB
testcase_09 AC 486 ms
10,996 KB
testcase_10 AC 153 ms
5,528 KB
testcase_11 AC 613 ms
11,788 KB
testcase_12 AC 504 ms
9,676 KB
testcase_13 AC 102 ms
5,784 KB
testcase_14 AC 450 ms
10,996 KB
testcase_15 AC 621 ms
12,156 KB
testcase_16 AC 669 ms
12,844 KB
testcase_17 AC 123 ms
5,784 KB
testcase_18 AC 324 ms
7,104 KB
testcase_19 AC 359 ms
7,896 KB
testcase_20 AC 478 ms
9,480 KB
testcase_21 AC 326 ms
8,688 KB
testcase_22 AC 762 ms
13,108 KB
testcase_23 AC 140 ms
6,312 KB
testcase_24 AC 283 ms
8,688 KB
testcase_25 AC 357 ms
6,840 KB
testcase_26 AC 553 ms
10,204 KB
testcase_27 AC 709 ms
12,844 KB
testcase_28 AC 253 ms
6,576 KB
testcase_29 AC 420 ms
9,216 KB
testcase_30 AC 274 ms
9,216 KB
testcase_31 AC 175 ms
4,348 KB
testcase_32 AC 211 ms
6,840 KB
testcase_33 AC 277 ms
5,580 KB
testcase_34 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
int main() {
  int N,Q;
  cin >> N >> Q;
  Graph G(N+1);
  for (int i=0; i<N-1; i++) {
    int a,b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int>depth(N+1,0);
  stack<int>S;
  S.push(1);
  vector<bool>seen(N+1,false);
  seen[1]=true;
  while(S.size()!=0) {
    int x=S.top();
    S.pop();
    for (auto next: G[x]) {
      if (seen[next]) {
        continue;
      }
      else {
        depth[next]=depth[x]+1;
        seen[next]=true;
        S.push(next);
      }
    }
  }
  vector<long int>P(N+1,1);
  P[1]=N;
  for (int i=2; i<=N; i++) {
    stack<int>A;
    A.push(i);
    vector<bool>see(N+1,false);
    see[i]=true;
    while (A.size()!=0) {
      int x=A.top();
      A.pop();
      for (auto next: G[x]) {
        if (!(see[next]) && depth[next]>depth[x]) {
          see[next]=true;
          A.push(next);
          P[i]++;
        }
      }
    }
  }
  long int ans=0;
  for (int i=0; i<Q; i++) {
    long int p,x;
    cin >> p >> x;
    ans+=P[p]*x;
    cout << ans << endl;
  }
}
0