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

#include <bits/stdc++.h>
typedef double D;
typedef long long int LL;
typedef long double LD;
#define OR ||
#define AND &&
#define nl '\\n'
#define S string
#define inf INT_MAX
#define SQR(a) (a) * (a)
#define pb push_back
#define GCD(a, b) __gcd(a, b)
#define PI 2.0 * acos(0.0)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define LCM(a, b) (a * b) / GCD(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
#define srtv(v) sort(v.begin(), v.end())
#define T                                                                      \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i >= b; i--)
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define inout                                                                  \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout)

#define si(x) scanf("%d", &x)
#define pi(x) printf("%d", x)
#define ss(str) scanf("%s", str)
#define pl(x) printf("%lld", x)
#define sl(x) scanf("%lld", &x)
#define sii(x, y) scanf("%d %d", &x, &y)
#define sll(x, y) scanf("%lld %lld", &x, &y)
#define siii(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define slll(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
using namespace std;

/*
int bs(int l,int r,int n)
{
    int mid,res=inf;

    while(l<=r)
    {
        mid=l+(r-l)/2;

        if(check(mid,n))
        {
            r=mid-1;
            res=min(res,mid);
        }
        else
            l=mid+1;

    }

    return res;
}
*/

//--------------------------code starts
//here---------------------------------------

#define mx 100005
int parent[mx];

void makeSet(int u) { parent[u] = u; }

void init(int n) {
  for (int i = 0; i <= n; i++)
    makeSet(i);
}

int Find(int u) {
  if (u == parent[u])
    return u;

  return parent[u] = Find(parent[u]);
}

void Union(int u, int v) {
  int p = Find(u);
  int q = Find(v);

  if (p != q) {
    parent[q] = p;
  }
}

int main() {
  int n, m, u, v, w;
  sii(n, m);

  init(n);

  Rep(i, 1, m) {
    sii(u, v);

    Union(u, v);
  }

  set<int> st;

  Rep(i, 1, n) { st.insert(Find(i)); }

  cout << st.size() - 1;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
typedef double D;
typedef long long int LL;
typedef long double LD;
#define OR ||
#define AND &&
#define nl '\\n'
#define S string
#define inf INT_MAX
#define SQR(a) (a) * (a)
#define pb push_back
#define GCD(a, b) __gcd(a, b)
#define PI 2.0 * acos(0.0)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define LCM(a, b) (a * b) / GCD(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
#define srtv(v) sort(v.begin(), v.end())
#define T                                                                      \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i >= b; i--)
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define inout                                                                  \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout)

#define si(x) scanf("%d", &x)
#define pi(x) printf("%d", x)
#define ss(str) scanf("%s", str)
#define pl(x) printf("%lld", x)
#define sl(x) scanf("%lld", &x)
#define sii(x, y) scanf("%d %d", &x, &y)
#define sll(x, y) scanf("%lld %lld", &x, &y)
#define siii(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define slll(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
using namespace std;

/*
int bs(int l,int r,int n)
{
    int mid,res=inf;

    while(l<=r)
    {
        mid=l+(r-l)/2;

        if(check(mid,n))
        {
            r=mid-1;
            res=min(res,mid);
        }
        else
            l=mid+1;

    }

    return res;
}
*/

//--------------------------code starts
//here---------------------------------------

struct Edge {
  int u, v, w;
};

bool compareByWeight(Edge a, Edge b) { return a.w < b.w; }

#define mx 30001
int parent[mx];

vector<Edge> E;

void makeSet(int u) { parent[u] = u; }

void init(int n) {
  for (int i = 0; i <= n; i++)
    makeSet(i);
}

int Find(int u) {
  if (u == parent[u])
    return u;

  return parent[u] = Find(parent[u]);
}

void Union(int u, int v) {
  int p = Find(u);
  int q = Find(v);

  if (p != q) {
    parent[q] = p;
  }
}

int main() {
  int n, m, u, v, w;
  sii(n, m);

  init(n);

  Rep(i, 1, m) {
    sii(u, v);

    Union(u, v);
  }

  set<int> st;

  Rep(i, 1, n) { st.insert(Find(i)); }

  cout << st.size() - 1;

  return 0;
}