結果
| 問題 |
No.1637 Easy Tree Query
|
| コンテスト | |
| ユーザー |
Example0911
|
| 提出日時 | 2021-08-06 21:24:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 176 ms / 2,000 ms |
| コード長 | 736 bytes |
| コンパイル時間 | 1,797 ms |
| コンパイル使用メモリ | 196,732 KB |
| 最終ジャッジ日時 | 2025-01-23 14:29:10 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 33 |
ソースコード
#include "bits/stdc++.h"
#define int long long
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 1000000007;
int N, Q;
vector<int>G[100010];
vector<int>s;
void dfs(int v, int p) {
for (auto nv : G[v]) {
if (nv == p)continue;
dfs(nv, v);
}
s[v] = 1;
for (auto c : G[v]) {
if (c == p)continue;
s[v] += s[c];
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> Q;
s.resize(N);
for (int i = 0; i < N - 1; i++) {
int a, b; cin >> a >> b; a--; b--;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(0, -1);
int now = 0;
for (int _ = 0; _ < Q; _++) {
int p, x; cin >> p >> x; p--;
now += s[p] * x;
cout << now << endl;
}
return 0;
}
Example0911