結果

問題 No.1637 Easy Tree Query
ユーザー jupiro
提出日時 2021-08-06 22:19:24
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 1,295 bytes
コンパイル時間 2,244 ms
コンパイル使用メモリ 199,832 KB
最終ジャッジ日時 2025-01-23 15:41:48
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
using std::cin;
using std::cout;
using std::endl;
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

void solve()
{
  int n, kkt; cin >> n >> kkt;
  std::vector<std::vector<int>> g(n);
  for (int i = 0; i < n - 1; ++i)
  {
    int u, v; cin >> u >> v;
    u -= 1, v -= 1;
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  std::vector<int> in(n), out(n);
  {
    int idx = 0;
    auto dfs = [&](auto &&self, int cur, int pre)->void
    {
      in[cur] = idx++;
      for (const auto &nxt : g[cur])
      {
        if(nxt == pre)
          continue;
        self(self, nxt, cur);
      }
      out[cur] = idx;
    };
    dfs(dfs, 0, -1);
  }
  ll res = 0;
  while(kkt--)
  {
    int p, x; cin >> p >> x;
    p -= 1;
    res += 1LL * x * (out[p] - in[p]);
    cout << res << "\n";
  }
  // cout << res << "\n";
}
int main()
{
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int kkt = 1; 
  // cin >> kkt;
  while(kkt--)
    solve();
  return 0;
  
}
0