結果
| 問題 |
No.1637 Easy Tree Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-09 10:40:50 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 57 ms / 2,000 ms |
| コード長 | 608 bytes |
| コンパイル時間 | 2,097 ms |
| コンパイル使用メモリ | 194,408 KB |
| 最終ジャッジ日時 | 2025-01-23 17:10:06 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 33 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
vector <int> adj[100005];
int dp[100005];
int dfs(int now,int prev)
{
dp[now] = 1;
for(auto next : adj[now])
{
if(prev==next)
{
continue;
}
dp[now] += dfs(next,now);
}
return dp[now];
}
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
int n,q,a,b;
cin >> n >> q;
for(int i=0;i<n-1;i++)
{
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1,0);
long long int sum = 0;
for(int i=0;i<q;i++)
{
cin >> a >> b;
long long int res = dp[a];
res*=b;
sum += res;
cout << sum << '\n';
}
return 0;
}