#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int parent[maxn];
int root[maxn];
int koji[maxn];
vector<pair<int, pair<int, int> > > unos;
vector<int> graf[maxn];
vector<int> upit[maxn];
vector<int> rj[maxn];
int val[maxn];
int t;
int dt[maxn];
int ft[maxn];
void dfs(int cvor, int otac) {
  dt[cvor] = t;
  for (int i = 0; i < graf[cvor].size(); i++) {
    if (graf[cvor][i] != otac) {
      t++;
      dfs(graf[cvor][i], cvor);
    }
  }
  ft[cvor] = t;
}
int find(int x) {
  if (x == parent[x]) return x;
  return parent[x] = find(parent[x]);
}
int main() {
  int n, q;
  cin >> n >> q;
  pair<int, pair<int, int> > kita = make_pair(0, pair<int, int>(0, 0));
  unos.insert(unos.begin(), maxn + 10, kita);
  int ind = 1;
  for (int i = 0; i < q; i++) {
    scanf("%d", &unos[i].first);
    if (unos[i].first == 1) {
      scanf("%d%d", &unos[i].second.first, &unos[i].second.second);
      graf[unos[i].second.first].push_back(unos[i].second.second);
      graf[unos[i].second.second].push_back(unos[i].second.first);
      root[unos[i].second.first] = -1;
    }
    if (unos[i].first == 2) {
      scanf("%d", &unos[i].second.first);
      koji[ind] = unos[i].second.first;
      ind++;
    }
    if (unos[i].first == 3) {
      scanf("%d%d", &unos[i].second.first, &unos[i].second.second);
      upit[unos[i].second.second].push_back(unos[i].second.first);
      rj[unos[i].second.second].push_back(0);
    }
  }
  for (int i = 1; i <= n; i++)
    if (!root[i]) {
      t++;
      dfs(i, -1);
    }
  ind = 1;
  for (int i = 1; i <= n; i++) parent[i] = i;
  for (int i = 0; i < q; i++) {
    if (unos[i].first == 1)
      parent[unos[i].second.first] = find(unos[i].second.second);
    if (unos[i].first == 2) {
      int dijete = koji[ind];
      for (int j = 0; j < upit[ind].size(); j++) {
        int otac = upit[ind][j];
        if (dt[otac] <= dt[dijete] && ft[otac] >= dt[dijete] &&
            find(dijete) == find(otac))
          rj[ind][j] = 1;
      }
      ind++;
    }
    if (unos[i].first == 3) {
      int index = unos[i].second.second;
      if (rj[index][val[index]])
        printf("YES\n");
      else
        printf("NO\n");
      val[index]++;
    }
  }
  return 0;
}
