結果
| 問題 |
No.1637 Easy Tree Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-06 22:00:43 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 286 ms / 2,000 ms |
| コード長 | 836 bytes |
| コンパイル時間 | 1,121 ms |
| コンパイル使用メモリ | 98,800 KB |
| 最終ジャッジ日時 | 2025-01-23 15:19:54 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 33 |
ソースコード
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i=0;i < (int)(n);i++)
vector<vector<int>> g;
vector<int> subsize;
int dfs(int now,int pre){
int res = 1;
for (auto nx:g[now]){
if (nx == pre) continue;
res += dfs(nx,now);
}
return subsize[now] = res;
}
int main(){
int n,q;
cin >> n >> q;
g.resize(n);
subsize.resize(n);
rep(i,n-1){
int a,b;
cin >> a >> b;
a--;b--;
g[a].push_back(b);
g[b].push_back(a);
}
ll ans = 0;
dfs(0,-1);
while(q--){
int p;
ll x;
cin >> p >> x;
ans += subsize[p-1] * x;
cout << ans << endl;
}
return 0;
}