#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3fffffff;
int n, m;
int fa[200005], vis[200005], ans[200005];
vector<int> v[200005];
vector<pair<int, int> > q[200005];
struct node {
  int st, ed;
} rang[200005], tmp;
int findfa(int a) {
  if (fa[a] == a)
    return a;
  else
    return fa[a] = findfa(fa[a]);
}
void dfs(int u) {
  vis[u] = 1;
  for (int i = 0; i < v[u].size(); i++) dfs(v[u][i]);
  for (int i = 0; i < q[u].size(); i++) {
    int v = q[u][i].first, id = q[u][i].second;
    if (vis[v]) ans[id]++;
  }
  vis[u] = 0;
}
int main() {
  while (scanf("%d %d", &n, &m) != EOF) {
    memset(ans, 0, sizeof(ans));
    int op, x, y, pos = 1, K = 1;
    for (int i = 0; i <= n; i++) fa[i] = i, v[i].clear();
    for (int i = 1; i <= m; i++) {
      scanf("%d", &op);
      if (op == 1) {
        scanf("%d %d", &x, &y);
        fa[x] = y;
        v[y].push_back(x);
      } else if (op == 2) {
        scanf("%d", &x);
        rang[pos].st = x;
        rang[pos++].ed = findfa(x);
      } else {
        scanf("%d %d", &x, &y);
        pair<int, int> u = make_pair(rang[y].ed, rang[y].st);
        q[x].push_back(make_pair(u.first, K));
        q[u.second].push_back(make_pair(x, K));
        K++;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (fa[i] == i) dfs(i);
    }
    for (int i = 1; i < K; i++) {
      if (ans[i] == 2)
        printf("YES\n");
      else
        printf("NO\n");
    }
  }
  return 0;
}
