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

/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;*/
#include <algorithm>
#include <bits/stdc++.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector<ll>
#define pii pair<ll, ll>
#define bc __builtin_popcountll
#define vvi vector<vi>
#define vpii vector<pair<ll, ll>>
#define mll unordered_map<ll, ll>
#define INF LONG_LONG_MAX
#define NINF LONG_LONG_MIN
#define f(i, a, b) for (i = a; i <= b; i++)
#define f(i, n) for (ll i = 0; i < n; i++)
#define f1(i, n) for (ll i = 1; i <= n; i++)
#define fb(i, n) for (ll i = n - 1; i >= 0; i--)
#define fb1(i, n) for (ll i = n; i > 0; i--)
#define endd cout << '\\n';
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define fr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define all(x) x.begin(), x.end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define CASES                                                                  \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
typedef long long ll;
typedef unsigned long long ull;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
// indexed_set; typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
//*X.find_by_order()
// X.order_of_key()
struct cmp {
  bool operator()(ll a, ll b) { return (a > b); }
};
ll mod = 1000000007;
const ll N = 200005;
vi adj[N];
ll vis[N];
ll c = 0;
void dfs(ll u, ll p) {
  vis[u] = 1;
  c++;
  // cout<<u;
  for (ll v : adj[u]) {
    if (vis[v] == 0) {
      dfs(v, u);
    }
  }
}
int main() {
  fast;
  vi v;
  ll n, m;
  cin >> n >> m;
  set<pii> s;
  f(i, m) {
    ll x, y;
    cin >> x >> y;
    s.insert({x, y});
  }
  for (auto const &it : s) {
    ll x = it.F, y = it.S;
    //  cout<<x<<' '<<y<<endl;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  clr(vis);
  f1(i, n) {
    if (vis[i] == 0) {
      c = 0;
      dfs(i, -1);
      v.pb(c);
    }
  }
  sortall(v);
  vi v1;
  v1.pb(v[0]);
  if (v.size() == 1)
    cout << v[0];
  else {
    f1(i, (ll)v.size() - 1) { v1.pb(v[i] - v[i - 1]); }
    ll sum = 0;
    f(i, (ll)v1.size()) { sum += v1[i]; }
    cout << sum;
  }
  return 0;
}

Error: WA

Fixed submission:

/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;*/
#include <algorithm>
#include <bits/stdc++.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector<ll>
#define pii pair<ll, ll>
#define bc __builtin_popcountll
#define vvi vector<vi>
#define vpii vector<pair<ll, ll>>
#define mll unordered_map<ll, ll>
#define INF LONG_LONG_MAX
#define NINF LONG_LONG_MIN
#define f(i, a, b) for (i = a; i <= b; i++)
#define f(i, n) for (ll i = 0; i < n; i++)
#define f1(i, n) for (ll i = 1; i <= n; i++)
#define fb(i, n) for (ll i = n - 1; i >= 0; i--)
#define fb1(i, n) for (ll i = n; i > 0; i--)
#define endd cout << '\\n';
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define fr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define all(x) x.begin(), x.end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define CASES                                                                  \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
typedef long long ll;
typedef unsigned long long ull;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
// indexed_set; typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
//*X.find_by_order()
// X.order_of_key()
struct cmp {
  bool operator()(ll a, ll b) { return (a > b); }
};
ll mod = 1000000007;
const ll N = 200005;
vi adj[N];
ll vis[N];
ll c = 0;
void dfs(ll u, ll p) {
  vis[u] = 1;
  c++;
  for (ll v : adj[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
}
int main() {
  fast;
  vi v;
  ll n, m;
  cin >> n >> m;
  set<pii> s;
  f(i, m) {
    ll x, y;
    cin >> x >> y;
    s.insert({x, y});
  }
  for (auto const &it : s) {
    ll x = it.F, y = it.S;
    //  cout<<x<<' '<<y<<endl;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  clr(vis);
  f1(i, n) {
    if (vis[i] == 0) {
      c = 0;
      dfs(i, -1);
      v.pb(c);
    }
  }
  sortall(v);
  vi v1;
  v1.pb(v[0]);
  if (v.size() == 1)
    cout << v[0];
  else {
    f1(i, (ll)v.size() - 1) { v1.pb(v[i] - v[i - 1]); }
    ll sum = 0;
    f(i, (ll)v1.size()) { sum += v1[i]; }
    cout << sum;
  }
  return 0;
}