結果
| 問題 |
No.1637 Easy Tree Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-06 21:32:49 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 176 ms / 2,000 ms |
| コード長 | 2,417 bytes |
| コンパイル時間 | 2,210 ms |
| コンパイル使用メモリ | 200,760 KB |
| 最終ジャッジ日時 | 2025-01-23 14:43:28 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 33 |
ソースコード
#line 1 "/workspaces/compro/lib/template.hpp"
#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>
#ifndef IO_VECTOR
#define IO_VECTOR
template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
int size = v.size();
for (int i = 0; i < size; i++) {
std::cout << v[i];
if (i != size - 1)
std::cout << " ";
}
return out;
}
template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) {
for (auto &el : v) {
std::cin >> el;
}
return in;
}
#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<
using namespace std;
template <class T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
template <class T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());
}
#line 2 "main.cpp"
void solve(int N, int Q, const std::vector<long long> &a, const std::vector<long long> &b,
const std::vector<long long> &p, const std::vector<long long> &x) {
vector<vector<int>> g(N);
REP(i, N - 1) {
g[a[i]].push_back(b[i]);
g[b[i]].push_back(a[i]);
}
vl size(N, 0);
auto dfs = [&](int cur, int par, auto self) -> void {
size[cur] = 1;
for (const auto &el : g[cur]) {
if (el == par)
continue;
self(el, cur, self);
size[cur] += size[el];
}
};
dfs(0, -1, dfs);
ll ans = 0;
REP(i, Q) {
ans += size[p[i]] * x[i];
cout << ans << endl;
}
}
// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
std::cin >> N;
std::vector<long long> a(N - 1), b(N - 1);
std::cin >> Q;
std::vector<long long> p(Q), x(Q);
for (int i = 0; i < N - 1; ++i) {
std::cin >> a[i] >> b[i];
a[i]--;
b[i]--;
}
for (int i = 0; i < Q; ++i) {
std::cin >> p[i] >> x[i];
p[i]--;
}
solve(N, Q, a, b, p, x);
return 0;
}