Problem p03252 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define INF 10000000000000009
#define INF 9223372036854775807
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define ORREP(i, n) for (int i = (n); i >= 1; --i)
#define ZREP(i, n) for (int i = 1; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define rollcall cout << "I'm Sucu." << endl;
#define YES(s) s ? cout << "YES" << endl : cout << "NO" << endl
#define Yes(s) s ? cout << "Yes" << endl : cout << "No" << endl
#define Taka(s) s ? cout << "Takahashi" << endl : cout << "Aoki" << endl
#define out(s, t, u) s ? cout << t << endl : cout << u << endl
#define int ll
#define Endl endl

int UF[114514];
int UF2[114514];
void UFinit(int n) {
  for (int i = 0; i <= n; i++) {
    UF[i] = i;
  }
}
int UFroot(int k) {
  if (UF[k] == k) {
    return k;
  } else {
    UF[k] = UFroot(UF[k]);
    return UF[k];
  }
}
int UFsame(int P, int Q) {
  if (UFroot(P) == UFroot(Q)) {
    return true;
  } else {
    return false;
  }
}
void UFunite(int P, int Q) {
  int p = UFroot(P);
  int q = UFroot(Q);
  if (p == q)
    return;
  if (p > q)
    swap(p, q);
  UF[q] = p;
}

signed main() {
  string S, T;
  cin >> S >> T;
  int E[26];
  UFinit(S.size());
  REP(i, 26) { E[i] = -1; }
  REP(i, S.size()) {
    if (E[S[i] - 'a'] == -1) {
      E[S[i] - 'a'] = i;
    } else {
      UFunite(E[S[i] - 'a'], i);
    }
  }
  REP(i, S.size()) { UF2[i] = UF[i]; }
  UFinit(T.size());
  REP(i, 26) { E[i] = -1; }
  REP(i, T.size()) {
    if (E[T[i] - 'a'] == -1) {
      E[T[i] - 'a'] = i;
    } else {
      UFunite(E[T[i] - 'a'], i);
    }
  }
  /*REP(i,S.size()){
      cout << UF2[i] << " ";
  }cout << Endl;
  REP(i,S.size()){
      cout << UF[i] << " ";
  }cout << Endl;*/
  REP(i, S.size()) {
    if (UF[i] != UF2[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define INF 10000000000000009
#define INF 9223372036854775807
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define ORREP(i, n) for (int i = (n); i >= 1; --i)
#define ZREP(i, n) for (int i = 1; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define rollcall cout << "I'm Sucu." << endl;
#define YES(s) s ? cout << "YES" << endl : cout << "NO" << endl
#define Yes(s) s ? cout << "Yes" << endl : cout << "No" << endl
#define Taka(s) s ? cout << "Takahashi" << endl : cout << "Aoki" << endl
#define out(s, t, u) s ? cout << t << endl : cout << u << endl
#define int ll
#define Endl endl

int UF[214514];
int UF2[214514];
void UFinit(int n) {
  for (int i = 0; i <= n; i++) {
    UF[i] = i;
  }
}
int UFroot(int k) {
  if (UF[k] == k) {
    return k;
  } else {
    UF[k] = UFroot(UF[k]);
    return UF[k];
  }
}
int UFsame(int P, int Q) {
  if (UFroot(P) == UFroot(Q)) {
    return true;
  } else {
    return false;
  }
}
void UFunite(int P, int Q) {
  int p = UFroot(P);
  int q = UFroot(Q);
  if (p == q)
    return;
  if (p > q)
    swap(p, q);
  UF[q] = p;
}

signed main() {
  string S, T;
  cin >> S >> T;
  int E[26];
  UFinit(S.size());
  REP(i, 26) { E[i] = -1; }
  REP(i, S.size()) {
    if (E[S[i] - 'a'] == -1) {
      E[S[i] - 'a'] = i;
    } else {
      UFunite(E[S[i] - 'a'], i);
    }
  }
  REP(i, S.size()) { UF2[i] = UF[i]; }
  UFinit(T.size());
  REP(i, 26) { E[i] = -1; }
  REP(i, T.size()) {
    if (E[T[i] - 'a'] == -1) {
      E[T[i] - 'a'] = i;
    } else {
      UFunite(E[T[i] - 'a'], i);
    }
  }
  /*REP(i,S.size()){
      cout << UF2[i] << " ";
  }cout << Endl;
  REP(i,S.size()){
      cout << UF[i] << " ";
  }cout << Endl;*/
  REP(i, S.size()) {
    if (UF[i] != UF2[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  return 0;
}