#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-7;
const double PI = acos(-1);
const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
const int N = 5e5 + 7;
struct DSU {
  vector<int> fa;
  void init(int n) {
    fa = vector<int>(n + 1);
    iota(fa.begin(), fa.end(), 0);
  }
  int get(int s) { return s == fa[s] ? s : fa[s] = get(fa[s]); }
  int &operator[](int i) { return fa[get(i)]; }
  bool merge(int x, int y) {
    x = get(x);
    y = get(y);
    return x == y ? false : (fa[x] = y, true);
  }
};
struct BIT {
  static const int SIZE = N;
  long long bit1[SIZE], bit2[SIZE];
  int limit;
  void init(int n = SIZE - 1) {
    limit = n;
    for (int i = 1; i <= n; i++) bit1[i] = bit2[i] = 0;
  }
  BIT() { init(); }
  void add(long long *bit, int x, long long v) {
    while (x <= limit) bit[x] += v, x += x & -x;
  }
  long long query(long long *bit, int x) {
    long long res = 0;
    while (x) res += bit[x], x -= x & -x;
    return res;
  }
  void add(int x, long long v) { add(x, x, v); }
  void add(int l, int r, long long v) {
    add(bit1, l, v);
    add(bit2, l, v * l);
    add(bit1, r + 1, -v);
    add(bit2, r + 1, -v * (r + 1));
  }
  long long query(int x) { return query(x, x); }
  long long query(int l, int r) {
    return (r + 1) * query(bit1, r) - query(bit2, r) - l * query(bit1, l - 1) +
           query(bit2, l - 1);
  }
};
template <typename T>
struct HLD {
  int dfn;
  int fa[N], d[N], num[N], son[N], id[N], tp[N];
  vector<pair<int, int> > *e;
  BIT t_xor, t_sum;
  void build(vector<pair<int, int> > e[], const int &rt = 1) {
    this->e = e;
    fa[rt] = 0;
    dfs1(rt);
    dfs2(rt);
  }
  void dfs1(const int &u = 1) {
    d[u] = d[fa[u]] + 1;
    num[u] = 1;
    son[u] = 0;
    for (auto p : e[u]) {
      int v = p.first;
      if (v == fa[u]) continue;
      fa[v] = u;
      dfs1(v);
      num[u] += num[v];
      if (num[v] > num[son[u]]) son[u] = v;
    }
  }
  void dfs2(const int &u = 1) {
    tp[u] = son[fa[u]] == u ? tp[fa[u]] : u;
    id[u] = ++dfn;
    if (son[u]) dfs2(son[u]);
    for (auto p : e[u]) {
      int v = p.first;
      int w = p.second;
      if (v == fa[u]) continue;
      if (v != son[u]) dfs2(v);
      t_xor.add(id[v], w);
    }
  }
  void add(int x, int y, const T &k, const int &is_edge = 1) {
    while (tp[x] != tp[y]) {
      if (d[tp[x]] < d[tp[y]]) swap(x, y);
      t_sum.add(id[tp[x]], id[x], k);
      x = fa[tp[x]];
    }
    if (d[x] > d[y]) swap(x, y);
    t_sum.add(id[x], id[y], k);
    if (is_edge) t_sum.add(id[x], -k);
  }
  T query(int x, int y, int is_edge = 1) {
    T q_xor = 0, q_sum = 0;
    while (tp[x] != tp[y]) {
      if (d[tp[x]] < d[tp[y]]) swap(x, y);
      q_xor += t_xor.query(id[tp[x]], id[x]);
      q_sum += t_sum.query(id[tp[x]], id[x]);
      x = fa[tp[x]];
    }
    if (d[x] > d[y]) swap(x, y);
    q_xor += t_xor.query(id[x], id[y]);
    q_sum += t_sum.query(id[x], id[y]);
    if (is_edge) {
      q_xor -= t_xor.query(id[x]);
      q_sum -= t_sum.query(id[x]);
    }
    if (q_sum) return -1;
    return q_xor & 1;
  }
};
int n, q;
int u[N], v[N], w[N], res[N];
vector<int> qset[N];
vector<pair<int, int> > e[N];
DSU dsu;
HLD<int> tree;
void fuck(int i) {
  if (res[i] == -1) {
    int x = tree.query(u[i], v[i], 1);
    if (x == -1 || w[i] == x)
      res[i] = 0;
    else
      res[i] = 1, tree.add(u[i], v[i], 1, 1);
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n >> q;
  dsu.init(n);
  for (int i = 1; i <= q; ++i) {
    cin >> u[i] >> v[i] >> w[i];
    res[i] = -1;
    if (dsu.merge(u[i], v[i])) {
      res[i] = 1;
      e[u[i]].emplace_back(v[i], w[i]);
      e[v[i]].emplace_back(u[i], w[i]);
    }
  }
  for (int i = 1; i <= q; ++i) {
    assert(dsu[u[i]] == dsu[v[i]]);
    qset[dsu[u[i]]].emplace_back(i);
  }
  tree.t_xor.init(n);
  tree.t_sum.init(n);
  for (int i = 1; i <= n; ++i)
    if (qset[i].size()) {
      tree.build(e, i);
      for (int j : qset[i]) fuck(j);
    }
  for (int i = 1; i <= q; ++i) {
    cout << (res[i] ? "YES" : "NO") << '\n';
  }
  return 0;
}

### Prompt

Construct a Cpp code solution to the problem outlined:

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 double eps = 1e-7;
const double PI = acos(-1);
const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
const int N = 5e5 + 7;
struct DSU {
  vector<int> fa;
  void init(int n) {
    fa = vector<int>(n + 1);
    iota(fa.begin(), fa.end(), 0);
  }
  int get(int s) { return s == fa[s] ? s : fa[s] = get(fa[s]); }
  int &operator[](int i) { return fa[get(i)]; }
  bool merge(int x, int y) {
    x = get(x);
    y = get(y);
    return x == y ? false : (fa[x] = y, true);
  }
};
struct BIT {
  static const int SIZE = N;
  long long bit1[SIZE], bit2[SIZE];
  int limit;
  void init(int n = SIZE - 1) {
    limit = n;
    for (int i = 1; i <= n; i++) bit1[i] = bit2[i] = 0;
  }
  BIT() { init(); }
  void add(long long *bit, int x, long long v) {
    while (x <= limit) bit[x] += v, x += x & -x;
  }
  long long query(long long *bit, int x) {
    long long res = 0;
    while (x) res += bit[x], x -= x & -x;
    return res;
  }
  void add(int x, long long v) { add(x, x, v); }
  void add(int l, int r, long long v) {
    add(bit1, l, v);
    add(bit2, l, v * l);
    add(bit1, r + 1, -v);
    add(bit2, r + 1, -v * (r + 1));
  }
  long long query(int x) { return query(x, x); }
  long long query(int l, int r) {
    return (r + 1) * query(bit1, r) - query(bit2, r) - l * query(bit1, l - 1) +
           query(bit2, l - 1);
  }
};
template <typename T>
struct HLD {
  int dfn;
  int fa[N], d[N], num[N], son[N], id[N], tp[N];
  vector<pair<int, int> > *e;
  BIT t_xor, t_sum;
  void build(vector<pair<int, int> > e[], const int &rt = 1) {
    this->e = e;
    fa[rt] = 0;
    dfs1(rt);
    dfs2(rt);
  }
  void dfs1(const int &u = 1) {
    d[u] = d[fa[u]] + 1;
    num[u] = 1;
    son[u] = 0;
    for (auto p : e[u]) {
      int v = p.first;
      if (v == fa[u]) continue;
      fa[v] = u;
      dfs1(v);
      num[u] += num[v];
      if (num[v] > num[son[u]]) son[u] = v;
    }
  }
  void dfs2(const int &u = 1) {
    tp[u] = son[fa[u]] == u ? tp[fa[u]] : u;
    id[u] = ++dfn;
    if (son[u]) dfs2(son[u]);
    for (auto p : e[u]) {
      int v = p.first;
      int w = p.second;
      if (v == fa[u]) continue;
      if (v != son[u]) dfs2(v);
      t_xor.add(id[v], w);
    }
  }
  void add(int x, int y, const T &k, const int &is_edge = 1) {
    while (tp[x] != tp[y]) {
      if (d[tp[x]] < d[tp[y]]) swap(x, y);
      t_sum.add(id[tp[x]], id[x], k);
      x = fa[tp[x]];
    }
    if (d[x] > d[y]) swap(x, y);
    t_sum.add(id[x], id[y], k);
    if (is_edge) t_sum.add(id[x], -k);
  }
  T query(int x, int y, int is_edge = 1) {
    T q_xor = 0, q_sum = 0;
    while (tp[x] != tp[y]) {
      if (d[tp[x]] < d[tp[y]]) swap(x, y);
      q_xor += t_xor.query(id[tp[x]], id[x]);
      q_sum += t_sum.query(id[tp[x]], id[x]);
      x = fa[tp[x]];
    }
    if (d[x] > d[y]) swap(x, y);
    q_xor += t_xor.query(id[x], id[y]);
    q_sum += t_sum.query(id[x], id[y]);
    if (is_edge) {
      q_xor -= t_xor.query(id[x]);
      q_sum -= t_sum.query(id[x]);
    }
    if (q_sum) return -1;
    return q_xor & 1;
  }
};
int n, q;
int u[N], v[N], w[N], res[N];
vector<int> qset[N];
vector<pair<int, int> > e[N];
DSU dsu;
HLD<int> tree;
void fuck(int i) {
  if (res[i] == -1) {
    int x = tree.query(u[i], v[i], 1);
    if (x == -1 || w[i] == x)
      res[i] = 0;
    else
      res[i] = 1, tree.add(u[i], v[i], 1, 1);
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n >> q;
  dsu.init(n);
  for (int i = 1; i <= q; ++i) {
    cin >> u[i] >> v[i] >> w[i];
    res[i] = -1;
    if (dsu.merge(u[i], v[i])) {
      res[i] = 1;
      e[u[i]].emplace_back(v[i], w[i]);
      e[v[i]].emplace_back(u[i], w[i]);
    }
  }
  for (int i = 1; i <= q; ++i) {
    assert(dsu[u[i]] == dsu[v[i]]);
    qset[dsu[u[i]]].emplace_back(i);
  }
  tree.t_xor.init(n);
  tree.t_sum.init(n);
  for (int i = 1; i <= n; ++i)
    if (qset[i].size()) {
      tree.build(e, i);
      for (int j : qset[i]) fuck(j);
    }
  for (int i = 1; i <= q; ++i) {
    cout << (res[i] ? "YES" : "NO") << '\n';
  }
  return 0;
}

```