#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
const int N = 800005, Q = 500005;
struct LCT {
  struct Node {
    int s[2], p, v;
    long long sum;
    bool rev;
  } tr[N];
  int stk[N];
  void pushup(int x) {
    tr[x].sum = tr[tr[x].s[0]].sum + tr[x].v + tr[tr[x].s[1]].sum;
  }
  void pushrev(int x) {
    swap(tr[x].s[0], tr[x].s[1]);
    tr[x].rev ^= 1;
  }
  void pushdown(int x) {
    if (tr[x].rev) {
      pushrev(tr[x].s[0]), pushrev(tr[x].s[1]);
      tr[x].rev = 0;
    }
  }
  bool isroot(int x) { return tr[tr[x].p].s[0] != x && tr[tr[x].p].s[1] != x; }
  void rotate(int x) {
    int y = tr[x].p, z = tr[y].p;
    int k = tr[y].s[1] == x;
    if (!isroot(y)) tr[z].s[tr[z].s[1] == y] = x;
    tr[x].p = z;
    tr[y].s[k] = tr[x].s[k ^ 1], tr[tr[x].s[k ^ 1]].p = y;
    tr[x].s[k ^ 1] = y, tr[y].p = x;
    pushup(y), pushup(x);
  }
  void splay(int x) {
    int top = 0, r = x;
    stk[++top] = r;
    while (!isroot(r)) stk[++top] = r = tr[r].p;
    while (top) pushdown(stk[top--]);
    while (!isroot(x)) {
      int y = tr[x].p, z = tr[y].p;
      if (!isroot(y))
        if ((tr[y].s[1] == x) ^ (tr[z].s[1] == y))
          rotate(x);
        else
          rotate(y);
      rotate(x);
    }
  }
  void access(int x) {
    int z = x;
    for (int y = 0; x; y = x, x = tr[x].p) {
      splay(x);
      tr[x].s[1] = y, pushup(x);
    }
    splay(z);
  }
  void makeroot(int x) {
    access(x);
    pushrev(x);
  }
  int findroot(int x) {
    access(x);
    while (tr[x].s[0]) pushdown(x), x = tr[x].s[0];
    splay(x);
    return x;
  }
  void split(int x, int y) {
    makeroot(x);
    access(y);
  }
  bool is_link(int x, int y) {
    makeroot(x);
    return findroot(y) == x;
  }
  bool is_direct_link(int x, int y) {
    return is_link(x, y) && tr[y].p == x && !tr[y].s[0];
  }
  void link(int x, int y) {
    if (!is_link(x, y)) tr[x].p = y;
  }
  void cut(int x, int y) {
    if (is_direct_link(x, y)) {
      tr[x].s[1] = tr[y].p = 0;
      pushup(x);
    }
  }
  long long query(int x, int y) {
    split(x, y);
    return tr[y].sum;
  }
  void update(int x, long long y) {
    splay(x);
    tr[x].v = y;
    pushup(x);
  }
} lct1, lct2;
struct Offline {
  int u, v, x;
  bool f;
} off[Q];
int stac[N], top;
void dfs(int now) {
  stac[++top] = now;
  if (lct2.tr[now].s[0]) dfs(lct2.tr[now].s[0]);
  if (lct2.tr[now].s[1]) dfs(lct2.tr[now].s[1]);
}
void solve() {
  int n = read(), q = read();
  for (int i = 1; i <= q; i++) {
    int u = read(), v = read(), x = read();
    off[i] = {u, v, x};
    if (lct1.is_link(u, v)) continue;
    off[i].f = 1;
    int mid = i + n;
    lct1.tr[mid].v = x;
    lct1.link(u, mid), lct1.link(mid, v);
    lct2.link(u, mid), lct2.link(mid, v);
  }
  for (int i = 1; i <= q; i++) {
    if (off[i].f) {
      puts("YES");
      continue;
    }
    int u = off[i].u, v = off[i].v, x = off[i].x;
    if (lct2.query(u, v) > 0) {
      puts("NO");
      continue;
    }
    if ((lct1.query(u, v) & 1) ^ x) {
      puts("YES");
      lct2.split(u, v);
      dfs(v);
      while (top) {
        int now = stac[top--];
        if (now <= n) continue;
        lct2.update(now, lct2.tr[now].v + 1);
      }
    } else
      puts("NO");
  }
}
int main() {
  int ttt = 1;
  for (int i = 1; i <= ttt; i++) solve();
  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;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
const int N = 800005, Q = 500005;
struct LCT {
  struct Node {
    int s[2], p, v;
    long long sum;
    bool rev;
  } tr[N];
  int stk[N];
  void pushup(int x) {
    tr[x].sum = tr[tr[x].s[0]].sum + tr[x].v + tr[tr[x].s[1]].sum;
  }
  void pushrev(int x) {
    swap(tr[x].s[0], tr[x].s[1]);
    tr[x].rev ^= 1;
  }
  void pushdown(int x) {
    if (tr[x].rev) {
      pushrev(tr[x].s[0]), pushrev(tr[x].s[1]);
      tr[x].rev = 0;
    }
  }
  bool isroot(int x) { return tr[tr[x].p].s[0] != x && tr[tr[x].p].s[1] != x; }
  void rotate(int x) {
    int y = tr[x].p, z = tr[y].p;
    int k = tr[y].s[1] == x;
    if (!isroot(y)) tr[z].s[tr[z].s[1] == y] = x;
    tr[x].p = z;
    tr[y].s[k] = tr[x].s[k ^ 1], tr[tr[x].s[k ^ 1]].p = y;
    tr[x].s[k ^ 1] = y, tr[y].p = x;
    pushup(y), pushup(x);
  }
  void splay(int x) {
    int top = 0, r = x;
    stk[++top] = r;
    while (!isroot(r)) stk[++top] = r = tr[r].p;
    while (top) pushdown(stk[top--]);
    while (!isroot(x)) {
      int y = tr[x].p, z = tr[y].p;
      if (!isroot(y))
        if ((tr[y].s[1] == x) ^ (tr[z].s[1] == y))
          rotate(x);
        else
          rotate(y);
      rotate(x);
    }
  }
  void access(int x) {
    int z = x;
    for (int y = 0; x; y = x, x = tr[x].p) {
      splay(x);
      tr[x].s[1] = y, pushup(x);
    }
    splay(z);
  }
  void makeroot(int x) {
    access(x);
    pushrev(x);
  }
  int findroot(int x) {
    access(x);
    while (tr[x].s[0]) pushdown(x), x = tr[x].s[0];
    splay(x);
    return x;
  }
  void split(int x, int y) {
    makeroot(x);
    access(y);
  }
  bool is_link(int x, int y) {
    makeroot(x);
    return findroot(y) == x;
  }
  bool is_direct_link(int x, int y) {
    return is_link(x, y) && tr[y].p == x && !tr[y].s[0];
  }
  void link(int x, int y) {
    if (!is_link(x, y)) tr[x].p = y;
  }
  void cut(int x, int y) {
    if (is_direct_link(x, y)) {
      tr[x].s[1] = tr[y].p = 0;
      pushup(x);
    }
  }
  long long query(int x, int y) {
    split(x, y);
    return tr[y].sum;
  }
  void update(int x, long long y) {
    splay(x);
    tr[x].v = y;
    pushup(x);
  }
} lct1, lct2;
struct Offline {
  int u, v, x;
  bool f;
} off[Q];
int stac[N], top;
void dfs(int now) {
  stac[++top] = now;
  if (lct2.tr[now].s[0]) dfs(lct2.tr[now].s[0]);
  if (lct2.tr[now].s[1]) dfs(lct2.tr[now].s[1]);
}
void solve() {
  int n = read(), q = read();
  for (int i = 1; i <= q; i++) {
    int u = read(), v = read(), x = read();
    off[i] = {u, v, x};
    if (lct1.is_link(u, v)) continue;
    off[i].f = 1;
    int mid = i + n;
    lct1.tr[mid].v = x;
    lct1.link(u, mid), lct1.link(mid, v);
    lct2.link(u, mid), lct2.link(mid, v);
  }
  for (int i = 1; i <= q; i++) {
    if (off[i].f) {
      puts("YES");
      continue;
    }
    int u = off[i].u, v = off[i].v, x = off[i].x;
    if (lct2.query(u, v) > 0) {
      puts("NO");
      continue;
    }
    if ((lct1.query(u, v) & 1) ^ x) {
      puts("YES");
      lct2.split(u, v);
      dfs(v);
      while (top) {
        int now = stac[top--];
        if (now <= n) continue;
        lct2.update(now, lct2.tr[now].v + 1);
      }
    } else
      puts("NO");
  }
}
int main() {
  int ttt = 1;
  for (int i = 1; i <= ttt; i++) solve();
  return 0;
}

```