結果
| 問題 |
No.1637 Easy Tree Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-07 02:18:33 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 289 ms / 2,000 ms |
| コード長 | 750 bytes |
| コンパイル時間 | 851 ms |
| コンパイル使用メモリ | 75,188 KB |
| 最終ジャッジ日時 | 2025-01-23 16:29:12 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 33 |
ソースコード
#include <iostream>
#include <vector>
using namespace std;
vector<int> G[100100];
vector<int> cnt(100100, 1);
vector<bool> check(100100, 0);
long dfs(int x) {
check[x] = 1;
for (int i = 0; i < G[x].size(); i++) {
if (!check[G[x][i]]) {
cnt[x] += dfs(G[x][i]);
}
}
return cnt[x];
}
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].emplace_back(b);
G[b].emplace_back(a);
}
dfs(0);
long ans = 0;
for (int i = 0; i < q; i++) {
int p;
long x;
cin >> p >> x;
p--;
ans += x * cnt[p];
cout << ans << endl;
}
return 0;
}