結果
問題 |
No.1637 Easy Tree Query
|
ユーザー |
![]() |
提出日時 | 2021-08-08 20:38:56 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,300 bytes |
コンパイル時間 | 2,475 ms |
コンパイル使用メモリ | 217,024 KB |
実行使用メモリ | 21,064 KB |
最終ジャッジ日時 | 2024-09-19 17:20:55 |
合計ジャッジ時間 | 6,635 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 WA * 1 |
other | TLE * 1 -- * 32 |
ソースコード
#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<long int>P(N+1,1); P[1]=N; vector<int>p(Q); vector<long int>x(Q); for (int i=0; i<Q; i++) { cin >> p[i] >> x[i]; } for (int i=0; i<Q; i++) { int y=p[i]; if (y!=1) { continue; } stack<int>A; A.push(y); vector<bool>see(N+1,false); see[y]=true; int count=1; 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); count++; } } } P[y]=count; } long int ans=0; for (int i=0; i<Q; i++) { ans+=P[p[i]]*x[i]; cout << ans << endl; } }