結果

問題 No.1637 Easy Tree Query
ユーザー penguin8331penguin8331
提出日時 2022-10-21 14:01:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,400 bytes
コンパイル時間 1,573 ms
コンパイル使用メモリ 170,096 KB
実行使用メモリ 4,884 KB
最終ジャッジ日時 2023-09-13 15:24:26
合計ジャッジ時間 11,770 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 WA -
testcase_02 RE -
testcase_03 WA -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#if 1 && defined(LOCAL)
#include <debug.hpp>
#else
#define debug(...)
#define line
#endif
using namespace std;
using ll = long long;
using ld = long double;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); }
template<typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); }

struct UnionFind {
    vector<int> par, siz;
    //初期化
    UnionFind(int N) : par(N, -1), siz(N, 1){};
    int N = par.size();
    //根を求める
    int root(int x) {
        if (par[x] == -1)
            return x;
        else
            return par[x] = root(par[x]);
    }
    // xとyが同じグループに属するか(根が一致するか)
    bool issame(int x, int y) { return root(x) == root(y); }
    // xを含むグループとyを含むグループを併合
    bool unite(int x, int y) {
        // x,yを根まで移動
        x = root(x);
        y = root(y);
        //同じだったら何もない
        if (x == y) return false;
        // union by size(y側のサイズを小さくする)
        if (siz[x] > siz[y]) swap(x, y);
        // yをxの子とする
        par[y] = x;
        siz[x] += siz[y];
        return true;
    }
    // xを含むグループのサイズ
    int size(int x) { return siz[root(x)]; }
    //どのようなグループ構成か返す
    vector<vector<int>> groups() {
        vector<int> unite_buf(N), group_siz(N);
        for (int i = 0; i < N; i++) {
            unite_buf[i] = root(i);
            group_siz[unite_buf[i]]++;
        }
        vector<vector<int>> res(N);
        for (int i = 0; i < N; i++) {
            res[i].reserve(group_siz[i]);
        }
        for (int i = 0; i < N; i++) {
            res[unite_buf[i]].push_back(i);
        }
        res.erase(remove_if(res.begin(), res.end(),
                            [&](const vector<int>& v) { return v.empty(); }),
                  res.end());
        return res;
    }
};
int main(){
    int N,Q;
    cin>>N>>Q;
    UnionFind uf(N);
    for(int i=0;i<N;i++){
        int a,b;
        cin>>a>>b;
        a--;b--;
        uf.unite(a,b);
    }
    ll pre=0;
    for(int i=0;i<Q;i++){
        int x,y;
        cin>>x>>y;
        x--;
        pre+=(ll)uf.size(x)*y;
        cout<<pre<<endl;
    }
}
0