結果
| 問題 |
No.1637 Easy Tree Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-10-10 22:32:00 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 117 ms / 2,000 ms |
| コード長 | 865 bytes |
| コンパイル時間 | 2,161 ms |
| コンパイル使用メモリ | 204,996 KB |
| 最終ジャッジ日時 | 2025-02-08 01:18:16 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 33 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N,Q;
cin>>N>>Q;
vector<vector<int>>G(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);
}
vector<int>chi(N,1),p(N);
stack<array<int,2>>dfs;
dfs.push({0,0});
vector<bool>vst(N);
vst[0]=true;
while(dfs.size()){
int v=dfs.top()[0],c=dfs.top()[1];
dfs.pop();
for(;c<G[v].size();c++){
if(!vst[G[v][c]]){
vst[G[v][c]]=true;
dfs.push({v,c+1});
dfs.push({G[v][c],0});
p[G[v][c]]=v;
break;
}
}
if(c==G[v].size()&&v!=0){
chi[p[v]]+=chi[v];
}
}
long long ans=0;
for(int i=0;i<Q;i++){
int v;
long long x;
cin>>v>>x;
ans+=chi[v-1]*x;
cout<<ans<<'\n';
}
}