description: 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
solution: #include <bits/stdc++.h>
  using namespace std;
  const int INF = 0x3f3f3f3f;
  const long long INFF = 0x3f3f3f3f3f3f3f3fll;
  const long long M = 998244353;
  const long long maxn = 3e5 + 107;
  const long long maxm = 5e5 + 107;
  const double pi = acos(-1.0);
  const double eps = 0.0000000001;
  long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
  template <typename T>
  inline void pr2(T x, unsigned long long k = 64) {
    unsigned long long i;
    for (i = 0; i < k; i++) fprintf(stderr, "%d", (int)((x >> i) & 1));
    putchar(' ');
  }
  template <typename T>
  inline void add_(T &A, int B, long long MOD = M) {
    A += B;
    (A >= MOD) && (A -= MOD);
  }
  template <typename T>
  inline void mul_(T &A, long long B, long long MOD = M) {
    A = (A * B) % MOD;
  }
  template <typename T>
  inline void mod_(T &A, long long MOD = M) {
    A %= MOD;
    A += MOD;
    A %= MOD;
  }
  template <typename T>
  inline void max_(T &A, T B) {
    (A < B) && (A = B);
  }
  template <typename T>
  inline void min_(T &A, T B) {
    (A > B) && (A = B);
  }
  template <typename T>
  inline T abs(T a) {
    return a > 0 ? a : -a;
  }
  inline long long powMM(long long a, long long b, long long mod = M) {
    long long ret = 1;
    for (; b; b >>= 1ll, a = a * a % mod)
      if (b & 1) ret = ret * a % mod;
    return ret;
  }
  int startTime;
  void startTimer() { startTime = clock(); }
  void printTimer() {
    fprintf(stderr, "/--- Time: %ld milliseconds ---/\n", clock() - startTime);
  }
  int MAX[maxn * 4];
  void update(int x, int l, int r, int value, int L, int R) {
    if (l <= L && R <= r) {
      MAX[x] = max(MAX[x], value);
      return;
    }
    int mid = (L + R) / 2;
    if (l <= mid) update(x << 1, l, r, value, L, mid);
    if (mid < r) update(x << 1 | 1, l, r, value, mid + 1, R);
  }
  int query(int x, int pos, int L, int R) {
    if (L == R) return MAX[x];
    int mid = (L + R) / 2, ret = MAX[x];
    if (pos <= mid) max_(ret, query(x << 1, pos, L, mid));
    if (mid < pos) max_(ret, query(x << 1 | 1, pos, mid + 1, R));
    return ret;
  }
  int in[maxn], out[maxn], tot;
  vector<pair<int, int> > edge[maxn];
  int fa[maxn], col[maxn], dep[maxn];
  int sz[maxn], son[maxn];
  void dfs(int x, int fa, int color) {
    dep[x] = dep[fa] + 1;
    col[x] = color;
    ::fa[x] = fa;
    in[x] = ++tot;
    sz[x] = 1;
    int mx = -1;
    for (auto e : edge[x])
      if (e.first != fa) {
        dfs(e.first, x, color ^ e.second);
        sz[x] += sz[e.first];
        if (sz[e.first] > mx) mx = sz[e.first], son[x] = e.first;
      }
    out[x] = tot;
  }
  int top[maxn], id[maxn];
  void dfs2(int u, int x) {
    top[u] = x;
    if (son[u]) dfs2(son[u], x);
    for (auto e : edge[u]) {
      int v = e.first;
      if (v == son[u]) continue;
      if (v == fa[u]) continue;
      dfs2(v, v);
    }
  }
  int lca(int x, int y) {
    while (top[x] != top[y]) {
      if (dep[top[x]] < dep[top[y]]) swap(x, y);
      x = fa[top[x]];
    }
    if (dep[x] > dep[y]) swap(x, y);
    return x;
  }
  inline int getfa(int x) {
    if (fa[x] == x) return x;
    return fa[x] = getfa(fa[x]);
  }
  int U[maxm], V[maxm];
  bool C[maxm], ans[maxm];
  int main() {
    int n, q, i;
    scanf("%d%d", &n, &q);
    for (i = 1; i <= n; i++) fa[i] = i;
    for (i = 1; i <= q; i++) {
      int c;
      scanf("%d%d%d", &U[i], &V[i], &c);
      C[i] = c;
      int x = getfa(U[i]), y = getfa(V[i]);
      if (x == y) continue;
      if (x < 1 || x > n) continue;
      fa[x] = y;
      edge[U[i]].push_back(make_pair(V[i], C[i]));
      edge[V[i]].push_back(make_pair(U[i], C[i]));
      ans[i] = 1;
    }
    for (i = 1; i <= n; i++)
      if (!in[i]) {
        dfs(i, 0, 0);
        dfs2(i, 1);
      }
    for (i = 1; i <= n; i++) vector<pair<int, int> >().swap(edge[i]);
    for (i = 1; i <= q; i++)
      if (!ans[i]) {
        int u = U[i], v = V[i];
        if (col[u] ^ col[v] ^ C[i]) {
          int f = lca(u, v);
          int du = query(1, in[u], 1, n);
          int dv = query(1, in[v], 1, n);
          if (max(du, dv) <= dep[f]) {
            ans[i] = 1;
            while (u != v) {
              if (dep[u] < dep[v]) swap(u, v);
              update(1, in[u], out[u], dep[u], 1, n);
              u = fa[u];
            }
          }
        }
      }
    for (i = 1; i <= q; i++) puts(ans[i] ? "YES" : "NO");
  }
  