結果

問題 No.1637 Easy Tree Query
ユーザー monnu
提出日時 2021-08-17 23:51:54
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 278 ms / 2,000 ms
コード長 717 bytes
コンパイル時間 3,649 ms
コンパイル使用メモリ 230,724 KB
実行使用メモリ 18,560 KB
最終ジャッジ日時 2024-10-11 00:41:29
合計ジャッジ時間 12,358 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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