#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
ld const PI = 4 * atan((ld)1);
constexpr int MAX = 3e5 + 69;
int n, q;
int ds[MAX];
vector<ii> adj[MAX];
inline int frep(int x) { return ds[x] == x ? x : ds[x] = frep(ds[x]); }
inline void join(int x, int y) { ds[frep(x)] = frep(y); }
bool vst[MAX];
int XOR[MAX], depth[MAX], par[MAX], head[MAX], heavy[MAX], pos[MAX], sz[MAX],
    st[2 * MAX];
inline void dfs_sz(int node) {
  vst[node] = true;
  sz[node] = 1;
  int sz_max = 0;
  for (auto [to, w] : adj[node]) {
    if (to != par[node]) {
      par[to] = node;
      depth[to] = depth[node] + 1;
      XOR[to] = XOR[node] ^ w;
      dfs_sz(to);
      sz[node] += sz[to];
      if (sz[to] > sz_max) sz_max = sz[to], heavy[node] = to;
    }
  }
}
static int cur_pos = 0;
inline void dfs_hld(int node, int h) {
  vst[node] = true;
  head[node] = h;
  pos[node] = cur_pos++;
  if (heavy[node] != -1) dfs_hld(heavy[node], h);
  for (auto [to, w] : adj[node])
    if (to != heavy[node] && to != par[node]) dfs_hld(to, to);
}
inline int lca(int u, int v) {
  for (; head[u] != head[v]; u = par[head[u]]) {
    if (depth[head[u]] < depth[head[v]]) swap(u, v);
  }
  return depth[u] < depth[v] ? u : v;
}
inline void modify(int p, int val) {
  for (st[p += n] = val; p > 1; p >>= 1) st[p >> 1] = max(st[p], st[p ^ 1]);
}
inline int rmq(int l, int r) {
  int res = 0;
  for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
    if (l & 1) res = max(res, st[l++]);
    if (r & 1) res = max(res, st[--r]);
  }
  return res;
}
inline int get(int u, int lc) {
  int res = 0;
  for (; head[u] != head[lc]; u = par[head[u]])
    res = max(res, rmq(pos[head[u]], pos[u]));
  if (pos[lc] < pos[u]) res = max(res, rmq(pos[lc] + 1, pos[u]));
  return res;
}
inline int ask(int u, int v) {
  int lc = lca(u, v);
  return max(get(u, lc), get(v, lc));
}
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  iota(ds, ds + MAX, 0);
  cin >> n >> q;
  vector<bool> res(q);
  vector<array<int, 4>> pending;
  for (int i = 0; i < q; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    if (frep(u) != frep(v)) {
      res[i] = true;
      join(u, v);
      adj[u].push_back({v, w});
      adj[v].push_back({u, w});
    } else
      pending.push_back({u, v, w, i});
  }
  for (int i = 0; i < MAX; ++i) {
    heavy[i] = -1;
    depth[i] = XOR[i] = 0;
  }
  memset(vst, false, sizeof(vst));
  for (int i = 1; i <= n; ++i)
    if (!vst[i]) dfs_sz(i);
  memset(vst, false, sizeof(vst));
  for (int i = 1; i <= n; ++i)
    if (!vst[i]) dfs_hld(i, i);
  for (auto &[u, v, w, i] : pending) {
    if ((XOR[u] ^ XOR[v] ^ w) == 1 && ask(u, v) == 0) {
      res[i] = true;
      for (; u != v; u = par[u]) {
        if (depth[u] < depth[v]) swap(u, v);
        modify(pos[u], 1);
      }
    }
  }
  string s[2] = {"NO", "YES"};
  for (bool x : res) cout << s[x] << '\n';
  return 0;
}

### Prompt

Your challenge is to write a Cpp solution 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;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
ld const PI = 4 * atan((ld)1);
constexpr int MAX = 3e5 + 69;
int n, q;
int ds[MAX];
vector<ii> adj[MAX];
inline int frep(int x) { return ds[x] == x ? x : ds[x] = frep(ds[x]); }
inline void join(int x, int y) { ds[frep(x)] = frep(y); }
bool vst[MAX];
int XOR[MAX], depth[MAX], par[MAX], head[MAX], heavy[MAX], pos[MAX], sz[MAX],
    st[2 * MAX];
inline void dfs_sz(int node) {
  vst[node] = true;
  sz[node] = 1;
  int sz_max = 0;
  for (auto [to, w] : adj[node]) {
    if (to != par[node]) {
      par[to] = node;
      depth[to] = depth[node] + 1;
      XOR[to] = XOR[node] ^ w;
      dfs_sz(to);
      sz[node] += sz[to];
      if (sz[to] > sz_max) sz_max = sz[to], heavy[node] = to;
    }
  }
}
static int cur_pos = 0;
inline void dfs_hld(int node, int h) {
  vst[node] = true;
  head[node] = h;
  pos[node] = cur_pos++;
  if (heavy[node] != -1) dfs_hld(heavy[node], h);
  for (auto [to, w] : adj[node])
    if (to != heavy[node] && to != par[node]) dfs_hld(to, to);
}
inline int lca(int u, int v) {
  for (; head[u] != head[v]; u = par[head[u]]) {
    if (depth[head[u]] < depth[head[v]]) swap(u, v);
  }
  return depth[u] < depth[v] ? u : v;
}
inline void modify(int p, int val) {
  for (st[p += n] = val; p > 1; p >>= 1) st[p >> 1] = max(st[p], st[p ^ 1]);
}
inline int rmq(int l, int r) {
  int res = 0;
  for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
    if (l & 1) res = max(res, st[l++]);
    if (r & 1) res = max(res, st[--r]);
  }
  return res;
}
inline int get(int u, int lc) {
  int res = 0;
  for (; head[u] != head[lc]; u = par[head[u]])
    res = max(res, rmq(pos[head[u]], pos[u]));
  if (pos[lc] < pos[u]) res = max(res, rmq(pos[lc] + 1, pos[u]));
  return res;
}
inline int ask(int u, int v) {
  int lc = lca(u, v);
  return max(get(u, lc), get(v, lc));
}
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  iota(ds, ds + MAX, 0);
  cin >> n >> q;
  vector<bool> res(q);
  vector<array<int, 4>> pending;
  for (int i = 0; i < q; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    if (frep(u) != frep(v)) {
      res[i] = true;
      join(u, v);
      adj[u].push_back({v, w});
      adj[v].push_back({u, w});
    } else
      pending.push_back({u, v, w, i});
  }
  for (int i = 0; i < MAX; ++i) {
    heavy[i] = -1;
    depth[i] = XOR[i] = 0;
  }
  memset(vst, false, sizeof(vst));
  for (int i = 1; i <= n; ++i)
    if (!vst[i]) dfs_sz(i);
  memset(vst, false, sizeof(vst));
  for (int i = 1; i <= n; ++i)
    if (!vst[i]) dfs_hld(i, i);
  for (auto &[u, v, w, i] : pending) {
    if ((XOR[u] ^ XOR[v] ^ w) == 1 && ask(u, v) == 0) {
      res[i] = true;
      for (; u != v; u = par[u]) {
        if (depth[u] < depth[v]) swap(u, v);
        modify(pos[u], 1);
      }
    }
  }
  string s[2] = {"NO", "YES"};
  for (bool x : res) cout << s[x] << '\n';
  return 0;
}

```