結果

問題 No.1637 Easy Tree Query
ユーザー _umbrella_kasa
提出日時 2021-08-08 16:15:26
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,136 bytes
コンパイル時間 2,768 ms
コンパイル使用メモリ 217,592 KB
実行使用メモリ 20,124 KB
最終ジャッジ日時 2024-09-19 13:01:46
合計ジャッジ時間 16,903 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 32 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

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