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;
  static char buf[200 << 20];
  void* operator new(size_t s) {
    static size_t i = sizeof buf;
    assert(s < i);
    return (void*)&buf[i -= s];
  }
  void operator delete(void*) {}
  const int inf = 1e9;
  struct Node {
    Node *l = 0, *r = 0;
    int lo, hi, mset = inf, madd = 0, val = 0;
    Node(int lo, int hi) : lo(lo), hi(hi) {}
    Node(vector<int>& v, int lo, int hi) : lo(lo), hi(hi) {
      if (lo + 1 < hi) {
        int mid = lo + (hi - lo) / 2;
        l = new Node(v, lo, mid);
        r = new Node(v, mid, hi);
        val = max(l->val, r->val);
      } else
        val = v[lo];
    }
    int query(int L, int R) {
      if (R <= lo || hi <= L) return -inf;
      if (L <= lo && hi <= R) return val;
      push();
      return max(l->query(L, R), r->query(L, R));
    }
    void set(int L, int R, int x) {
      if (R <= lo || hi <= L) return;
      if (L <= lo && hi <= R)
        mset = val = x, madd = 0;
      else {
        push(), l->set(L, R, x), r->set(L, R, x);
        val = max(l->val, r->val);
      }
    }
    void add(int L, int R, int x) {
      if (R <= lo || hi <= L) return;
      if (L <= lo && hi <= R) {
        if (mset != inf)
          mset += x;
        else
          madd += x;
        val += x;
      } else {
        push(), l->add(L, R, x), r->add(L, R, x);
        val = max(l->val, r->val);
      }
    }
    void push() {
      if (!l) {
        int mid = lo + (hi - lo) / 2;
        l = new Node(lo, mid);
        r = new Node(mid, hi);
      }
      if (mset != inf)
        l->set(lo, hi, mset), r->set(lo, hi, mset), mset = inf;
      else if (madd)
        l->add(lo, hi, madd), r->add(lo, hi, madd), madd = 0;
    }
  };
  template <bool VALS_EDGES>
  struct HLD {
    int N, tim = 0;
    vector<vector<int>> adj;
    vector<int> par, siz, depth, rt, pos;
    Node* tree;
    HLD(vector<vector<int>> adj_)
        : N(adj_.size()),
          adj(adj_),
          par(N, -1),
          siz(N, 1),
          depth(N),
          rt(N),
          pos(N),
          tree(new Node(0, N)) {
      dfsSz(0);
      dfsHld(0);
    }
    void dfsSz(int v) {
      if (par[v] != -1) adj[v].erase(find(adj[v].begin(), adj[v].end(), par[v]));
      for (int& u : adj[v]) {
        par[u] = v, depth[u] = depth[v] + 1;
        dfsSz(u);
        siz[v] += siz[u];
        if (siz[u] > siz[adj[v][0]]) swap(u, adj[v][0]);
      }
    }
    void dfsHld(int v) {
      pos[v] = tim++;
      for (int u : adj[v]) {
        rt[u] = (u == adj[v][0] ? rt[v] : u);
        dfsHld(u);
      }
    }
    template <class B>
    void process(int u, int v, B op) {
      for (; rt[u] != rt[v]; v = par[rt[v]]) {
        if (depth[rt[u]] > depth[rt[v]]) swap(u, v);
        op(pos[rt[v]], pos[v] + 1);
      }
      if (depth[u] > depth[v]) swap(u, v);
      op(pos[u] + VALS_EDGES, pos[v] + 1);
    }
    void modifyPath(int u, int v, int val) {
      process(u, v, [&](int l, int r) { tree->add(l, r, val); });
    }
    int queryPath(int u, int v) {
      int res = -1e9;
      process(u, v, [&](int l, int r) { res = max(res, tree->query(l, r)); });
      return res;
    }
    int querySubtree(int v) {
      return tree->query(pos[v] + VALS_EDGES, pos[v] + siz[v]);
    }
  };
  class DSU {
    vector<int> rank, parent;
    int n;
  
   public:
    DSU(int n) : rank(n, 0), parent(n) {
      this->n = n;
      for (int i = 0; i < n; ++i) parent[i] = i;
    }
    int find(int x) {
      if (parent[x] != x) parent[x] = find(parent[x]);
      return parent[x];
    }
    void join(int x, int y) {
      int xset = find(x);
      int yset = find(y);
      if (xset == yset) return;
      if (rank[xset] < rank[yset])
        parent[xset] = yset;
      else if (rank[xset] > rank[yset])
        parent[yset] = xset;
      else {
        parent[yset] = xset;
        rank[xset]++;
      }
    }
  };
  vector<int> get_parents(const vector<vector<int>>& adj, int root) {
    int n = adj.size();
    vector<int> parents(n, -1);
    stack<pair<int, int>> st;
    st.emplace(root, -1);
    while (!st.empty()) {
      int node = st.top().first;
      parents[node] = st.top().second;
      st.pop();
      for (int i : adj[node]) {
        if (i != parents[node]) {
          st.emplace(i, node);
        }
      }
    }
    return parents;
  }
  vector<vector<int>> get_children(const vector<int>& parents) {
    int n = parents.size();
    vector<vector<int>> children(n);
    for (int i = 0; i < n; ++i) {
      if (parents[i] >= 0) {
        children[parents[i]].push_back(i);
      }
    }
    return children;
  }
  vector<bool> solve_component(int n,
                               const vector<pair<pair<int, int>, int>>& queries) {
    int n_queries = queries.size();
    vector<bool> ans(n_queries, false);
    vector<int> edge_indices;
    DSU dsu(n);
    vector<int> remaining;
    for (int qi = 0; qi < n_queries; ++qi) {
      int u = queries[qi].first.first;
      int v = queries[qi].first.second;
      if (dsu.find(u) != dsu.find(v)) {
        ans[qi] = true;
        edge_indices.push_back(qi);
        dsu.join(u, v);
      } else {
        remaining.push_back(qi);
      }
    }
    assert(int(edge_indices.size()) == (n - 1));
    vector<vector<int>> adj(n);
    for (int i : edge_indices) {
      const pair<int, int>& edge = queries[i].first;
      adj[edge.first].push_back(edge.second);
      adj[edge.second].push_back(edge.first);
    }
    vector<int> parents = get_parents(adj, 0);
    vector<int> cumxor(n, 0);
    for (int i : edge_indices) {
      const pair<int, int>& edge = queries[i].first;
      if (parents[edge.first] == edge.second) {
        cumxor[edge.first] = queries[i].second;
      } else {
        cumxor[edge.second] = queries[i].second;
      }
    }
    vector<vector<int>> children = get_children(parents);
    vector<int> q(1, 0);
    for (int i = 0; i < int(q.size()); ++i) {
      if (parents[q[i]] >= 0) {
        cumxor[q[i]] ^= cumxor[parents[q[i]]];
      }
      q.insert(q.end(), children[q[i]].begin(), children[q[i]].end());
    }
    HLD<true> hld(adj);
    for (int qi : remaining) {
      int u = queries[qi].first.first;
      int v = queries[qi].first.second;
      int x = queries[qi].second;
      if (((cumxor[u] ^ cumxor[v] ^ x) != 1) || (hld.queryPath(u, v) > 0)) {
        ans[qi] = false;
        continue;
      }
      ans[qi] = true;
      hld.modifyPath(u, v, 1);
    }
    return ans;
  }
  vector<bool> solve(int n, const vector<int>& u, const vector<int>& v,
                     const vector<int>& x) {
    int q = u.size();
    DSU dsu(n);
    for (int i = 0; i < q; ++i) {
      dsu.join(u[i], v[i]);
    }
    vector<int> component_map(n, -1);
    vector<vector<int>> components;
    int n_components = 0;
    for (int i = 0; i < n; ++i) {
      int compi = dsu.find(i);
      if (component_map[compi] < 0) {
        component_map[compi] = n_components++;
        components.emplace_back();
      }
      int j = component_map[compi];
      components[j].push_back(i);
    }
    vector<vector<int>> queries_by_component(n_components);
    for (int i = 0; i < q; ++i) {
      queries_by_component[component_map[dsu.find(u[i])]].push_back(i);
    }
    vector<bool> ans(q);
    vector<int> vertex_map(n, -1);
    for (int compi = 0; compi < n_components; ++compi) {
      vector<pair<pair<int, int>, int>> queries;
      int compn = components[compi].size();
      for (int i = 0; i < compn; ++i) {
        vertex_map[components[compi][i]] = i;
      }
      for (int i : queries_by_component[compi]) {
        queries.emplace_back(make_pair(vertex_map[u[i]], vertex_map[v[i]]), x[i]);
      }
      vector<bool> comp_ans = solve_component(compn, queries);
      for (int i = 0; i < int(queries_by_component[compi].size()); ++i) {
        ans[queries_by_component[compi][i]] = comp_ans[i];
      }
    }
    return ans;
  }
  int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, q;
    cin >> n >> q;
    vector<int> u(q), v(q), x(q);
    for (int i = 0; i < q; ++i) {
      cin >> u[i] >> v[i] >> x[i];
      --u[i];
      --v[i];
    }
    vector<bool> ans = solve(n, u, v, x);
    for (int i = 0; i < q; ++i) {
      cout << (ans[i] ? "YES" : "NO") << "\n";
    }
  }
  