結果
| 問題 |
No.1637 Easy Tree Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-06 21:42:46 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 57 ms / 2,000 ms |
| コード長 | 1,705 bytes |
| コンパイル時間 | 2,352 ms |
| コンパイル使用メモリ | 199,668 KB |
| 最終ジャッジ日時 | 2025-01-23 14:55:13 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 33 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define overload3(_1, _2, _3, name, ...) name
#define rep1(n) for (decltype(n) _tmp = 0; _tmp < n; _tmp++)
#define rep2(i, n) for (decltype(n) i = 0; i < n; i++)
#define rep3(i, a, b) for (decltype(n) i = a; i < b; i++)
#define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
struct IOSetup {
IOSetup() noexcept {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template<class T> bool chmax(T &a, const T &b) { return a < b ? a = b, true : false; }
template<class T> bool chmin(T &a, const T &b) { return a > b ? a = b, true : false; }
using i64 = long long;
using u64 = unsigned long long;
#include <boost/hana/functional/fix.hpp>
#if __has_include(<debug.hpp>)
#include <debug.hpp>
#else
#define dbg(...) (void(0))
#endif
int main() {
size_t n, q;
cin >> n >> q;
vector<vector<size_t>> tree(n, vector<size_t>{});
rep(n - 1) {
size_t u, v;
cin >> u >> v;
tree[u - 1].push_back(v - 1);
tree[v - 1].push_back(u - 1);
}
vector<int> children(n, -1); // inclusive
boost::hana::fix([&](auto &&dfs, size_t v, size_t prev) -> void {
int cnt = 1;
for (const auto &u: tree[v]) {
if (u == prev) continue;
if (children[u] == -1) dfs(u, v);
cnt += children[u];
}
children[v] = cnt;
dbg(v);
})(0, 0);
dbg(children);
i64 ans = 0;
while (q--) {
size_t p;
i64 x;
cin >> p >> x;
ans += children[p - 1] * x;
cout << ans << "\n";
}
}