結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,696 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 601 ms
13,060 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 233 ms
8,376 KB
testcase_05 AC 327 ms
7,056 KB
testcase_06 AC 215 ms
6,000 KB
testcase_07 AC 79 ms
4,348 KB
testcase_08 AC 179 ms
7,320 KB
testcase_09 AC 458 ms
10,948 KB
testcase_10 AC 153 ms
5,480 KB
testcase_11 AC 572 ms
11,740 KB
testcase_12 AC 483 ms
9,628 KB
testcase_13 AC 98 ms
5,736 KB
testcase_14 AC 419 ms
10,948 KB
testcase_15 AC 598 ms
12,108 KB
testcase_16 AC 637 ms
12,796 KB
testcase_17 AC 121 ms
5,736 KB
testcase_18 AC 309 ms
7,056 KB
testcase_19 AC 353 ms
7,848 KB
testcase_20 AC 467 ms
9,432 KB
testcase_21 AC 314 ms
8,640 KB
testcase_22 AC 725 ms
13,060 KB
testcase_23 AC 140 ms
6,264 KB
testcase_24 AC 274 ms
8,640 KB
testcase_25 AC 352 ms
6,792 KB
testcase_26 AC 535 ms
10,156 KB
testcase_27 AC 714 ms
12,796 KB
testcase_28 AC 249 ms
6,528 KB
testcase_29 AC 411 ms
9,168 KB
testcase_30 AC 278 ms
9,168 KB
testcase_31 AC 175 ms
4,348 KB
testcase_32 AC 202 ms
6,792 KB
testcase_33 AC 273 ms
5,532 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