func_code_string: #include <bits/stdc++.h>   using namespace std;   ostream& operator<<(ostream& os, vector<int>& x) {     for (int i = 0; i < x.size(); i++) os << x[i] <<    ;     return os;   }   ostream& operator<<(ostream& os, pair<int, int> x) {     cout << x.first <<     << x.second <<    ;     return os;   }   ostream& operator<<(ostream& os, vector<pair<int, int> >& x) {     for (int i = 0; i < x.size(); i++) os << x[i] <<   n ;     return os;   }   vector<pair<int, int> > changed;   bool mark[500005];   struct dsu {     int par[500005];     dsu() {       for (int i = 1; i <= 500005 - 5; i++) par[i] = i;     }     int find(int x, bool cache = false) {       if (cache) {         if (!mark[x]) {           mark[x] = true;           changed.push_back(make_pair(x, par[x]));         }       }       if (par[x] == x) return x;       return par[x] = find(par[x]);     }     bool uni(int x, int y, bool cache = false) {       x = find(x, cache);       y = find(y, cache);       if (x != y) {         if (cache) {           if (!mark[x]) {             mark[x] = true;             changed.push_back(make_pair(x, par[x]));           }         }         par[x] = y;         return true;       }       return false;     }   } d;   struct data {     int u, v, c;     data(int _u, int _v, int _c) : u(_u), v(_v), c(_c){};   };   bool byPrice(data x, data y) { return x.c < y.c; }   int n, m, q;   vector<data> edge;   vector<data> oredge;   vector<vector<vector<int> > > query(500005);   vector<vector<pair<int, int> > > withW(500005);   vector<vector<int> > tmp(500005);   bool ans[500005];   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int u, v, c;       cin >> u >> v >> c;       edge.push_back(data(u, v, c));       withW[c].push_back(make_pair(u, v));     }     cin >> q;     for (int i = 1; i <= q; i++) {       int num;       cin >> num;       vector<int> zz;       while (num--) {         int t;         cin >> t;         t--;         zz.push_back(t);         tmp[edge[t].c].push_back(t);       }       for (int j = 0; j < zz.size(); j++) {         int c = edge[zz[j]].c;         if (tmp[c].empty()) continue;         tmp[c].push_back(i);         query[c].push_back(tmp[c]);         tmp[c].clear();       }     }     memset(ans, 1, sizeof(ans));     for (int w = 1; w <= 500000; w++) {       for (int i = 0; i < query[w].size(); i++) {         for (int j = 0; j < query[w][i].size() - 1; j++) {           int num = query[w][i][j];           assert(edge[num].c == w);           int u = edge[num].u;           int v = edge[num].v;           d.find(u);           d.find(v);         }       }       for (int i = 0; i < query[w].size(); i++) {         int which = query[w][i].back();         bool ok = true;         for (int j = 0; j < query[w][i].size() - 1; j++) {           int num = query[w][i][j];           int u = edge[num].u;           int v = edge[num].v;           if (!d.uni(u, v, 1)) {             ok = false;             break;           }         }         if (!ok) {           ans[which] = false;         }         for (int j = 0; j < changed.size(); j++) {           d.par[changed[j].first] = changed[j].second;           mark[changed[j].first] = false;         }         changed.clear();       }       for (int i = 0; i < withW[w].size(); i++) {         int u = withW[w][i].first;         int v = withW[w][i].second;         d.uni(u, v);       }     }     for (int i = 1; i <= q; i++) {       if (ans[i] == 1)         cout <<  YES  <<   n ;       else         cout <<  NO  <<   n ;     }   }   