类似于并查集一个ne数组一个pre数组,不能按秩合并或者路径压缩
#include<unordered_set>
#include<unordered_map>
#include<functional>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<iterator>
#include<cstring>
#include<numeric>
#include<cstdio>
#include<vector>
#include<bitset>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
#include<deque>
using namespace std;
//================================
#define debug(a) cout << #a": " << a << endl;
#define rep(i, ll,rr) for(int i = ll; i <= rr; ++i)
#define N 100010
//================================
typedef pair<int,int> pii;
#define x first
#define y second
typedef long long LL; typedef unsigned long long ULL; typedef long double LD;
inline LL read() { LL s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; }
inline void print(LL x, int op = 10) { if (!x) { putchar('0'); if (op) putchar(op); return; } char F[40]; LL tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); if (op) putchar(op); }
LL fpower(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; } LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
//=================================
int n,q,ne[N],pre[N],id,x,y;
int find_ne(int x){
return ne[x]==x?ne[x]:find_ne(ne[x]);
}
int find_pre(int x){
return pre[x] == x?pre[x]:find_pre(pre[x]);
}
//=================================
int main(){
n = read(), q = read();
rep(i,1,n) ne[i] = i,pre[i] = i;
rep(i,1,q){
id = read();
if(id==1){
x = read(),y = read();
ne[y]=x;
pre[x]=y;
}
else if(id==2){
x = read(),y = read();
ne[y] = y;
pre[x]=x;
}
else{
x = read();
x = find_ne(x);
queue<int> Q;
while(Q.size()) Q.pop();
int p = x;
while(p!=pre[p]){
Q.push(p);
p = pre[p];
}
Q.push(p);
printf("%d ",Q.size());
while(Q.size()){
printf("%d ",Q.front());
Q.pop();
}
puts("");
}
}
return 0;
}