#include <bits/stdc++.h>
using namespace std;
long long n, m, par[100005][20], root[100005], h[100005], c[100005], a[100005],
    tmp;
pair<long long, pair<long long, long long> > p[100005];
long long ans[100005];
vector<long long> dsk[100005];
long long getRoot(long long u) {
  if (u == root[u]) return u;
  return root[u] = getRoot(root[u]);
}
void merge(long long u, long long v) { root[getRoot(u)] = getRoot(v); }
void input() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) root[i] = i;
  for (long long i = 1; i <= m; i++) {
    long long t;
    cin >> t;
    if (t == 2) {
      long long x;
      cin >> x;
      p[i] = make_pair(t, make_pair(x, -1));
    } else {
      long long x, y;
      cin >> x >> y;
      p[i] = make_pair(t, make_pair(x, y));
      if (t == 1) {
        dsk[x].push_back(y);
        dsk[y].push_back(x);
        c[x] = 1;
      }
    }
  }
}
void build(long long u, long long q) {
  h[u] = h[q] + 1;
  par[u][0] = q;
  for (long long i = 1; i <= 17; i++) {
    par[u][i] = par[par[u][i - 1]][i - 1];
  }
  for (long long i = 0; i < dsk[u].size(); i++) {
    if (dsk[u][i] != q) build(dsk[u][i], u);
  }
}
long long lca(long long x, long long y) {
  for (long long i = 17; i >= 0; i--) {
    if (h[par[y][i]] >= h[x]) {
      y = par[y][i];
    }
  }
  for (long long i = 17; i >= 0; i--) {
    if (par[x][i] != par[y][i]) {
      x = par[x][i];
      y = par[y][i];
    }
  }
  while (x != y) {
    x = par[x][0];
    y = par[y][0];
  }
  return x;
}
long long find(long long x) {
  for (long long i = 17; i >= 0; i--) {
    if (getRoot(par[x][i]) == getRoot(x)) {
      x = par[x][i];
    }
  }
  while (getRoot(x) == getRoot(par[x][0])) {
    x = par[x][0];
  }
  return x;
}
void solve() {
  for (long long i = 1; i <= n; i++)
    if (c[i] != 1) build(i, 0);
  for (long long i = 1; i <= m; i++) {
    if (p[i].first == 1) {
      merge(p[i].second.first, p[i].second.second);
    } else if (p[i].first == 2) {
      tmp++;
      a[tmp] = p[i].second.first;
      ans[tmp] = find(p[i].second.first);
    } else if (p[i].first == 3) {
      long long x = p[i].second.first, y = p[i].second.second;
      if (x == a[y] || x == ans[y]) {
        cout << "YES\n";
      } else if (h[x] > h[a[y]]) {
        cout << "NO\n";
      } else if (h[ans[y]] > h[x]) {
        cout << "NO\n";
      } else {
        long long e = lca(x, a[y]), f = lca(ans[y], x);
        if (e == x && f == ans[y])
          cout << "YES\n";
        else
          cout << "NO\n";
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  input();
  solve();
}
