結果

問題 No.1637 Easy Tree Query
ユーザー Nzt3Nzt3
提出日時 2022-10-10 22:32:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 865 bytes
コンパイル時間 2,425 ms
コンパイル使用メモリ 210,072 KB
実行使用メモリ 10,336 KB
最終ジャッジ日時 2023-09-07 01:41:02
合計ジャッジ時間 9,930 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 60 ms
9,784 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 33 ms
6,416 KB
testcase_05 AC 43 ms
5,440 KB
testcase_06 AC 29 ms
4,944 KB
testcase_07 AC 13 ms
4,380 KB
testcase_08 AC 23 ms
5,932 KB
testcase_09 AC 55 ms
7,740 KB
testcase_10 AC 21 ms
4,380 KB
testcase_11 AC 66 ms
8,264 KB
testcase_12 AC 56 ms
7,252 KB
testcase_13 AC 14 ms
4,568 KB
testcase_14 AC 50 ms
8,024 KB
testcase_15 AC 74 ms
8,552 KB
testcase_16 AC 74 ms
9,176 KB
testcase_17 AC 18 ms
4,688 KB
testcase_18 AC 42 ms
5,384 KB
testcase_19 AC 45 ms
5,908 KB
testcase_20 AC 60 ms
7,232 KB
testcase_21 AC 38 ms
6,468 KB
testcase_22 AC 81 ms
9,536 KB
testcase_23 AC 18 ms
4,856 KB
testcase_24 AC 32 ms
6,448 KB
testcase_25 AC 43 ms
5,288 KB
testcase_26 AC 62 ms
7,400 KB
testcase_27 AC 76 ms
9,056 KB
testcase_28 AC 34 ms
5,332 KB
testcase_29 AC 53 ms
6,764 KB
testcase_30 AC 32 ms
6,800 KB
testcase_31 AC 25 ms
4,380 KB
testcase_32 AC 27 ms
5,504 KB
testcase_33 AC 36 ms
4,412 KB
testcase_34 AC 35 ms
10,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N,Q;
  cin>>N>>Q;
  vector<vector<int>>G(N);
  for(int i=0;i<N-1;i++){
    int a,b;
    cin>>a>>b;
    --a,--b;
    G[a].push_back(b),G[b].push_back(a);
  }
  vector<int>chi(N,1),p(N);
  stack<array<int,2>>dfs;
  dfs.push({0,0});
  vector<bool>vst(N);
  vst[0]=true;
  while(dfs.size()){
    int v=dfs.top()[0],c=dfs.top()[1];
    dfs.pop();
    for(;c<G[v].size();c++){
      if(!vst[G[v][c]]){
        vst[G[v][c]]=true;
        dfs.push({v,c+1});
        dfs.push({G[v][c],0});
        p[G[v][c]]=v;
        break;
      }
    }
    if(c==G[v].size()&&v!=0){
      chi[p[v]]+=chi[v];
    }
  }
  long long ans=0;
  for(int i=0;i<Q;i++){
    int v;
    long long x;
    cin>>v>>x;
    ans+=chi[v-1]*x;
    cout<<ans<<'\n';
  }
}
0