#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
constexpr int MOD = 1000000007;
constexpr ll INF = (1LL << 61) - 1;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
void fail(T v) {
  cout << v << endl;
  exit(0);
}
int idx = 0;
vector<int> p, g[101010], root, L, R;
void EulerTour(int i) {
  L[i] = idx++;
  for (int e : g[i]) {
    EulerTour(e);
  }
  R[i] = idx++;
}
int find(int x) {
  if (p[x] == x) return x;
  return p[x] = find(p[x]);
}
void solve() {
  int N, M;
  cin >> N >> M;
  p.assign(N, 0);
  for (int i = 0; i < (int)(N); ++i) p[i] = i;
  vector<int> chi;
  vector<pair<int, int>> qry;
  while (M--) {
    int t;
    cin >> t;
    if (t == 1) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      p[x] = y;
      g[y].push_back(x);
    } else if (t == 2) {
      int x;
      cin >> x;
      x--;
      chi.push_back(x);
      root.push_back(find(x));
    } else {
      int x, i;
      cin >> x >> i;
      x--;
      i--;
      qry.push_back({x, i});
    }
  }
  L.assign(N, -1);
  R.assign(N, -1);
  for (int i = 0; i < (int)(N); ++i)
    if (!~L[i] && find(i) == i) {
      EulerTour(i);
    }
  for (auto q : qry) {
    auto [x, i] = q;
    if (L[root[i]] <= L[x] && L[x] <= L[chi[i]] && R[x] > L[chi[i]])
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  solve();
  return 0;
}
