結果

問題 No.1637 Easy Tree Query
ユーザー nuxxppnuxxpp
提出日時 2021-08-07 16:51:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 1,535 bytes
コンパイル時間 2,939 ms
コンパイル使用メモリ 161,156 KB
実行使用メモリ 14,156 KB
最終ジャッジ日時 2023-10-18 23:35:38
合計ジャッジ時間 8,614 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,956 KB
testcase_01 AC 2 ms
5,960 KB
testcase_02 AC 176 ms
9,472 KB
testcase_03 AC 2 ms
5,956 KB
testcase_04 AC 34 ms
7,820 KB
testcase_05 AC 154 ms
7,296 KB
testcase_06 AC 101 ms
6,948 KB
testcase_07 AC 58 ms
6,040 KB
testcase_08 AC 27 ms
7,420 KB
testcase_09 AC 104 ms
8,688 KB
testcase_10 AC 69 ms
6,748 KB
testcase_11 AC 148 ms
8,980 KB
testcase_12 AC 154 ms
8,288 KB
testcase_13 AC 23 ms
6,796 KB
testcase_14 AC 56 ms
8,752 KB
testcase_15 AC 134 ms
9,116 KB
testcase_16 AC 92 ms
9,452 KB
testcase_17 AC 44 ms
6,792 KB
testcase_18 AC 141 ms
7,296 KB
testcase_19 AC 135 ms
7,564 KB
testcase_20 AC 164 ms
8,224 KB
testcase_21 AC 76 ms
7,868 KB
testcase_22 AC 157 ms
9,524 KB
testcase_23 AC 40 ms
6,980 KB
testcase_24 AC 59 ms
7,876 KB
testcase_25 AC 157 ms
7,188 KB
testcase_26 AC 165 ms
8,420 KB
testcase_27 AC 178 ms
9,364 KB
testcase_28 AC 110 ms
7,100 KB
testcase_29 AC 125 ms
8,112 KB
testcase_30 AC 30 ms
8,088 KB
testcase_31 AC 134 ms
5,972 KB
testcase_32 AC 65 ms
7,232 KB
testcase_33 AC 150 ms
6,768 KB
testcase_34 AC 35 ms
14,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <bitset>
#include <set>
#include <deque>
#include <stack>
#include <atcoder/all>
#include <array>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < (t); ++i)
#define dsrep(i,t,s) for(int i = (t)-1; i >= (s); --i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
int dx4[4]={1,0,-1,0};
int dy4[4]={0,-1,0,1};
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using P = pair<int,int>;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
// const ll mod = 998244353;
const ll mod = 1e9 + 7;

///////////////////////////////////////////
int n, q;
vector<int> g[100005];
int cnt[100005];

int dfs(int x) {
  cnt[x] = 1;

  int sum = 0;
  for(auto y : g[x]) {
    if(cnt[y] < 0) sum += dfs(y);
  }

  return cnt[x] += sum;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> q;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--; b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  rep(i, n) cnt[i] = -1;
  dfs(0);

  ll sum = 0;
  while(q--) {
    int p, x;
    cin >> p >> x;
    p--;
    sum += (ll)cnt[p] * x;
    cout << sum << endl;
  }
}
0