#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int inf = 1000000007;
const long long linf = 1000000000000000000LL;
const unsigned long long ulinf = (1ULL << 63) - 10ULL;
const double eps = 0.000001;
const double pi = 3.14159265358979323846;
template <class T>
T abs(T a) {
  return a >= 0 ? a : -a;
}
template <class T>
T sqr(T a) {
  return a * a;
}
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <class T>
T mod(T a, T b) {
  return (a % b + b) % b;
}
template <class T>
T lowbit(T x) {
  return x & -x;
}
long long addmod(long long a, long long b, long long c) {
  return ((a + b) % c + c) % c;
}
long long mulmod(long long a, long long b, long long c) {
  if (b == 0LL) return 0LL;
  long long ret = mulmod(a, b >> 1, c);
  ret = (ret + ret) % c;
  if (b & 1LL) ret = (ret + a) % c;
  return ret;
}
long long powmod(long long a, long long b, long long c) {
  if (b == 0LL) return 1LL;
  long long ret = powmod(a, b >> 1, c);
  ret = ret * ret % c;
  if (b & 1LL) ret = ret * a % c;
  return ret;
}
long long modinv(long long a, long long b) { return powmod(a, b - 2LL, b); }
template <class T>
void maxe(T &a, T b) {
  if (a < b) a = b;
}
template <class T>
void mine(T &a, T b) {
  if (a > b) a = b;
}
int iszero(double a) { return abs(a) <= eps; }
template <class T>
void geti(T &a) {
  a = 0;
  T b = 1;
  char c = getchar();
  if (c == '-')
    b = -1;
  else
    a = c - 48;
  while ((c = getchar()) != ' ' && c != '\n') a = a * 10 + c - 48;
  a *= b;
}
void fileio_in_out() {
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
}
void fileio_txt() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
const int N = 1111111;
const int M = 27;
const int K = 33;
const long long md = 10000007;
int test;
int n, m, k, ans;
int f[N];
vector<pair<int, int> > qry;
int from[N], to[N];
vector<int> v[N];
int dep[N];
int anc[N][M];
void dfs(int fa, int cur, int depth) {
  dep[cur] = depth;
  anc[cur][0] = fa;
  for (int i = 1; i < M; i++) anc[cur][i] = anc[anc[cur][i - 1]][i - 1];
  for (int i = 0, sz = v[cur].size(); i < sz; i++) {
    int next = v[cur][i];
    if (next != fa) dfs(cur, next, depth + 1);
  }
}
int lca(int x, int y) {
  if (dep[x] > dep[y]) swap(x, y);
  if (dep[x] < dep[y]) {
    int t = dep[y] - dep[x];
    for (int i = 0; i < M; i++)
      if (t & (1 << i)) y = anc[y][i];
  }
  if (x != y) {
    for (int i = M - 1; i >= 0; i--)
      if (anc[x][i] != anc[y][i]) {
        x = anc[x][i];
        y = anc[y][i];
      }
    x = anc[x][0];
    y = anc[y][0];
  }
  return x;
}
int ff(int x) { return f[x] == x ? x : f[x] = ff(f[x]); }
int check(int x, int y, int z) { return lca(x, y) == y && lca(y, z) == z; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) f[i] = i;
  for (int i = 1; i <= m; i++) {
    int t, x, y;
    scanf("%d", &t);
    if (t == 1) {
      scanf("%d%d", &x, &y);
      v[x].push_back(y);
      v[y].push_back(x);
      int fx = ff(x), fy = ff(y);
      f[fx] = fy;
    } else if (t == 2) {
      scanf("%d", &x);
      y = ff(x);
      k++;
      from[k] = x;
      to[k] = y;
    } else {
      scanf("%d%d", &x, &y);
      qry.push_back(make_pair(x, y));
    }
  }
  for (int i = 1; i <= n; i++)
    if (ff(i) == i) dfs(i, i, 0);
  for (int i = 0; i < qry.size(); i++) {
    int y = qry[i].first, id = qry[i].second;
    int x = from[id], z = to[id];
    puts(check(x, y, z) ? "YES" : "NO");
  }
  return 0;
}
