結果

問題 No.1637 Easy Tree Query
ユーザー masutech16masutech16
提出日時 2021-08-06 21:32:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 2,417 bytes
コンパイル時間 2,847 ms
コンパイル使用メモリ 207,176 KB
実行使用メモリ 14,820 KB
最終ジャッジ日時 2023-10-17 05:01:21
合計ジャッジ時間 8,079 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 174 ms
12,708 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 34 ms
7,428 KB
testcase_05 AC 151 ms
7,692 KB
testcase_06 AC 99 ms
6,372 KB
testcase_07 AC 56 ms
4,348 KB
testcase_08 AC 28 ms
6,636 KB
testcase_09 AC 105 ms
10,068 KB
testcase_10 AC 68 ms
5,612 KB
testcase_11 AC 147 ms
11,124 KB
testcase_12 AC 152 ms
9,804 KB
testcase_13 AC 22 ms
5,316 KB
testcase_14 AC 56 ms
9,540 KB
testcase_15 AC 132 ms
11,124 KB
testcase_16 AC 93 ms
11,388 KB
testcase_17 AC 42 ms
5,424 KB
testcase_18 AC 138 ms
7,428 KB
testcase_19 AC 133 ms
7,956 KB
testcase_20 AC 162 ms
9,540 KB
testcase_21 AC 75 ms
7,956 KB
testcase_22 AC 156 ms
12,180 KB
testcase_23 AC 40 ms
5,844 KB
testcase_24 AC 58 ms
7,956 KB
testcase_25 AC 153 ms
7,428 KB
testcase_26 AC 162 ms
10,068 KB
testcase_27 AC 178 ms
12,180 KB
testcase_28 AC 108 ms
6,900 KB
testcase_29 AC 126 ms
9,012 KB
testcase_30 AC 28 ms
7,956 KB
testcase_31 AC 130 ms
4,788 KB
testcase_32 AC 64 ms
6,636 KB
testcase_33 AC 148 ms
6,636 KB
testcase_34 AC 34 ms
14,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "/workspaces/compro/lib/template.hpp"


#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}


#line 2 "main.cpp"

void solve(int N, int Q, const std::vector<long long> &a, const std::vector<long long> &b,
           const std::vector<long long> &p, const std::vector<long long> &x) {
  vector<vector<int>> g(N);
  REP(i, N - 1) {
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }

  vl size(N, 0);
  auto dfs = [&](int cur, int par, auto self) -> void {
    size[cur] = 1;
    for (const auto &el : g[cur]) {
      if (el == par)
        continue;
      self(el, cur, self);
      size[cur] += size[el];
    }
  };
  dfs(0, -1, dfs);

  ll ans = 0;
  REP(i, Q) {
    ans += size[p[i]] * x[i];
    cout << ans << endl;
  }
}

// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, Q;
  std::cin >> N;
  std::vector<long long> a(N - 1), b(N - 1);
  std::cin >> Q;
  std::vector<long long> p(Q), x(Q);
  for (int i = 0; i < N - 1; ++i) {
    std::cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  for (int i = 0; i < Q; ++i) {
    std::cin >> p[i] >> x[i];
    p[i]--;
  }
  solve(N, Q, a, b, p, x);
  return 0;
}
0