#include <bits/stdc++.h>
using namespace std;
inline void scan(int &n) {
  register char c = 0;
  while (c < 33) c = getc(stdin);
  n = 0;
  while (c > 32) {
    n = n * 10 + (c - '0');
    c = getc(stdin);
  }
}
inline void scan(long long int &n) {
  register char c = 0;
  while (c < 33) c = getc(stdin);
  n = 0;
  while (c > 32) {
    n = n * 10LL + (c - '0');
    c = getc(stdin);
  }
}
vector<int> boss;
vector<int> repr;
vector<int> tim;
vector<int> repr_at;
int find(int v) {
  if (repr[v] == 0) return v;
  repr[v] = find(repr[v]);
  return repr[v];
}
int main() {
  ios_base::sync_with_stdio(0);
  int n, m, t;
  cin >> n >> m;
  boss.resize(n + 1, 0);
  repr.resize(n + 1, 0);
  while (m--) {
    cin >> t;
    if (t == 1) {
      int x, y;
      cin >> x >> y;
      boss[x] = y;
      repr[x] = y;
    }
    if (t == 2) {
      int x;
      cin >> x;
      tim.push_back(x);
      repr_at.push_back(find(x));
    }
    if (t == 3) {
      int x, i;
      cin >> x >> i;
      int cur = tim[i - 1], end = repr_at[i - 1];
      while (cur != end) {
        if (cur == x) break;
        cur = boss[cur];
      }
      if (cur == x)
        cout << "YES\n";
      else
        cout << "NO\n";
    }
  }
}
