#include <bits/stdc++.h>
using namespace std;
int t, n, doc[100010], doc1[100010], p[100010];
vector<vector<int> > g;
vector<pair<int, pair<int, int> > > q;
vector<int> tin, tout;
int par(int u) {
  if (p[u] == u) return u;
  return p[u] = par(p[u]);
}
void dfs(int u) {
  tin[u] = t;
  for (int i = 0; i < g[u].size(); i++)
    if (tin[g[u][i]] == -1) {
      t++;
      dfs(g[u][i]);
    }
  tout[u] = ++t;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int m, d = 0;
  cin >> n >> m;
  g.resize(n);
  tin.resize(n, -1);
  tout.resize(n);
  for (int i = 0; i < n; i++) p[i] = i;
  for (int i = 0; i < m; i++) {
    int type, x, y;
    cin >> type;
    if (type == 1) {
      cin >> x >> y;
      x--;
      y--;
      p[x] = y;
      g[y].push_back(x);
    } else if (type == 2) {
      cin >> x;
      x--;
      doc1[d] = par(x);
      doc[d++] = x;
    } else {
      cin >> x >> y;
      x--;
      y--;
      int a = 0;
      if (par(x) == par(doc[y])) a = 1;
      q.push_back(make_pair(a, make_pair(x, y)));
    }
  }
  t = 0;
  for (int i = 0; i < n; i++)
    if (p[i] == i) dfs(i);
  for (int i = 0; i < q.size(); i++) {
    if (q[i].first == 0) {
      cout << "NO\n";
      continue;
    }
    int u = q[i].second.first, v = doc[q[i].second.second],
        x = doc1[q[i].second.second];
    if (tin[u] <= tin[v] && tout[u] >= tout[v] && tin[x] <= tin[u] &&
        tout[x] >= tout[u])
      cout << "YES\n";
    else
      cout << "NO\n";
  }
  return 0;
}
