結果

問題 No.1637 Easy Tree Query
ユーザー first_vilfirst_vil
提出日時 2021-08-06 21:56:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 3,857 bytes
コンパイル時間 2,292 ms
コンパイル使用メモリ 208,500 KB
実行使用メモリ 13,496 KB
最終ジャッジ日時 2023-10-17 05:09:22
合計ジャッジ時間 6,332 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 59 ms
9,536 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 27 ms
6,368 KB
testcase_05 AC 44 ms
5,576 KB
testcase_06 AC 30 ms
5,048 KB
testcase_07 AC 13 ms
4,348 KB
testcase_08 AC 20 ms
5,840 KB
testcase_09 AC 51 ms
7,952 KB
testcase_10 AC 21 ms
4,636 KB
testcase_11 AC 64 ms
8,480 KB
testcase_12 AC 55 ms
7,424 KB
testcase_13 AC 13 ms
4,784 KB
testcase_14 AC 41 ms
8,216 KB
testcase_15 AC 64 ms
8,744 KB
testcase_16 AC 57 ms
9,272 KB
testcase_17 AC 17 ms
4,784 KB
testcase_18 AC 41 ms
5,576 KB
testcase_19 AC 43 ms
6,104 KB
testcase_20 AC 55 ms
7,160 KB
testcase_21 AC 34 ms
6,632 KB
testcase_22 AC 70 ms
9,536 KB
testcase_23 AC 18 ms
5,048 KB
testcase_24 AC 30 ms
6,632 KB
testcase_25 AC 43 ms
5,312 KB
testcase_26 AC 57 ms
7,424 KB
testcase_27 AC 72 ms
9,272 KB
testcase_28 AC 33 ms
5,312 KB
testcase_29 AC 48 ms
6,896 KB
testcase_30 AC 28 ms
6,896 KB
testcase_31 AC 26 ms
4,348 KB
testcase_32 AC 26 ms
5,576 KB
testcase_33 AC 37 ms
4,840 KB
testcase_34 AC 35 ms
13,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
template<class T> using V = vector<T>;
using VI = V<int>;
using VL = V<ll>;
using VS = V<string>;
template<class T> using PQ = priority_queue<T, V<T>, greater<T>>;
using graph = V<VI>;
template<class T> using w_graph = V<V<pair<int, T>>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#ifdef _DEBUG
#define line cout << "-----------------------------\n"
#define stop system("pause")
#endif
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 61;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, V<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline V<T> vec(size_t a) { return V<T>(a); }
template<class T>inline V<T> defvec(T def, size_t a) { return V<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return V<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return V<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const V<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const V<V<T>>& v) { for (auto& a : v)print(a); }
template<class T>inline T sum(const V<T>& a, int l, int r) { return 0 <= l && l <= r && r < a.size() ? a[r] - (l == 0 ? 0 : a[l - 1]) : 0; }
template<class T>inline void END(T s) { print(s); exit(0); }
void END() { exit(0); }

class unionfind {
    VI par, rank;
    int forest_num;
public:
    unionfind(const int& n) : par(n), rank(n, 1), forest_num(n) {
        FOR(i, 0, n)par[i] = i;
    }
    int root(int x) {
        if (par[x] == x)return x;
        return par[x] = root(par[x]);
    }
    int size(int x) {
        if (par[x] == x)return rank[x];
        return size(par[x]);
    }
    void unite(int x, int y) {
        int rx = root(x), ry = root(y);
        if (rx == ry)return;
        if (rank[rx] < rank[ry]) {
            par[rx] = ry;
            rank[ry] += rank[rx];
        }
        else {
            par[ry] = rx;
            rank[rx] += rank[ry];
        }
        --forest_num;
    }
    bool same(int x, int y) { return root(x) == root(y); }
    int fnum() { return forest_num; }
};

int main() {
    init();

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

    VL siz(n, 1);
    auto dfs = [&](auto&& f, int cur, int par)->ll {
        for (int to : g[cur]) {
            if (to == par)continue;
            siz[cur] += f(f, to, cur);
        }
        return siz[cur];
    };
    dfs(dfs, 0, -1);

    ll ans = 0;
    while (q--) {
        int p; ll x; cin >> p >> x;
        print(ans += siz[--p] * x);
    }

    return 0;
}
0