Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define double long double
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define endl '\\n'
#define ff first
#define ss second
#define rev(a) a.rbegin(), a.rend()
#define all(a) a.begin(), a.end()
#define pi pair<int, int>
#define vii vector<pi>
#define pb push_back
// #define size(a)  (int)(a.size())
#define fill(s, x, n)                                                          \\
  vector
 x(n);                                                              \\
  rep(i, 0, n) cin >> x[i];
#define debug(...)                                                             \\
  cerr << " [" << #__VA_ARGS__ << ": " << (__VA_ARGS__) << "] ";
#define debugv(...)                                                            \\
  for (int i = 0; i < size(__VA_ARGS__); i++)                                  \\
    cerr << " [" << #__VA_ARGS__ << i << ": " << (__VA_ARGS__)[i] << "] ";
const int mod = 1e8;
const int inf = 1e9 + 5;
const int inf64 = 1e18 + 5;
//           -------"Where is your Pen and Paper?"-------
vector<int> parent(100005);
int find_set(int v) {
  if (v == parent[v])
    return v;
  return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b)
    parent[b] = a;
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    parent[i] = i;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    union_sets(a, b);
  }
  unordered_set<int> s;
  for (int i = 1; i <= n; i++) {
    find_set(i);
    s.insert(parent[i]);
  }
  cout << s.size() - 1;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /*
  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  #endif
  */
  int test;
  // cin>>test;
  test = 1;
  // int cse=1;
  while (test--) {
    // cout<<"Case #"<<cse<<": ";
    solve();
    cout << endl;
    // cse++;
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
  return 0;
}

Error: WA

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define double long double
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define endl '\\n'
#define ff first
#define ss second
#define rev(a) a.rbegin(), a.rend()
#define all(a) a.begin(), a.end()
#define pi pair<int, int>
#define vii vector<pi>
#define pb push_back
// #define size(a)  (int)(a.size())
#define fill(s, x, n)                                                          \\
  vector
 x(n);                                                              \\
  rep(i, 0, n) cin >> x[i];
#define debug(...)                                                             \\
  cerr << " [" << #__VA_ARGS__ << ": " << (__VA_ARGS__) << "] ";
#define debugv(...)                                                            \\
  for (int i = 0; i < size(__VA_ARGS__); i++)                                  \\
    cerr << " [" << #__VA_ARGS__ << i << ": " << (__VA_ARGS__)[i] << "] ";
const int mod = 1e8;
const int inf = 1e9 + 5;
const int inf64 = 1e18 + 5;
//           -------"Where is your Pen and Paper?"-------
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> adj(n + 1, vector<int>(n + 1, 1));
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a][b] = 0;
    adj[b][a] = 0;
  }
  int total_weight = 0;
  vector<int> min_e(n + 1, inf);
  min_e[1] = 0;
  set<pi> q;
  q.insert({0, 1});
  vector<bool> selected(n + 1, false);
  for (int i = 1; i <= n; ++i) {
    int v = q.begin()->second;
    selected[v] = true;
    total_weight += q.begin()->first;
    q.erase(q.begin());

    // if (min_e[v].to != -1)
    //     cout << v << " " << min_e[v].to << endl;

    for (int i = 1; i <= n; i++) {
      int w = adj[v][i];
      if (!selected[i] && w < min_e[i]) {
        q.erase({min_e[i], i});
        min_e[i] = w;
        q.insert({w, i});
      }
    }
  }

  cout << total_weight << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /*
  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  #endif
  */
  int test;
  // cin>>test;
  test = 1;
  // int cse=1;
  while (test--) {
    // cout<<"Case #"<<cse<<": ";
    solve();
    cout << endl;
    // cse++;
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
  return 0;
}