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;
  struct dsu {
    vector<int> p;
    dsu(int n) : p(n + 1, -1) {}
    int root(int x) { return p[x] < 0 ? x : p[x] = root(p[x]); }
    bool join(int x, int y) {
      if ((x = root(x)) == (y = root(y))) return 0;
      if (p[y] < p[x]) swap(x, y);
      p[x] += p[y];
      p[y] = x;
      return 1;
    }
  };
  const int nmax = 300030;
  int n, m, tin[nmax], tout[nmax], xr[nmax], anc[nmax][20], f[nmax];
  vector<tuple<int, int, int>> query;
  vector<pair<int, int>> adj[nmax];
  void dfs(int u, int p = 0, int d = 0) {
    anc[u][0] = p;
    for (int j = 1; j < 19; ++j) anc[u][j] = anc[anc[u][j - 1]][j - 1];
    xr[u] = d;
    tin[u] = ++*tin;
    for (auto& x : adj[u]) {
      if (x.second == p) continue;
      dfs(x.second, u, d ^ x.first);
    }
    tout[u] = *tin;
  }
  bool isanc(int u, int v) { return tin[u] <= tin[v] && tout[v] <= tout[u]; }
  int lca(int u, int v) {
    if (isanc(u, v)) return u;
    if (isanc(v, u)) return v;
    for (int j = 18; j >= 0; --j)
      if (!isanc(anc[v][j], u)) v = anc[v][j];
    return anc[v][0];
  }
  void inc(int x, int d) {
    for (; x <= n; x += x & -x) f[x] += d;
  }
  int sum(int x) {
    int ans = 0;
    for (x = tin[x]; x > 0; x &= x - 1) ans += f[x];
    return ans;
  }
  void add(int x, int p) {
    for (; x != p; x = anc[x][0]) {
      inc(tin[x], 1);
      inc(tout[x] + 1, -1);
    }
  }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    dsu d(n);
    vector<int> choose(m, 0);
    for (int i = (0), _b = (m - 1); i <= _b; ++i) {
      int x, y, w;
      cin >> x >> y >> w;
      query.push_back(make_tuple(x, y, w));
      if (d.join(x, y)) {
        adj[x].push_back({w, y});
        adj[y].push_back({w, x});
        choose[i] = 1;
      }
    }
    for (int i = (1), _b = (n); i <= _b; ++i)
      if (!tin[i]) dfs(i);
    tin[0] = 0;
    tout[0] = 0x3f3f3f3f;
    for (int i = (0), _b = (m - 1); i <= _b; ++i) {
      int u, v, w;
      tie(u, v, w) = query[i];
      if (!(xr[u] ^ xr[v] ^ w)) continue;
      int x = lca(u, v);
      if (sum(u) + sum(v) - 2 * sum(x) > 0) continue;
      choose[i] = 1;
      add(u, x);
      add(v, x);
    }
    for (int i = (0), _b = (m - 1); i <= _b; ++i)
      cout << (choose[i] ? "YES" : "NO") << '\n';
    return 0;
  }
  