結果

問題 No.1637 Easy Tree Query
ユーザー _umbrella_kasa_umbrella_kasa
提出日時 2021-08-08 14:31:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,114 bytes
コンパイル時間 2,819 ms
コンパイル使用メモリ 214,292 KB
実行使用メモリ 12,860 KB
最終ジャッジ日時 2023-10-19 14:41:28
合計ジャッジ時間 11,571 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
4,348 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 136 ms
12,680 KB
権限があれば一括ダウンロードができます

ソースコード

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<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[i]) {
        if (!(see[next]) && depth[next]>x) {
          see[next]=true;
          A.push(next);
          P[i]++;
        }
      }
    }
  }
  int ans=0;
  for (int i=0; i<Q; i++) {
    int p,x;
    cin >> p >> x;
    ans+=P[p]*x;
    cout << ans << endl;
  }
}
0