#include <bits/stdc++.h>
using namespace std;
int p[100010], n, m, x, y, t, P[100010];
vector<int> d, p2;
int parent(int x) {
  if (p[x] == x)
    return x;
  else
    return p[x] = parent(p[x]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) p[i] = i;
  for (int i = 1; i <= m; i++) {
    cin >> t;
    if (t == 1) {
      cin >> x >> y;
      p[x] = y;
      P[x] = y;
    } else if (t == 2) {
      cin >> x;
      d.push_back(x);
      p2.push_back(parent(x));
    } else {
      cin >> x >> y;
      int a = d[y - 1], b = p2[y - 1];
      while (a != b) {
        if (a == x) break;
        a = P[a];
      }
      if (a == x)
        cout << "YES\n";
      else
        cout << "NO\n";
    }
  }
  return 0;
}
