結果
問題 | No.1637 Easy Tree Query |
ユーザー |
|
提出日時 | 2022-09-01 22:43:15 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 308 ms / 2,000 ms |
コード長 | 796 bytes |
コンパイル時間 | 2,235 ms |
コンパイル使用メモリ | 198,288 KB |
最終ジャッジ日時 | 2025-02-07 00:26:30 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 33 |
ソースコード
typedef long long ll; typedef long double ld; #include <bits/stdc++.h> using namespace std; #define int long long signed main(){ ll n,q; std::cin >> n>>q; vector<vector<ll>> edges(n); for (int i = 0; i < n-1; i++) { ll a,b; std::cin >> a>>b; a--;b--; edges[a].push_back(b); edges[b].push_back(a); } vector<ll> size(n); function<ll(ll,ll)> dfs = [&](ll now, ll par){ ll sum = 1; for (auto e : edges[now]) { if(e==par)continue; sum += dfs(e, now); } return size[now] = sum; }; dfs(0,-1); ll ans = 0; for (int i = 0; i < q; i++) { ll p,x; std::cin >> p>>x; p--; ans += x*size[p]; std::cout << ans << std::endl; } }