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

#include <algorithm>
#include <bits/stdc++.h>
#define ll long long int
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define fr(i, a, b) for (long long int i = a; i < b; i++)
#define pb push_back
#define mp make_pair
#define MOD 1000000007
using namespace std;
vector<ll> g[1000001];
ll vis[1000005] = {0};
map<ll, ll> mx;
/*queue<ll>q ;
void bfs(ll s)
{
       vis[s]=1;
       q.push(s);
       ll curr=q.front(); vis[curr]=1;
       while(!q.empty())
       {
              curr=q.front(); q.pop(); // vis[curr]=1;
              for(ll i=0;i<g[curr].size();i++)
              {
                     ll child=g[curr][i];
                     if(vis[child]==0)
                     {
                            q.push(child);
                            vis[child]=1;
                     }
              }
       }
}*/
ll co = 0;
ll dfs(ll curr) {
  vis[curr] = 1;
  for (ll i = 0; i < g[curr].size(); i++) {
    ll child = g[curr][i];
    if (vis[child] == 0) {
      co++;
      dfs(child);
    }
  }
  return co;
}
signed main() {
  fast ll n, m;
  cin >> n >> m;
  while (m--) {
    ll u, v;
    cin >> u >> v;
    if (mx[u] != v) {
      g[u].pb(v);
      g[v].pb(u);
      mx[u] = v;
    }
  }
  ll c = 0;
  vector<ll> v;
  for (ll i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      v.pb(dfs(i) + 1);
      co = 0;
      c++;
    }
  }
  // for(ll i=0;i<v.size();i++) cout<<v[i]<<" ";
  if (c == 1)
    cout << n << " ";
  else {
    sort(v.begin(), v.end());
    cout << v[0] + (v[v.size() - 1] - v[0]);
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#define ll long long int
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define fr(i, a, b) for (long long int i = a; i < b; i++)
#define pb push_back
#define mp make_pair
#define MOD 1000000007
using namespace std;
vector<ll> g[1000001];
ll vis[1000005] = {0};
ll arr[1000][1000];
/*queue<ll>q ;
void bfs(ll s)
{
       vis[s]=1;
       q.push(s);
       ll curr=q.front(); vis[curr]=1;
       while(!q.empty())
       {
              curr=q.front(); q.pop(); // vis[curr]=1;
              for(ll i=0;i<g[curr].size();i++)
              {
                     ll child=g[curr][i];
                     if(vis[child]==0)
                     {
                            q.push(child);
                            vis[child]=1;
                     }
              }
       }
}*/
ll co = 0;
ll dfs(ll curr) {
  vis[curr] = 1;
  for (ll i = 0; i < g[curr].size(); i++) {
    ll child = g[curr][i];
    if (vis[child] == 0) {
      co++;
      dfs(child);
    }
  }
  return co;
}
signed main() {
  fast ll n, m;
  cin >> n >> m;
  while (m--) {
    ll u, v;
    cin >> u >> v;
    if (arr[u][v] == 0) {
      g[u].pb(v);
      g[v].pb(u);
      arr[u][v] = 1;
    }
  }
  ll c = 0;
  vector<ll> v;
  for (ll i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      v.pb(dfs(i) + 1);
      co = 0;
      c++;
    }
  }
  // for(ll i=0;i<v.size();i++) cout<<v[i]<<" ";
  if (c == 1)
    cout << n << " ";
  else {
    sort(v.begin(), v.end());
    cout << v[0] + (v[v.size() - 1] - v[0]);
  }
}