#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int par[maxn], boss[maxn];
int get_par(int v) {
  if (v == par[v]) return v;
  par[v] = get_par(par[v]);
  return par[v];
}
vector<pair<int, int>> check;
int main() {
  int m, n;
  cin >> n >> m;
  for (int i = 0; i < maxn; i++) {
    par[i] = i;
  }
  for (int i = 0; i < m; i++) {
    int t;
    cin >> t;
    if (t == 1) {
      int p, q;
      cin >> p >> q;
      boss[p] = q;
      par[p] = q;
    } else if (t == 2) {
      int p;
      cin >> p;
      check.push_back({p, get_par(p)});
    } else {
      int p, q;
      cin >> p >> q;
      int l = check[q - 1].second;
      int r = check[q - 1].first;
      if (r == p) {
        cout << "YES" << endl;
      } else {
        bool ans = 0;
        while (r != l) {
          if (r == p) {
            break;
          }
          r = boss[r];
        }
        if (r == p)
          cout << "YES" << endl;
        else
          cout << "NO" << endl;
      }
    }
  }
}
