1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cctype> #include <iostream> #include <cmath> #define ll long long #define ri register int using namespace std; const int maxn=200005; const int inf=0x7fffffff; template <class T>inline void read(T &x){ x=0;int ne=0;char c; while(!isdigit(c=getchar()))ne=c=='-'; x=c-48; while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48; x=ne?-x:x; return ; } int size[maxn],id[maxn],sum[maxn],fa[maxn],tot=0; int find(int x){ return fa[x]==x?fa[x]:fa[x]=find(fa[x]); } int n,m; int main(){ int opt,p,q,x,y; while(scanf("%d %d",&n,&m)!=EOF){ tot=n; for(ri i=1;i<=n;i++){ size[i]=1; fa[i]=id[i]=sum[i]=i; } while(m--){ read(opt); if(opt==1){ read(p),read(q); p=find(id[p]),q=find(id[q]); if(p==q)continue; fa[p]=q; sum[q]+=sum[p]; size[q]+=size[p]; } else if(opt==2){ read(p),read(q); x=find(id[p]),y=find(id[q]); if(p==q)continue; id[p]=++tot; fa[id[p]]=y; sum[y]+=p,size[y]++; size[x]--,sum[x]-=p; } else{ read(p); x=find(id[p]); printf("%d %d\n",size[x],sum[x]); } } } return 0; }
|