cplib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub NEET-6z/cplib

:warning: src/dsu/dsu_weighted.hpp

Depends on

Code

#include "../template.hpp"

template<typename T> struct dsu_weighted {
    vector<int> p;
    vector<T> w;
    dsu_weighted(int n):p(n,-1),w(n,0){}
    bool same(int a,int b){return leader(a)==leader(b);}
    int leader(int a){
        return p[a]<0?a:(leader(p[a]),w[a]+=w[p[a]],p[a]=leader(p[a]));
    }
    int size(int a){return -p[leader(a)];}
    int merge(int a,int b,long c){
        c+=weight(a),c-=weight(b);
        a=leader(a),b=leader(b);
        if(a==b) return a;
        if(p[a]>p[b]) swap(a,b),c*=-1;
        p[a]+=p[b];
        p[b]=a;
        w[b]=c;
        return a;
    }
    T weight(int a){
        leader(a);
        return w[a];
    }
};
#line 2 "src/template.hpp"
#include <bits/stdc++.h>
using namespace std;
#define si(a) (long)a.size()
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(int)(n);++i)
template<typename S,typename F> bool chmin(S&a,F b){return b<a?(a=b,1):0;}
template<typename S,typename F> bool chmax(S&a,F b){return b>a?(a=b,1):0;}
bool _=(ios::sync_with_stdio(0),cin.tie(0),cout<<fixed<<setprecision(16),0);
#line 2 "src/dsu/dsu_weighted.hpp"

template<typename T> struct dsu_weighted {
    vector<int> p;
    vector<T> w;
    dsu_weighted(int n):p(n,-1),w(n,0){}
    bool same(int a,int b){return leader(a)==leader(b);}
    int leader(int a){
        return p[a]<0?a:(leader(p[a]),w[a]+=w[p[a]],p[a]=leader(p[a]));
    }
    int size(int a){return -p[leader(a)];}
    int merge(int a,int b,long c){
        c+=weight(a),c-=weight(b);
        a=leader(a),b=leader(b);
        if(a==b) return a;
        if(p[a]>p[b]) swap(a,b),c*=-1;
        p[a]+=p[b];
        p[b]=a;
        w[b]=c;
        return a;
    }
    T weight(int a){
        leader(a);
        return w[a];
    }
};
Back to top page