#include<bits/stdc++.h>
#define pos(i,a,b) for(int i=(a);i<=(b);i++)
#define gc getchar()
using namespace std;
const int maxn=2e5+10;
int size[maxn],f[maxn],cnt=0,fa[maxn];
long long sum[maxn];
inline int read(){
int ans=0,f=1;
char ch=gc;
while(ch>'9'||ch<'0'){
if(ch=='-'){
f=-1;
}
ch=gc;
}
while(ch>='0'&&ch<='9'){
ans=ans*10+ch-'0';
ch=gc;
}
return ans*f;
}
int find(int x,int del=0){
if(x==-1)return -1;
if(del){
size[x]--;
sum[x]-=del;
}
if(x==f[x])return x;
int FX=find(f[x],del);
if(del)f[x]=FX;
return FX;
}
void merge(int x,int y){
if(fa[x]==-1){
if(fa[y]==-1){
++cnt;
fa[x]=fa[y]=f[cnt]=cnt;
size[cnt]=2;
sum[cnt]=x+y;
}
else{
int fy=find(fa[y]);
size[fy]++;
sum[fy]+=x;
fa[x]=fy;
}
}
else{
if(fa[y]==-1){
int fx=find(fa[x]);
size[fx]++;
sum[fx]+=y;
fa[y]=fx;
}
else{
int fx=find(fa[x]);
int fy=find(fa[y]);
if(fx==fy)return ;
if(fx<fy)swap(fx,fy);
f[fx]=fy;
size[fy]+=size[fx];
sum[fy]+=sum[fx];
}
}
}
void move(int x,int y){
int fx=find(fa[x],x);
if(fa[y]==-1){
++cnt;
fa[y]=f[cnt]=cnt;
size[cnt]=1;
sum[cnt]=y;
}
int fy=find(fa[y]);
sum[fy]+=x;
size[fy]++;
fa[x]=fy;
}
void query(int x){
int fx=find(fa[x]);
if(fx==-1){
printf("%d %d\n",1,x);
return ;
}
printf("%d %lld\n",size[fx],sum[fx]);
}
int n,m;
void do_main(){
memset(fa,-1,sizeof(fa));
cnt=0;
int opt,p,q;
pos(i,1,m){
opt=read();
if(opt==1){
p=read();q=read();
merge(p,q);
}
else if(opt==2){
p=read();q=read();
move(p,q);
}
else{
p=read();
query(p);
}
}
}
int main(){
while(scanf("%d%d",&n,&m)>0)do_main();
}
2021-09-16
最新推荐文章于 2021-10-09 18:35:31 发布