結果

問題 No.1637 Easy Tree Query
ユーザー monnumonnu
提出日時 2021-08-17 23:51:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 285 ms / 2,000 ms
コード長 717 bytes
コンパイル時間 3,960 ms
コンパイル使用メモリ 225,136 KB
実行使用メモリ 18,432 KB
最終ジャッジ日時 2024-04-19 08:04:57
合計ジャッジ時間 12,594 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 261 ms
9,216 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 61 ms
6,940 KB
testcase_05 AC 218 ms
6,940 KB
testcase_06 AC 145 ms
6,944 KB
testcase_07 AC 77 ms
6,940 KB
testcase_08 AC 49 ms
6,944 KB
testcase_09 AC 166 ms
7,808 KB
testcase_10 AC 98 ms
6,944 KB
testcase_11 AC 230 ms
8,192 KB
testcase_12 AC 236 ms
7,040 KB
testcase_13 AC 36 ms
6,940 KB
testcase_14 AC 105 ms
7,936 KB
testcase_15 AC 218 ms
8,576 KB
testcase_16 AC 159 ms
8,960 KB
testcase_17 AC 65 ms
6,940 KB
testcase_18 AC 203 ms
6,940 KB
testcase_19 AC 195 ms
6,940 KB
testcase_20 AC 253 ms
6,944 KB
testcase_21 AC 124 ms
6,944 KB
testcase_22 AC 257 ms
9,216 KB
testcase_23 AC 62 ms
6,944 KB
testcase_24 AC 94 ms
6,940 KB
testcase_25 AC 222 ms
6,940 KB
testcase_26 AC 247 ms
7,424 KB
testcase_27 AC 285 ms
8,832 KB
testcase_28 AC 157 ms
6,940 KB
testcase_29 AC 186 ms
6,944 KB
testcase_30 AC 56 ms
6,944 KB
testcase_31 AC 169 ms
6,940 KB
testcase_32 AC 102 ms
6,940 KB
testcase_33 AC 215 ms
6,948 KB
testcase_34 AC 78 ms
18,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll=long long;
using Graph=vector<vector<int>>;
#define MAX 2000000
#define MOD 1000000007
#define INF 1000000000

void dfs(Graph &G,int v,int p,vector<int> &cnt){
  cnt[v]=1;
  for(auto nv:G[v]){
    if(nv==p){
      continue;
    }
    dfs(G,nv,v,cnt);
    cnt[v]+=cnt[nv];
  }
}

int main(){
  int N,Q;
  cin>>N>>Q;
  Graph 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> cnt(N);
  dfs(G,0,-1,cnt);
  ll ans=0;
  for(int i=0;i<Q;i++){
    int p,x;
    cin>>p>>x;
    p--;
    ans+=(ll)cnt[p]*(ll)x;
    cout<<ans<<'\n';
  }
}
0