結果

問題 No.1054 Union add query
ユーザー kyoprounokyoprouno
提出日時 2020-05-15 23:11:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 495 ms / 2,000 ms
コード長 2,579 bytes
コンパイル時間 2,043 ms
コンパイル使用メモリ 184,716 KB
実行使用メモリ 14,848 KB
最終ジャッジ日時 2023-10-19 16:54:21
合計ジャッジ時間 6,046 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 364 ms
5,548 KB
testcase_04 AC 422 ms
14,848 KB
testcase_05 AC 344 ms
4,492 KB
testcase_06 AC 329 ms
7,924 KB
testcase_07 AC 300 ms
7,924 KB
testcase_08 AC 234 ms
7,924 KB
testcase_09 AC 495 ms
14,848 KB
testcase_10 AC 96 ms
14,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

const ll INF=1e9+7;


class DisjointSet{
    public:
    vector<ll> rank,p,val;
    DisjointSet(){}
    DisjointSet(ll size){ //作られうる木の頂点数の最大値を入れる。
        rank.resize(size,0);
        p.resize(size,0);
        val.resize(size,0);
        rep(i,size) makeSet(i);
    }
    void makeSet(ll x){ //xだけが属する木を作る。
        p[x]=x;
        rank[x]=0;
    }
    bool same(ll x,ll y){ //xとyが同じ木に属するかどうか
        return findSet(x)==findSet(y);
    }
    void unite(ll x, ll y){
        link(findSet(x),findSet(y));
    }
    void link(ll x, ll y){ //rankが大きい方の根に小さい方の根をつける。
        if(rank[x]>rank[y]){
            val[y]-=val[x];
            p[y]=x;
            
        }
        else{
            val[x]-=val[y];
            p[x]=y;
            
            if(rank[x]==rank[y]){
                rank[y]++; //xとyの木のrankが同じであれば、統合するとrankが1増える。
            }
        }
    }
    ll findSet(ll x){ //xが属する木の根の番号を返す
        ll num=p[x];
        if(x!=p[x]){
            num=findSet(p[x]);
        }
        return num;
    }
    ll calc(ll x){
        if(x==p[x]) return val[x];
        if(p[x]==findSet(x)){
            return val[x]+val[p[x]];
        }
        val[x]+=calc(p[x])-val[findSet(x)];
        p[x]=findSet(x);
        return val[x]+val[findSet(x)];
    }
};


int main()
{
    ios;
    ll n,q;
    cin >> n >> q;
    DisjointSet ds=DisjointSet(n);
    while(q--){
        ll t,a,b;
        cin >> t >> a >> b;
        if(t==1){
            a--; b--;
            if(!ds.same(a,b)){
                ds.unite(a,b);
            }
        }
        if(t==2){
            a--;
            ds.val[ds.findSet(a)]+=b;
            //cout << ds.val[ds.findSet(a)] << " ";
        }
        if(t==3){
            a--;
            cout << ds.calc(a) << endl;
        }
    }
    return 0;
} 
0