#include <bits/stdc++.h>
using namespace std;
long long n, m, p[500500], pp[500500], t = 1;
vector<long long> s, f;
int fd(long long x) {
  if (x == pp[x]) {
    return x;
  }
  return pp[x] = fd(pp[x]);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    p[i] = i;
    pp[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    long long x;
    cin >> x;
    if (x == 1) {
      long long l, r;
      cin >> l >> r;
      p[l] = r;
      pp[l] = r;
    }
    if (x == 2) {
      long long r;
      cin >> r;
      s.push_back(r);
      f.push_back(fd(r));
    }
    if (x == 3) {
      long long r, y;
      cin >> r >> y;
      int st = s[y - 1], fn = f[y - 1];
      while (st != fn) {
        if (st == r) {
          break;
        }
        st = p[st];
      }
      if (st == r) {
        cout << "YES" << endl;
        continue;
      }
      cout << "NO" << endl;
    }
  }
}
