結果

問題 No.1637 Easy Tree Query
ユーザー ytsmashytsmash
提出日時 2021-08-20 19:30:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 275 ms / 2,000 ms
コード長 1,030 bytes
コンパイル時間 2,288 ms
コンパイル使用メモリ 199,836 KB
実行使用メモリ 14,888 KB
最終ジャッジ日時 2024-04-22 01:48:07
合計ジャッジ時間 10,404 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 265 ms
10,956 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 55 ms
7,296 KB
testcase_05 AC 200 ms
6,940 KB
testcase_06 AC 135 ms
6,940 KB
testcase_07 AC 75 ms
6,940 KB
testcase_08 AC 44 ms
6,940 KB
testcase_09 AC 157 ms
8,960 KB
testcase_10 AC 92 ms
6,940 KB
testcase_11 AC 217 ms
9,600 KB
testcase_12 AC 219 ms
8,064 KB
testcase_13 AC 35 ms
6,944 KB
testcase_14 AC 96 ms
9,216 KB
testcase_15 AC 200 ms
9,856 KB
testcase_16 AC 159 ms
10,624 KB
testcase_17 AC 67 ms
6,940 KB
testcase_18 AC 204 ms
6,944 KB
testcase_19 AC 189 ms
6,940 KB
testcase_20 AC 242 ms
7,936 KB
testcase_21 AC 117 ms
7,296 KB
testcase_22 AC 255 ms
10,624 KB
testcase_23 AC 59 ms
6,944 KB
testcase_24 AC 93 ms
7,424 KB
testcase_25 AC 210 ms
6,940 KB
testcase_26 AC 239 ms
8,448 KB
testcase_27 AC 275 ms
10,368 KB
testcase_28 AC 155 ms
6,944 KB
testcase_29 AC 189 ms
7,680 KB
testcase_30 AC 58 ms
7,680 KB
testcase_31 AC 176 ms
6,944 KB
testcase_32 AC 95 ms
6,944 KB
testcase_33 AC 201 ms
6,940 KB
testcase_34 AC 69 ms
14,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *    author:  ytsmash
 *    created: 15.08.2021 10:24:54
 **/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
using P = pair<int, int>;

vector<vector<ll>> d;
vector<ll> cnt;
vector<ll> B;

void f(int i) {
    B[i] = 1;
    for (ll j : d[i]) {
        if (B[j] == 0) {
            f(j);
            cnt[i] += cnt[j];
        }
    }
    return;
}

int main() {
    ll N, Q, a, b, ans = 0, p, x;
    cin >> N >> Q;
    d.resize(N);
    cnt.resize(N);
    B.resize(N);
    for (int i = 0; i < N - 1; i++) {
        cnt[i] = 1;  // 頂点p[i]を根とする部分木に含まれる頂点の数
        cin >> a >> b;
        d[a - 1].push_back(b - 1);
        d[b - 1].push_back(a - 1);
    }
    cnt[N - 1] = 1;
    f(0);
    rep(i, Q) {
        cin >> p >> x;
        ans += cnt[p - 1] * x;
        cout << ans << "\n";
    }
}
0