結果

問題 No.1637 Easy Tree Query
ユーザー Ricky_ponRicky_pon
提出日時 2021-08-06 21:26:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 3,142 bytes
コンパイル時間 2,373 ms
コンパイル使用メモリ 208,012 KB
実行使用メモリ 18,652 KB
最終ジャッジ日時 2023-10-17 04:51:35
合計ジャッジ時間 6,459 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 69 ms
11,524 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 30 ms
7,384 KB
testcase_05 AC 49 ms
6,304 KB
testcase_06 AC 34 ms
5,452 KB
testcase_07 AC 15 ms
4,348 KB
testcase_08 AC 23 ms
6,560 KB
testcase_09 AC 58 ms
9,172 KB
testcase_10 AC 24 ms
5,188 KB
testcase_11 AC 76 ms
9,940 KB
testcase_12 AC 63 ms
8,356 KB
testcase_13 AC 14 ms
5,188 KB
testcase_14 AC 46 ms
9,412 KB
testcase_15 AC 72 ms
10,204 KB
testcase_16 AC 69 ms
10,996 KB
testcase_17 AC 20 ms
5,188 KB
testcase_18 AC 46 ms
6,304 KB
testcase_19 AC 48 ms
6,864 KB
testcase_20 AC 64 ms
8,356 KB
testcase_21 AC 41 ms
7,564 KB
testcase_22 AC 85 ms
10,996 KB
testcase_23 AC 21 ms
5,452 KB
testcase_24 AC 37 ms
7,564 KB
testcase_25 AC 49 ms
6,088 KB
testcase_26 AC 68 ms
8,624 KB
testcase_27 AC 86 ms
10,732 KB
testcase_28 AC 38 ms
5,716 KB
testcase_29 AC 55 ms
8,092 KB
testcase_30 AC 31 ms
8,092 KB
testcase_31 AC 30 ms
4,348 KB
testcase_32 AC 28 ms
6,244 KB
testcase_33 AC 42 ms
5,188 KB
testcase_34 AC 42 ms
18,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>


#include <vector>

template <class T = int>
struct Edge {
    int from, to;
    T cost;
    int idx;

    Edge() = default;

    Edge(int from, int to, T cost = 1, int idx = 0)
        : from(from), to(to), cost(cost), idx(idx) {}
};

template <class T = int>
struct Graph {
    std::vector<std::vector<Edge<T>>> es;
    int edge_num;

    Graph(int n) : edge_num(0) { es.resize(n); }

    int size() { return es.size(); }

    void add_edge(int from, int to, T cost = 1, int idx = 0) {
        es[from].emplace_back(from, to, cost, idx);
        ++edge_num;
    }

    std::vector<Edge<T>> edges() {
        std::vector<Edge<T>> tmp;
        for (int v = 0; v < (int)es.size(); ++v) {
            for (auto& e : es[v]) {
                tmp.push_back(e);
            }
        }
        if (tmp.size() >= 2 && tmp[0].idx == tmp[1].idx) {
            return tmp;
        }
        std::vector<Edge<T>> res(edge_num);
        for (auto& e : tmp) res[e.idx] = e;
        return res;
    }

    std::vector<Edge<T>>& operator[](int i) { return es[i]; }
};


#include <algorithm>
#include <cassert>
#include <vector>

template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}

template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

template <typename T>
struct CoordComp {
    std::vector<T> v;
    bool sorted;

    CoordComp() : sorted(false) {}

    int size() { return v.size(); }

    void add(T x) { v.push_back(x); }

    void build() {
        std::sort(v.begin(), v.end());

        v.erase(std::unique(v.begin(), v.end()), v.end());
        sorted = true;
    }

    int get_idx(T x) {
        assert(sorted);
        return lower_bound(v.begin(), v.end(), x) - v.begin();
    }

    T &operator[](int i) { return v[i]; }
};


#define For(i, a, b) for (int i = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For(i, 0, n)
#define rrep(i, n) rFor(i, n, 0)
#define fi first
#define se second

using namespace std;

using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;

int dfs(Graph<int> &gr, int v, int pv, vector<int> &sz) {
    for (auto &e : gr[v]) {
        if (e.to != pv) {
            sz[v] += dfs(gr, e.to, v, sz);
        }
    }
    return sz[v];
}

int main() {
    int n, q;
    scanf("%d%d", &n, &q);
    Graph<int> gr(n);
    rep(i, n - 1) {
        int a, b;
        scanf("%d%d", &a, &b);
        --a;
        --b;
        gr.add_edge(a, b, 1);
        gr.add_edge(b, a, 1);
    }
    vector<int> sz(n, 1);
    dfs(gr, 0, -1, sz);

    lint ans = 0;
    rep(i, q) {
        int p, x;
        scanf("%d%d", &p, &x);
        --p;
        ans += (lint)sz[p] * x;
        printf("%lld\n", ans);
    }
}
0