結果
問題 | No.1637 Easy Tree Query |
ユーザー |
![]() |
提出日時 | 2021-08-06 21:24:41 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 200 ms / 2,000 ms |
コード長 | 1,301 bytes |
コンパイル時間 | 1,924 ms |
コンパイル使用メモリ | 172,540 KB |
実行使用メモリ | 18,048 KB |
最終ジャッジ日時 | 2024-09-17 03:28:14 |
合計ジャッジ時間 | 7,899 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 33 |
ソースコード
#include <bits/stdc++.h>using namespace std;#define ll long long#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)#define Pr pair<ll,ll>#define Tp tuple<ll,ll,ll>using Graph = vector<vector<int>>;#define double long doubleconst ll mod = 1000000007;//dfs//s:始点 i:dfs回数 t:始点からの距離vector<int> vis; int t;vector<int> depth;//探索範囲を距離L以下に制限する場合int L;vector<ll> cnt(100001,0);void dfs(Graph &G, int s,int i){int ti = t;t++;for(int nx:G[s]){if(t>=L) break;if(vis[nx]==i) continue;depth[nx] = depth[s] + 1;vis[nx] = i;dfs(G,nx,i);}t++;cnt[s] = (t-ti)/2;}int main() {std::ios::sync_with_stdio(false);std::cin.tie(nullptr);ll N,Q; cin >> N >> Q;Graph G(N+1);rep(i,N-1){ll a,b; cin >> a >> b;G[a].push_back(b);G[b].push_back(a);}ll ans = 0;//main関数内でvis.assign(N+1,-1);depth.assign(N+1,-1);int s = 1;vis[s] = 0; depth[s] = 0; t = -1; //s:始点L= 2000000000; //必要なら設定するdfs(G,s,0); //s:始点 i:dfs回数rep(i,Q){ll p,x; cin >> p >> x;ans += cnt[p]*x;cout << ans << endl;}//cout << ans << endl;}