結果

問題 No.1637 Easy Tree Query
ユーザー _umbrella_kasa_umbrella_kasa
提出日時 2021-08-08 20:24:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,201 bytes
コンパイル時間 3,038 ms
コンパイル使用メモリ 219,452 KB
実行使用メモリ 18,820 KB
最終ジャッジ日時 2023-10-19 21:01:44
合計ジャッジ時間 7,415 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
  vector<int>p(Q);
  vector<long int>x(Q);
  for (int i=0; i<Q; i++) {
      cin >> p[i] >> x[i];
  }
  for (auto i:p) {
    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++) {
      ans+=P[p[i]]*x[i];
      cout << ans << endl;
  }
}
0