如何在 OI 中优雅地使用继承?
posted on 2022-08-12 20:02:40 | under 学术 | source
template<int N> struct dsu{
int fa[N+10],siz[N+10],cnt;
dsu(int n=N):cnt(n){for(int i=1;i<=n;i++) fa[i]=i,siz[i]=1;}
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
void merge(int x,int y){if(x=find(x),y=find(y),x!=y) siz[x]<siz[y]&&(swap(x,y),0),cnt--,fa[y]=x,siz[x]+=siz[y];}
};
template<int N> struct exdsu:public dsu<N>{
int a[N+10];
dsu<N> &s=*this;//attention!
exdsu(){memset(a,0,sizeof a);}
void assign(int l,int r,int k){while(l<=r) if(s.find(l)==l) a[l]=k,s.fa[l]=s.find(l+1); else l=s.find(l);}
bool remain(int l,int r){return s.find(l)<=r;}
int operator[](int i){return a[i];}
};
标签:10,cnt,struct,inheritance,int,dsu,cpp
From: https://www.cnblogs.com/caijianhong/p/16863301.html