Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define lli long long int
#define ulli unsigned long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define loop(i, a, b) for (lli i = a; i < b; i++)
#define initialize(array, size, value)                                         \
  for (lli i = 0; i < size; i++)                                               \
  array[i] = value
#define couta(array, size)                                                     \
  for (lli i = 0; i < size; i++)                                               \
  cout << array[i] << " "
#define vl vector<lli>
#define vp vector<pair<lli, lli>>
#define sl set<lli>
#define msp multiset<pair<long long, long long>> S;
#define pll pair<lli, lli>
#define mll                                                                    \
  map<lli,                                                                     \
      lli> // for( map<lli, lli>::iterator
           // i=temp.begin();i!=temp.end();i++)cout<<i->fi<<" "<<i->se<<endl;
#define mvl map<lli, vl>
#define umll unordered_map<lli, lli>
#define vt vector<pair<lli, pll>>
#define vf vector<pair<pll, pll>>
#define qu queue<lli>
#define pq priority_queue<lli>
#define dq deque<lli>
#define ptr vector<lli>::iterator
#define bs(array, x)                                                           \
  binary_search(array.begin(), array.end(),                                    \
                x) // also valid for set and multiset
#define lb(array, x) lower_bound(array.begin(), array.end(), x)
#define ub(array, x) upper_bound(array.begin(), array.end(), x)
#define nobw(array, i, j)                                                      \
  upper_bound(array.begin(), array.end(), j) -                                 \
      lower_bound(array.begin(), array.end(),                                  \
                  i) // number of numbers between i & j
#define vc clear()
#define endl '\n'
#define sp system("pause");
#define INF 9223372036854775807
#define MINF -9223372036854775808
typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
lli par[100001];
lli siz[100001];
// vector<lli>k[200000];       //if memory limit exceed try to exclude it
lli find(lli a) {
  if (a == par[a])
    return a;
  return find(par[a]);
}
void unite(lli a, lli b) {
  a = find(a);
  b = find(b);
  if (siz[b] > siz[a])
    swap(a, b);
  par[b] = a;
  siz[a] += siz[b];
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  lli n, m;
  cin >> n >> m;
  lli a[m][3];
  for (lli i = 0; i < m; i++) {
    cin >> a[i][0] >> a[i][1] >> a[i][2];
  }
  loop(i, 1, n + 1) {
    par[i] = i;
    siz[i] = 1;
    // k[i].push_back(i);
  }
  for (lli i = 0; i < m; i++) {
    unite(a[i][0], a[i][1]);
  }
  lli te[n + 1];
  for (lli i = 1; i <= n; i++)
    te[i] = 0;
  // for(lli i=1;i<=n;i++)cout<<par[i];
  for (lli i = 1; i <= n; i++)
    te[find(i)]++;
  lli ans = 0;
  for (lli i = 1; i <= n; i++)
    if (te[i] != 0)
      ans++;
  cout << ans;

  /*
  for(lli i=m-1;i>=0;i--){
          ans.pb(ans1);
          if(find(a[i][0])==find(a[i][1])){continue;
          }
          ans1=ans1-(k[find(a[i][0])].size()*k[find(a[i][1])].size());
          //cout<<k[find(a[i][0])].size()<<" "<<k[find(a[i][1])].size()<<endl;
          unite(a[i][0],a[i][1]);

  }
  reverse(ans.begin(),ans.end());
  for(lli i=0;i<ans.size();i++)cout<<ans[i]<<endl;
  /*loop(i,0,n-1) {
          lli a, b; cin >> a >> b;
          unite(a, b);
  }*/
  // lli t=k[find(1)].size();
  // for (lli i=0;i<t;i++)cout <<k[find(1)][i]<< " ";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define lli long long int
#define ulli unsigned long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define loop(i, a, b) for (lli i = a; i < b; i++)
#define initialize(array, size, value)                                         \
  for (lli i = 0; i < size; i++)                                               \
  array[i] = value
#define couta(array, size)                                                     \
  for (lli i = 0; i < size; i++)                                               \
  cout << array[i] << " "
#define vl vector<lli>
#define vp vector<pair<lli, lli>>
#define sl set<lli>
#define msp multiset<pair<long long, long long>> S;
#define pll pair<lli, lli>
#define mll                                                                    \
  map<lli,                                                                     \
      lli> // for( map<lli, lli>::iterator
           // i=temp.begin();i!=temp.end();i++)cout<<i->fi<<" "<<i->se<<endl;
#define mvl map<lli, vl>
#define umll unordered_map<lli, lli>
#define vt vector<pair<lli, pll>>
#define vf vector<pair<pll, pll>>
#define qu queue<lli>
#define pq priority_queue<lli>
#define dq deque<lli>
#define ptr vector<lli>::iterator
#define bs(array, x)                                                           \
  binary_search(array.begin(), array.end(),                                    \
                x) // also valid for set and multiset
#define lb(array, x) lower_bound(array.begin(), array.end(), x)
#define ub(array, x) upper_bound(array.begin(), array.end(), x)
#define nobw(array, i, j)                                                      \
  upper_bound(array.begin(), array.end(), j) -                                 \
      lower_bound(array.begin(), array.end(),                                  \
                  i) // number of numbers between i & j
#define vc clear()
#define endl '\n'
#define sp system("pause");
#define INF 9223372036854775807
#define MINF -9223372036854775808
typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
lli par[100001];
lli siz[100001];
// vector<lli>k[200000];       //if memory limit exceed try to exclude it
lli find(lli a) {
  if (a == par[a])
    return a;
  return find(par[a]);
}
void unite(lli a, lli b) {
  a = find(a);
  b = find(b);
  if (siz[b] > siz[a])
    swap(a, b);
  par[b] = a;
  siz[a] += siz[b];
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  lli n, m;
  cin >> n >> m;
  lli a[m][3];
  for (lli i = 0; i < m; i++) {
    cin >> a[i][0] >> a[i][1] >> a[i][2];
  }
  loop(i, 1, n + 1) {
    par[i] = i;
    siz[i] = 1;
    // k[i].push_back(i);
  }
  for (lli i = 0; i < m; i++) {
    unite(a[i][0], a[i][1]);
  }
  lli te[n + 1];
  for (lli i = 1; i <= n; i++)
    te[i] = 0;
  // for(lli i=1;i<=n;i++)cout<<par[i];
  for (lli i = 1; i <= n; i++)
    te[find(i)]++;
  lli ans = 0;
  for (lli i = 1; i <= n; i++)
    if (te[i] != 0)
      ans++;
  cout << ans;

  /*
  for(lli i=m-1;i>=0;i--){
          ans.pb(ans1);
          if(find(a[i][0])==find(a[i][1])){continue;
          }
          ans1=ans1-(k[find(a[i][0])].size()*k[find(a[i][1])].size());
          //cout<<k[find(a[i][0])].size()<<" "<<k[find(a[i][1])].size()<<endl;
          unite(a[i][0],a[i][1]);

  }
  reverse(ans.begin(),ans.end());
  for(lli i=0;i<ans.size();i++)cout<<ans[i]<<endl;
  /*loop(i,0,n-1) {
          lli a, b; cin >> a >> b;
          unite(a, b);
  }*/
  // lli t=k[find(1)].size();
  // for (lli i=0;i<t;i++)cout <<k[find(1)][i]<< " ";
  return 0;
}