#include <bits/stdc++.h>
using namespace std;
const long long N = 8e5 + 10, inf = 0x3f3f3f3f, llinf = 0x3f3f3f3f3f3f3f3f;
const int M = 2e7 + 5;
long long n, m, q;
long long nn;
int a[N];
struct LCT {
  int c[N][2], fa[N], s[N], r[N], st[N], cir[N], tcir[N];
  inline int lc(int x) { return c[x][0]; }
  inline int rc(int x) { return c[x][1]; }
  void pushup(int x) {
    s[x] = s[lc(x)] ^ s[rc(x)] ^ a[x];
    cir[x] = cir[lc(x)] | cir[rc(x)] | tcir[x];
  }
  void pushr(int x) {
    swap(c[x][0], c[x][1]);
    r[x] ^= 1;
  }
  void pushdown(int x) {
    if (r[x]) {
      if (lc(x)) pushr(lc(x));
      if (rc(x)) pushr(rc(x));
      r[x] = 0;
    }
  }
  int nroot(int x) { return lc(fa[x]) == x || rc(fa[x]) == x; }
  void rotate(int x) {
    int y = fa[x], z = fa[y];
    int ys = c[y][1] == x, zs = c[z][1] == y, w = c[x][!ys];
    if (nroot(y)) c[z][zs] = x;
    c[x][!ys] = y;
    c[y][ys] = w;
    if (w) fa[w] = y;
    fa[x] = z;
    fa[y] = x;
    pushup(x);
    pushup(y);
  }
  void splay(int x) {
    int y = x, tot = 0;
    st[++tot] = y;
    while (nroot(y)) {
      st[++tot] = fa[y];
      y = fa[y];
    }
    while (tot) pushdown(st[tot--]);
    while (nroot(x)) {
      int y = fa[x], z = fa[y];
      if (nroot(y)) {
        rotate((c[y][0] == x) ^ (c[z][0] == y) ? x : y);
      }
      rotate(x);
    }
    pushup(x);
  }
  void access(int x) {
    for (int y = 0; x; y = x, x = fa[x]) {
      splay(x);
      c[x][1] = y;
      pushup(x);
    }
  }
  int findroot(int x) {
    access(x);
    splay(x);
    while (lc(x)) {
      pushdown(x);
      x = lc(x);
    }
    splay(x);
    return x;
  }
  void makeroot(int x) {
    access(x);
    splay(x);
    pushr(x);
  }
  void split(int x, int y) {
    makeroot(x);
    access(y);
    splay(y);
  }
  void link(int x, int y) {
    makeroot(x);
    if (findroot(y) != x) fa[x] = y;
  }
  void cut(int x, int y) {
    makeroot(x);
    if (findroot(y) == x && fa[y] == x && !lc(y)) {
      fa[y] = c[x][1] = 0;
      pushup(x);
    }
  }
  void update(int x) {
    if (x > n) {
      tcir[x] = 1;
    }
    if (lc(x)) update(lc(x));
    if (rc(x)) update(rc(x));
    pushup(x);
  }
  int work(int x, int y, int z) {
    if (findroot(x) != findroot(y)) {
      ++nn;
      a[nn] = z;
      link(x, nn);
      link(nn, y);
      return 1;
    } else {
      split(x, y);
      if ((s[y] ^ z) == 1 && !cir[y]) {
        splay(x);
        update(x);
        return 1;
      } else {
        return 0;
      }
    }
  }
} lct;
void solve() {
  cin >> n >> q;
  nn = n;
  for (int i = 1; i <= q; ++i) {
    int u, v, x;
    cin >> u >> v >> x;
    if (lct.work(u, v, x)) {
      cout << ((1) ? "YES" : "NO") << '\n';
    } else {
      cout << ((0) ? "YES" : "NO") << '\n';
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ;
  int tt = 1;
  while (tt--) {
    solve();
  }
}

### Prompt

In cpp, your task is to solve the following problem:

You have an undirected graph consisting of n vertices with weighted edges.

A simple cycle is a cycle of the graph without repeated vertices. Let the weight of the cycle be the [XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of weights of edges it consists of.

Let's say the graph is good if all its simple cycles have weight 1. A graph is bad if it's not good.

Initially, the graph is empty. Then q queries follow. Each query has the next type: 

  * u v x — add edge between vertices u and v of weight x if it doesn't make the graph bad. 



For each query print, was the edge added or not.

Input

The first line contains two integers n and q (3 ≤ n ≤ 3 ⋅ 10^5; 1 ≤ q ≤ 5 ⋅ 10^5) — the number of vertices and queries.

Next q lines contain queries — one per line. Each query contains three integers u, v and x (1 ≤ u, v ≤ n; u ≠ v; 0 ≤ x ≤ 1) — the vertices of the edge and its weight.

It's guaranteed that there are no multiple edges in the input.

Output

For each query, print YES if the edge was added to the graph, or NO otherwise (both case-insensitive).

Example

Input


9 12
6 1 0
1 3 1
3 6 0
6 2 0
6 4 1
3 4 1
2 4 0
2 5 0
4 5 0
7 8 1
8 9 1
9 7 0


Output


YES
YES
YES
YES
YES
NO
YES
YES
NO
YES
YES
NO

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 8e5 + 10, inf = 0x3f3f3f3f, llinf = 0x3f3f3f3f3f3f3f3f;
const int M = 2e7 + 5;
long long n, m, q;
long long nn;
int a[N];
struct LCT {
  int c[N][2], fa[N], s[N], r[N], st[N], cir[N], tcir[N];
  inline int lc(int x) { return c[x][0]; }
  inline int rc(int x) { return c[x][1]; }
  void pushup(int x) {
    s[x] = s[lc(x)] ^ s[rc(x)] ^ a[x];
    cir[x] = cir[lc(x)] | cir[rc(x)] | tcir[x];
  }
  void pushr(int x) {
    swap(c[x][0], c[x][1]);
    r[x] ^= 1;
  }
  void pushdown(int x) {
    if (r[x]) {
      if (lc(x)) pushr(lc(x));
      if (rc(x)) pushr(rc(x));
      r[x] = 0;
    }
  }
  int nroot(int x) { return lc(fa[x]) == x || rc(fa[x]) == x; }
  void rotate(int x) {
    int y = fa[x], z = fa[y];
    int ys = c[y][1] == x, zs = c[z][1] == y, w = c[x][!ys];
    if (nroot(y)) c[z][zs] = x;
    c[x][!ys] = y;
    c[y][ys] = w;
    if (w) fa[w] = y;
    fa[x] = z;
    fa[y] = x;
    pushup(x);
    pushup(y);
  }
  void splay(int x) {
    int y = x, tot = 0;
    st[++tot] = y;
    while (nroot(y)) {
      st[++tot] = fa[y];
      y = fa[y];
    }
    while (tot) pushdown(st[tot--]);
    while (nroot(x)) {
      int y = fa[x], z = fa[y];
      if (nroot(y)) {
        rotate((c[y][0] == x) ^ (c[z][0] == y) ? x : y);
      }
      rotate(x);
    }
    pushup(x);
  }
  void access(int x) {
    for (int y = 0; x; y = x, x = fa[x]) {
      splay(x);
      c[x][1] = y;
      pushup(x);
    }
  }
  int findroot(int x) {
    access(x);
    splay(x);
    while (lc(x)) {
      pushdown(x);
      x = lc(x);
    }
    splay(x);
    return x;
  }
  void makeroot(int x) {
    access(x);
    splay(x);
    pushr(x);
  }
  void split(int x, int y) {
    makeroot(x);
    access(y);
    splay(y);
  }
  void link(int x, int y) {
    makeroot(x);
    if (findroot(y) != x) fa[x] = y;
  }
  void cut(int x, int y) {
    makeroot(x);
    if (findroot(y) == x && fa[y] == x && !lc(y)) {
      fa[y] = c[x][1] = 0;
      pushup(x);
    }
  }
  void update(int x) {
    if (x > n) {
      tcir[x] = 1;
    }
    if (lc(x)) update(lc(x));
    if (rc(x)) update(rc(x));
    pushup(x);
  }
  int work(int x, int y, int z) {
    if (findroot(x) != findroot(y)) {
      ++nn;
      a[nn] = z;
      link(x, nn);
      link(nn, y);
      return 1;
    } else {
      split(x, y);
      if ((s[y] ^ z) == 1 && !cir[y]) {
        splay(x);
        update(x);
        return 1;
      } else {
        return 0;
      }
    }
  }
} lct;
void solve() {
  cin >> n >> q;
  nn = n;
  for (int i = 1; i <= q; ++i) {
    int u, v, x;
    cin >> u >> v >> x;
    if (lct.work(u, v, x)) {
      cout << ((1) ? "YES" : "NO") << '\n';
    } else {
      cout << ((0) ? "YES" : "NO") << '\n';
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ;
  int tt = 1;
  while (tt--) {
    solve();
  }
}

```