#include <bits/stdc++.h>
using namespace std;
template <typename T>
class BinaryIndexedTree {
 private:
  static const int N = 1 << 20;
  int n;
  int c[N];

 public:
  void Build(int n) {
    this->n = n;
    for (int i = 0; i <= n; i++) c[i] = 0;
  }
  void Add(int x, int v) {
    for (x++; x <= n; x += x & -x) c[x] += v;
  }
  T Sum(int x) {
    T ret = 0;
    for (x++; x; x -= x & -x) ret += c[x];
    return ret;
  }
};
class DisjointSet {
 private:
  int n;
  vector<int> pa;

 public:
  void Build(int n) {
    this->n = n;
    pa.resize(n);
    for (int i = 0; i < n; i++) pa[i] = i;
  }
  int Find(int x) { return x == pa[x] ? x : pa[x] = Find(pa[x]); }
  bool Union(int x, int y) {
    int fx = Find(x), fy = Find(y);
    if (fx == fy) return false;
    pa[fx] = fy;
    return true;
  }
};
const int N = 3e5, Q = 5e5;
int n;
vector<pair<int, int> > G[N];
vector<pair<int, int> > queries[N];
int f[N];
int pa[N], pre[N], post[N];
int dfs_clock;
DisjointSet ds;
int u[Q], v[Q], x[Q];
int lca[Q];
bool ans[Q];
BinaryIndexedTree<int> bit;
void dfs(int u, int fa = -1) {
  pre[u] = dfs_clock++;
  for (auto [v, qid] : G[u]) {
    if (v == fa) continue;
    f[v] = f[u] ^ x[qid];
    pa[v] = u;
    dfs(v, u);
    ds.Union(v, u);
  }
  for (auto [v, qid] : queries[u]) {
    int fv = ds.Find(v);
    if (v != fv) lca[qid] = fv;
  }
  post[u] = dfs_clock;
}
int main() {
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  ds.Build(n);
  for (int i = 0; i < q; i++) {
    cin >> u[i] >> v[i] >> x[i];
    u[i]--, v[i]--;
    if (ds.Union(u[i], v[i])) {
      ans[i] = true;
      G[u[i]].emplace_back(v[i], i);
      G[v[i]].emplace_back(u[i], i);
    } else {
      queries[u[i]].emplace_back(v[i], i);
      queries[v[i]].emplace_back(u[i], i);
    }
  }
  ds.Build(n);
  for (int i = 0; i < n; i++) pre[i] = -1;
  for (int i = 0; i < n; i++)
    if (pre[i] == -1) {
      pa[i] = -1;
      dfs(i);
      f[i] = 0;
    }
  bit.Build(n + 1);
  for (int i = 0; i < q; i++) {
    if (ans[i]) continue;
    int U = u[i], V = v[i];
    if ((x[i] ^ f[U] ^ f[V]) != 1) continue;
    int LCA = lca[i];
    int s = bit.Sum(pre[U]) + bit.Sum(pre[V]) - 2 * bit.Sum(pre[LCA]);
    if (s > 0) continue;
    ans[i] = true;
    for (int st : {U, V}) {
      for (int i = st; i != LCA; i = pa[i]) {
        bit.Add(pre[i], +1);
        bit.Add(post[i], -1);
      }
    }
  }
  for (int i = 0; i < q; i++) puts(ans[i] ? "YES" : "NO");
}

### Prompt

Please create a solution in Cpp to 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;
template <typename T>
class BinaryIndexedTree {
 private:
  static const int N = 1 << 20;
  int n;
  int c[N];

 public:
  void Build(int n) {
    this->n = n;
    for (int i = 0; i <= n; i++) c[i] = 0;
  }
  void Add(int x, int v) {
    for (x++; x <= n; x += x & -x) c[x] += v;
  }
  T Sum(int x) {
    T ret = 0;
    for (x++; x; x -= x & -x) ret += c[x];
    return ret;
  }
};
class DisjointSet {
 private:
  int n;
  vector<int> pa;

 public:
  void Build(int n) {
    this->n = n;
    pa.resize(n);
    for (int i = 0; i < n; i++) pa[i] = i;
  }
  int Find(int x) { return x == pa[x] ? x : pa[x] = Find(pa[x]); }
  bool Union(int x, int y) {
    int fx = Find(x), fy = Find(y);
    if (fx == fy) return false;
    pa[fx] = fy;
    return true;
  }
};
const int N = 3e5, Q = 5e5;
int n;
vector<pair<int, int> > G[N];
vector<pair<int, int> > queries[N];
int f[N];
int pa[N], pre[N], post[N];
int dfs_clock;
DisjointSet ds;
int u[Q], v[Q], x[Q];
int lca[Q];
bool ans[Q];
BinaryIndexedTree<int> bit;
void dfs(int u, int fa = -1) {
  pre[u] = dfs_clock++;
  for (auto [v, qid] : G[u]) {
    if (v == fa) continue;
    f[v] = f[u] ^ x[qid];
    pa[v] = u;
    dfs(v, u);
    ds.Union(v, u);
  }
  for (auto [v, qid] : queries[u]) {
    int fv = ds.Find(v);
    if (v != fv) lca[qid] = fv;
  }
  post[u] = dfs_clock;
}
int main() {
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  ds.Build(n);
  for (int i = 0; i < q; i++) {
    cin >> u[i] >> v[i] >> x[i];
    u[i]--, v[i]--;
    if (ds.Union(u[i], v[i])) {
      ans[i] = true;
      G[u[i]].emplace_back(v[i], i);
      G[v[i]].emplace_back(u[i], i);
    } else {
      queries[u[i]].emplace_back(v[i], i);
      queries[v[i]].emplace_back(u[i], i);
    }
  }
  ds.Build(n);
  for (int i = 0; i < n; i++) pre[i] = -1;
  for (int i = 0; i < n; i++)
    if (pre[i] == -1) {
      pa[i] = -1;
      dfs(i);
      f[i] = 0;
    }
  bit.Build(n + 1);
  for (int i = 0; i < q; i++) {
    if (ans[i]) continue;
    int U = u[i], V = v[i];
    if ((x[i] ^ f[U] ^ f[V]) != 1) continue;
    int LCA = lca[i];
    int s = bit.Sum(pre[U]) + bit.Sum(pre[V]) - 2 * bit.Sum(pre[LCA]);
    if (s > 0) continue;
    ans[i] = true;
    for (int st : {U, V}) {
      for (int i = st; i != LCA; i = pa[i]) {
        bit.Add(pre[i], +1);
        bit.Add(post[i], -1);
      }
    }
  }
  for (int i = 0; i < q; i++) puts(ans[i] ? "YES" : "NO");
}

```