#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int b[maxn], s[maxn];
vector<int> d, p;
int n, m;
int get_par(int x) {
  if (s[x] == 0)
    return x;
  else {
    s[x] = get_par(s[x]);
    return s[x];
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int t;
    cin >> t;
    if (t == 1) {
      int x, y;
      cin >> x >> y;
      b[x] = y;
      s[x] = y;
    } else if (t == 2) {
      int x;
      cin >> x;
      d.push_back(x);
      p.push_back(get_par(x));
    } else {
      int x, i;
      cin >> x >> i;
      int y = d[i - 1], z = p[i - 1];
      while (y != z) {
        if (y == x) {
          break;
        }
        y = b[y];
      }
      if (y == x)
        cout << "YES" << endl;
      else
        cout << "NO" << endl;
    }
  }
}
