結果

問題 No.1054 Union add query
ユーザー TAISA_TAISA_
提出日時 2020-05-15 21:57:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 2,537 bytes
コンパイル時間 2,107 ms
コンパイル使用メモリ 204,036 KB
実行使用メモリ 8,988 KB
最終ジャッジ日時 2023-10-19 14:09:51
合計ジャッジ時間 4,588 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 143 ms
4,500 KB
testcase_04 AC 156 ms
8,988 KB
testcase_05 AC 139 ms
4,348 KB
testcase_06 AC 131 ms
5,556 KB
testcase_07 AC 121 ms
5,556 KB
testcase_08 AC 132 ms
5,556 KB
testcase_09 AC 148 ms
8,988 KB
testcase_10 AC 101 ms
8,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
template <class T>
inline void chmin(T &a, const T &b) { a = min(a, b); }
template <class T>
inline void chmax(T &a, const T &b) { a = max(a, b); }
template <class T>
inline bool kbit(const T &x, const int &k) { return ((x >> k) & 1LL); }
inline int popcount(const int &n) { return __builtin_popcount(n); }
inline ll popcountll(const ll &n) { return __builtin_popcountll(n); }
template <class T>
void zip(V<T> &v) {
    sort(all(v));
    v.erase(unique(all(v)), v.end());
}
void dump() {
    cerr << '\n';
}
template <class Head, class... Tail>
void dump(Head &&head, Tail &&... tail) {
    cerr << head << (sizeof...(Tail) == 0 ? " " : ", ");
    dump(std::move(tail)...);
}
template <class T>
void print(const vector<T> &v) {
    for (int i = 0; i < v.size(); i++) cout << v[i] << (i + 1 == v.size() ? '\n' : ' ');
}
template <class T>
void read(vector<T> &v) {
    for (int i = 0; i < v.size(); i++) cin >> v[i];
}
constexpr char sp = ' ', newl = '\n';
constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll MOD = 1e9 + 7;
//@docs Docs/UnionFind.md
struct UnionFind {
    vector<int> par, sz, dat;
    UnionFind(int n) : par(n), sz(n, 1), dat(n) {
        iota(par.begin(), par.end(), 0);
    }
    inline int find(int x) {
        if (x == par[x]) return x;
        return find(par[x]);
    }
    bool unite(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return false;
        if (sz[u] < sz[v]) swap(u, v);
        par[v] = u;
        dat[v] -= dat[u];
        sz[u] += sz[v];
        return true;
    }
    inline int size(int x) {
        return sz[find(x)];
    }
    inline bool same(int u, int v) {
        return find(u) == find(v);
    }
    void add(int k, int x) {
        dat[find(k)] += x;
    }
    int get(int x) {
        if (par[x] == x) return dat[x];
        return dat[x] + get(par[x]);
    }
};
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    UnionFind uf(n);
    while (q--) {
        int t, a, b;
        cin >> t >> a >> b;
        --a;
        if (t == 1) {
            --b;
            uf.unite(a, b);
        } else if (t == 2) {
            uf.add(a, b);
        } else {
            cout << uf.get(a) << newl;
        }
    }
}
0