結果
問題 |
No.1637 Easy Tree Query
|
ユーザー |
![]() |
提出日時 | 2021-08-08 16:13:02 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,121 bytes |
コンパイル時間 | 2,934 ms |
コンパイル使用メモリ | 213,700 KB |
実行使用メモリ | 18,048 KB |
最終ジャッジ日時 | 2024-09-19 12:59:39 |
合計ジャッジ時間 | 16,429 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 1 WA * 31 TLE * 1 |
ソースコード
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using Graph = vector<vector<int>>; int main() { int N,Q; cin >> N >> Q; Graph G(N+1); for (int i=0; i<N-1; i++) { int a,b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } vector<int>depth(N+1,0); stack<int>S; S.push(1); vector<bool>seen(N+1,false); seen[1]=true; while(S.size()!=0) { int x=S.top(); S.pop(); for (auto next: G[x]) { if (seen[next]) { continue; } else { depth[next]=depth[x]+1; seen[next]=true; S.push(next); } } } vector<int>P(N+1,1); P[1]=N; for (int i=2; i<=N; i++) { stack<int>A; A.push(i); vector<bool>see(N+1,false); see[i]=true; while (A.size()!=0) { int x=A.top(); A.pop(); for (auto next: G[x]) { if (!(see[next]) && depth[next]>depth[x]) { see[next]=true; A.push(next); P[i]++; } } } } int ans=0; for (int i=0; i<Q; i++) { int p,x; cin >> p >> x; ans+=P[p]*x; cout << ans << endl; } }