結果
| 問題 | 
                            No.1637 Easy Tree Query
                             | 
                    
| コンテスト | |
| ユーザー | 
                             _umbrella_kasa
                         | 
                    
| 提出日時 | 2021-08-08 14:31:24 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,114 bytes | 
| コンパイル時間 | 3,821 ms | 
| コンパイル使用メモリ | 213,368 KB | 
| 実行使用メモリ | 12,712 KB | 
| 最終ジャッジ日時 | 2024-09-19 10:49:44 | 
| 合計ジャッジ時間 | 11,306 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 WA * 1 | 
| other | AC * 2 WA * 31 | 
ソースコード
#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;
  }
}
            
            
            
        
            
_umbrella_kasa