#include<iostream> #include<stack> using namespace std; const int maxn = 3e5 + 7; int n, m, t, cnt1, cnt2; int p[maxn], sz[maxn]; struct node { int child;// 被修改的子节点 int parent;// 子节点原来的父节点 int sz_child;// 子节点原来的size }; stack<node> s; void Initial(int n) { for (int i = 1; i <= n; i++) { p[i] = i; sz[i] = i; } } int Find(int x) { if (p[x] == x) { return x; } else { return Find(p[x]); } } void Union(int x, int y) { int rootx = Find(x); int rooty = Find(y); if (rootx == rooty) { s.push({ 0, 0, 0 });//没有实际修改 return; } if (sz[rootx] > sz[rooty]) { swap(rootx, rooty); } s.push({ rootx, p[rootx], sz[rootx] });//记录修改信息 p[rootx] = rooty; sz[rooty] += sz[rootx]; } void Disconnect() { if (!s.empty()) { node last = s.top(); s.pop(); if (last.child != 0) { p[last.child] = last.parent; sz[last.child] = last.sz_child; int parent = Find(last.parent); sz[parent] -= last.sz_child; } } } int main() { cin >> n >> m; Initial(n); while (m—) { int t; cin >> t; if (t == 1) { int x, y; cin >> x >> y; Union(x, y); } else if (t == 2) { Disconnect(); } else { int x, y; cin >> x >> y; cout << (Find(x) == Find(y) ? “Yes\n” : “No\n”); } } return 0; }