prithivMLmods/Coder-Stat huggingface dataset:
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int dp[10005];

void solve() {
  string s;
  cin >> s;
  int n = s.size();
  dp[0] = 1;
  for (int i = 0; i < n; ++i) {
    if (dp[i] == 0)
      continue;
    if ((i + 5 <= n) && (s.substr(i, 5) == "dream"))
      dp[i + 5] = 1;
    if ((i + 7 <= n) && (s.substr(i, 7) == "dreamer"))
      dp[i + 7] = 1;
    if ((i + 5 <= n) && (s.substr(i, 5) == "erase"))
      dp[i + 5] = 1;
    if ((i + 6 <= n) && (s.substr(i, 6) == "eraser"))
      dp[i + 6] = 1;
  }
  cout << (dp[n] ? "YES" : "NO");
}

int main() {

  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int dp[100005];

void solve() {
  string s;
  cin >> s;
  int n = s.size();
  dp[0] = 1;
  for (int i = 0; i < n; ++i) {
    if (dp[i] == 0)
      continue;
    if ((i + 5 <= n) && (s.substr(i, 5) == "dream"))
      dp[i + 5] = 1;
    if ((i + 7 <= n) && (s.substr(i, 7) == "dreamer"))
      dp[i + 7] = 1;
    if ((i + 5 <= n) && (s.substr(i, 5) == "erase"))
      dp[i + 5] = 1;
    if ((i + 6 <= n) && (s.substr(i, 6) == "eraser"))
      dp[i + 6] = 1;
  }
  cout << (dp[n] ? "YES" : "NO");
}

int main() {

  solve();
  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;

int main() {
  string S;
  cin >> S;

  int len = S.length();
  string T = "";

  for (int i = 0; i < len - 1;) {
    if (S[i] == 'd') {
      // "dream"
      i += 5;
      T += "dream";

      // "dream" + "era" でないならば "dreamer"
      if (S[i] == 'e' && S[i + 2] != 'a') {
        i += 2;
        T += "er";
      }

    } else if (S[i] == 'e') {
      // "erase"
      i += 5;
      T += "erase";

      // "eraser"
      if (S[i] == 'r') {
        i += 1;
        T += "r";
      }
    }

    if (S.substr(0, i) != T) {
      cout << "NO" << endl;
      return 0;
    }
  }

  if (S == T)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
using namespace std;

int main() {
  string S;
  cin >> S;

  int len = S.length();
  string T = "";

  for (int i = 0; i < len - 1;) {
    if (S[i] == 'd') {
      // "dream"
      i += 5;
      T += "dream";

      // "dream" + "era" でないならば "dreamer"
      if (S[i] == 'e' && S[i + 2] != 'a') {
        i += 2;
        T += "er";
      }

    } else if (S[i] == 'e') {
      // "erase"
      i += 5;
      T += "erase";

      // "eraser"
      if (S[i] == 'r') {
        i += 1;
        T += "r";
      }
    } else {
      cout << "NO" << endl;
      return 0;
    }

    if (S.substr(0, i) != T) {
      cout << "NO" << endl;
      return 0;
    }
  }

  if (S == T)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define cyes cout << "YES" << endl
#define cno cout << "NO" << endl
#define sp << " " <<
#define cst(x) cout << fixed << setprecision(x)
#define pi 3.14159265359
#define mod 1000000007
using namespace std;
using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;
using que_a = priority_queue<int, vector<int>, greater<int>>;
using que_d = priority_queue<int>;
using pint = pair<int, int>;

int main() {

  string s;
  cin >> s;
  vector<string> word = {"dream", "dreamer", "erase", "eraser"};
  vector<bool> dp(10005, false);
  dp.at(0) = true;
  rep(i, s.size()) {
    if (dp.at(i) == false)
      continue;
    for (auto wo : word) {
      if (wo == s.substr(i, wo.size()))
        dp[i + wo.size()] = true;
    }
  }

  dp[s.size()] ? cyes : cno;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define cyes cout << "YES" << endl
#define cno cout << "NO" << endl
#define sp << " " <<
#define cst(x) cout << fixed << setprecision(x)
#define pi 3.14159265359
#define mod 1000000007
using namespace std;
using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;
using que_a = priority_queue<int, vector<int>, greater<int>>;
using que_d = priority_queue<int>;
using pint = pair<int, int>;

int main() {

  string s;
  cin >> s;
  vector<string> word = {"dream", "dreamer", "erase", "eraser"};
  vector<bool> dp(100005, false);
  dp.at(0) = true;
  rep(i, s.size()) {
    if (dp.at(i) == false)
      continue;
    for (auto wo : word) {
      if (wo == s.substr(i, wo.size()))
        dp[i + wo.size()] = true;
    }
  }

  dp[s.size()] ? cyes : cno;
  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string c = "YES", S;
  cin >> S;
  reverse(S.begin(), S.end());
  string a[4] = {"maerd", "remaerd", "esare", "resare"};
  for (int i = 0; i < S.size();) {
    bool can = 0;
    for (int j = 0; j < 4; j++) {
      string d = a[j];
      if (S.substr(i, d.size()) == d) {
        i += d.size();
        can = 1;
      }
    }
    if (!can) {
      c = "NO";
    }
  }
  cout << c << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string c = "YES", S;
  cin >> S;
  reverse(S.begin(), S.end());
  string a[4] = {"maerd", "remaerd", "esare", "resare"};
  for (int i = 0; i < S.size();) {
    bool can = 0;
    for (int j = 0; j < 4; j++) {
      string d = a[j];
      if (S.substr(i, d.size()) == d) {
        i += d.size();
        can = 1;
      }
    }
    if (!can) {
      c = "NO";
      break;
    }
  }
  cout << c << endl;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S;
  cin >> S;

  string divide[4] = {"dream", "dreamer", "erase", "eraser"};

  reverse(S.begin(), S.end());

  bool can = true;
  for (int i = 0; i < 4; i++) {
    reverse(divide[i].begin(), divide[i].end());
  }
  for (int i = 0; i < S.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string d = divide[j];
      if (S.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
      if (!can2) {
        can = false;
        break;
      }
    }
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S;
  cin >> S;

  string divide[4] = {"dream", "dreamer", "erase", "eraser"};

  reverse(S.begin(), S.end());

  bool can = true;
  for (int i = 0; i < 4; i++) {
    reverse(divide[i].begin(), divide[i].end());
  }
  for (int i = 0; i < S.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string d = divide[j];
      if (S.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
    }
    if (!can2) {
      can = false;
      break;
    }
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;

int main() {
  string s, ans = "YES";
  cin >> s;
  int i = 0;
  int n = s.size();
  while (i < n) {
    if (i + 4 >= n) {
      ans = "NO";
      break;
    }
    if (s[i] == 'd') {
      if (s[i + 1] == 'r' and s[i + 2] == 'e' and s[i + 3] == 'a' and
          s[i + 4] == 'm') {
        if (i + 4 == n - 1)
          break;
        if (s[i + 5] == 'e' and s[i + 6] == 'r') {
          if (i + 6 == n - 1)
            break;
          if (s[i + 7] == 'a') {
            i += 5;
            continue;
          } else if (s[i + 7] == 'd' or s[i + 7] == 'e') {
            i += 7;
            continue;
          } else {
            ans = "NO";
            break;
          }
        } else {
          i += 5;
          continue;
        }
      } else {
        ans = "NO";
        break;
      }
    } else if (s[i] == 'e') {
      if (s[i + 1] == 'r' and s[i + 2] == 'a' and s[i + 3] == 's' and
          s[i + 4] == 'e') {
        if (i + 4 == n - 1)
          break;
        if (s[i + 5] == 'r') {
          i += 6;
          continue;
        } else {
          i += 5;
          continue;
        }
      } else {
        ans = "NO";
        break;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;

int main() {
  string s, ans = "YES";
  cin >> s;
  int i = 0;
  int n = s.size();
  while (i < n) {
    if (i + 4 >= n) {
      ans = "NO";
      break;
    }
    if (s[i] == 'd') {
      if (s[i + 1] == 'r' and s[i + 2] == 'e' and s[i + 3] == 'a' and
          s[i + 4] == 'm') {
        if (i + 4 == n - 1)
          break;
        if (s[i + 5] == 'e' and s[i + 6] == 'r') {
          if (i + 6 == n - 1)
            break;
          if (s[i + 7] == 'a') {
            i += 5;
            continue;
          } else if (s[i + 7] == 'd' or s[i + 7] == 'e') {
            i += 7;
            continue;
          } else {
            ans = "NO";
            break;
          }
        } else {
          i += 5;
          continue;
        }
      } else {
        ans = "NO";
        break;
      }
    } else if (s[i] == 'e') {
      if (s[i + 1] == 'r' and s[i + 2] == 'a' and s[i + 3] == 's' and
          s[i + 4] == 'e') {
        if (i + 4 == n - 1)
          break;
        if (s[i + 5] == 'r') {
          i += 6;
          continue;
        } else {
          i += 5;
          continue;
        }
      } else {
        ans = "NO";
        break;
      }
    } else {
      ans = "NO";
      break;
    }
  }
  cout << ans << "\n";
  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

string str[4] = {"dream", "dreamer", "erase", "eraser"};

int main() {
  string S;
  cin >> S;

  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++) {
    reverse(str[i].begin(), str[i].end());
  }

  bool totalflag = true;
  for (int i = 0; i < S.size();) {
    bool partflag = false;
    for (int j = 0; j < 4; j++) {
      if (S.substr(i, str[j].size()) == str[j]) {
        partflag = true;
        i += str[i].size();
      }
    }
    if (!partflag) {
      totalflag = false;
      break;
    }
  }
  if (totalflag) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

string str[4] = {"dream", "dreamer", "erase", "eraser"};

int main() {
  string S;
  cin >> S;

  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++) {
    reverse(str[i].begin(), str[i].end());
  }

  bool totalflag = true;
  for (int i = 0; i < S.size();) {
    bool partflag = false;
    for (int j = 0; j < 4; j++) {
      if (S.substr(i, str[j].size()) == str[j]) {
        partflag = true;
        i += str[j].size();
      }
    }
    if (!partflag) {
      totalflag = false;
      break;
    }
  }
  if (totalflag) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VD = vector<double>;
using VVD = vector<VD>;
using VS = vector<string>;
using PII = pair<int, int>;
using VPII = vector<PII>;
using VL = vector<long long>;
using VVL = vector<VL>;

#define sz(var) (int)var.size()
#define REP(var, start, end) for (int var = start; var < (int)end; ++var)
#define CLEAR(var, with) memset(var, with, sizeof(var))
#define ALL(x) x.begin(), x.end()
#define LL long long
#define ULL unsigned long long
#define TIME                                                                   \
  cout << "Time elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms" << endl;

const int MAXN = (int)15;
bool DP[MAXN];
string s;
VS examples = {"dream", "dreamer", "erase", "eraser"};

bool isPossible() {
  CLEAR(DP, false);
  DP[0] = true;
  for (int i = 0; i <= sz(s); ++i) {
    if (DP[i] == 0)
      continue;
    for (string str : examples) {
      int n = sz(str);
      if (s.substr(i, n) == str)
        DP[i + n] = true;
    }
  }
  return DP[sz(s)];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
//	freopen("tmp.in", "r", stdin);
//	freopen("tmp.out", "w", stderr);
#endif
  cin >> s;

  cout << (isPossible() ? "YES" : "NO") << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VD = vector<double>;
using VVD = vector<VD>;
using VS = vector<string>;
using PII = pair<int, int>;
using VPII = vector<PII>;
using VL = vector<long long>;
using VVL = vector<VL>;

#define sz(var) (int)var.size()
#define REP(var, start, end) for (int var = start; var < (int)end; ++var)
#define CLEAR(var, with) memset(var, with, sizeof(var))
#define ALL(x) x.begin(), x.end()
#define LL long long
#define ULL unsigned long long
#define TIME                                                                   \
  cout << "Time elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms" << endl;

const int MAXN = (int)1e5 + 10;
bool DP[MAXN];
string s;
VS examples = {"dream", "dreamer", "erase", "eraser"};

bool isPossible() {
  CLEAR(DP, false);
  DP[0] = true;
  for (int i = 0; i <= sz(s); ++i) {
    if (DP[i] == 0)
      continue;
    for (string str : examples) {
      int n = sz(str);
      if (s.substr(i, n) == str)
        DP[i + n] = true;
    }
  }
  return DP[sz(s)];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
//	freopen("tmp.in", "r", stdin);
//	freopen("tmp.out", "w", stderr);
#endif
  cin >> s;

  cout << (isPossible() ? "YES" : "NO") << endl;

  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;
string a[4] = {"dream", "dreamer", "erase", "eraser"};

int main() {
  string s;
  cin >> s;

  reverse(s.begin(), s.end());

  for (int i = 0; i < s.size(); i++)
    reverse(a[i].begin(), a[i].end());
  bool can = true;
  for (int i = 0; i < s.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string d = a[j];
      if (s.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
    }
    if (!can2) {
      can = false;
      break;
    }
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;
string a[4] = {"dream", "dreamer", "erase", "eraser"};

int main() {
  string s;
  cin >> s;

  reverse(s.begin(), s.end());

  for (int i = 0; i < 4; ++i)
    reverse(a[i].begin(), a[i].end());
  bool can = true;
  for (int i = 0; i < s.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string d = a[j];
      if (s.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
    }
    if (!can2) {
      can = false;
      break;
    }
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>

int len;
char S[110];
const char words[4][8] = {"dream", "dreamer", "erase", "eraser"};
const int length[4] = {5, 7, 5, 6};

int f(int index) {
  if (index == len)
    return 1;
  if (index > len)
    return 0;
  for (int i = 0; i < 4; ++i) {
    int j;
    for (j = 0; j < length[i]; ++j)
      if (S[index + j] != words[i][j])
        break;
    if (j == length[i]) { // matches
      if (f(index + length[i]))
        return 1;
    }
  }
  return 0;
}

int main() {
  scanf("%s", S);
  len = strlen(S);
  if (f(0))
    printf("YES\n");
  else
    printf("NO\n");
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>

int len;
char S[100000];
const char words[4][8] = {"dream", "dreamer", "erase", "eraser"};
const int length[4] = {5, 7, 5, 6};

int f(int index) {
  if (index == len)
    return 1;
  if (index > len)
    return 0;
  for (int i = 0; i < 4; ++i) {
    int j;
    for (j = 0; j < length[i]; ++j)
      if (S[index + j] != words[i][j])
        break;
    if (j == length[i]) { // matches
      if (f(index + length[i]))
        return 1;
    }
  }
  return 0;
}

int main() {
  scanf("%s", S);
  len = strlen(S);
  if (f(0))
    printf("YES\n");
  else
    printf("NO\n");
  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 1; i <= n; i++)

string T[] = {"dream", "dreamer", "erase", "eraser"};

bool huga(string S) {
  // cout<<S<<endl;
  int size = S.size();
  if (size <= 4)
    return false;
  if (size == 5 || size == 6 || size == 7) {
    for (int i = 0; i < 4; i++) {
      if (T[i] == S)
        return true;
    }
  } else {
    string temp;
    bool ans = false;
    for (int i = 0; i < 5; i++) {
      temp += S[i];
    }
    if (temp == T[0]) {
      S.erase(S.begin(), S.begin() + 5);
      if (huga(S))
        return true;
      if (S[0] == 'e') {
        S.erase(S.begin(), S.begin() + 2);
        if (huga(S))
          return true;
      }
    } else if (temp == T[2]) {
      S.erase(S.begin(), S.begin() + 5);
      if (huga(S))
        return true;
      if (S[0] == 'r') {
        S.erase(S.begin(), S.begin() + 1);
        if (huga(S))
          return true;
      }
    }
  }
  return false;
}
signed main() {
  string S;
  bool ans = false;

  cin >> S;

  cout << (huga(S) ? "YES" : "NO") << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 1; i <= n; i++)

string T[] = {"dream", "dreamer", "erase", "eraser"};

bool huga(string &S) {
  // cout<<S<<endl;
  int size = S.size();
  if (size <= 4)
    return false;
  if (size == 5 || size == 6 || size == 7) {
    for (int i = 0; i < 4; i++) {
      if (T[i] == S)
        return true;
    }
  } else {
    string temp;
    bool ans = false;
    for (int i = 0; i < 5; i++) {
      temp += S[i];
    }
    if (temp == T[0]) {
      S.erase(S.begin(), S.begin() + 5);
      if (huga(S))
        return true;
      if (S[0] == 'e') {
        S.erase(S.begin(), S.begin() + 2);
        if (huga(S))
          return true;
      }
    } else if (temp == T[2]) {
      S.erase(S.begin(), S.begin() + 5);
      if (huga(S))
        return true;
      if (S[0] == 'r') {
        S.erase(S.begin(), S.begin() + 1);
        if (huga(S))
          return true;
      }
    }
  }
  return false;
}
signed main() {
  string S;
  bool ans = false;

  cin >> S;

  cout << (huga(S) ? "YES" : "NO") << endl;

  return 0;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S;
  cin >> S;
  string divide[4] = {"dream", "dreamer", "erase", "eraser"};

  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++)
    reverse(divide[i].begin(), divide[i].end());

  bool can = true;
  for (int i = 0; i < S.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string d = divide[i];
      if (S.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
    }
    if (!can2) {
      can = false;
      break;
    }
  }

  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S;
  cin >> S;
  string divide[4] = {"dream", "dreamer", "erase", "eraser"};

  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++)
    reverse(divide[i].begin(), divide[i].end());

  bool can = true;
  for (int i = 0; i < S.size();) {
    bool can2 = false;
    for (int j = 0; j < 4; j++) {
      string d = divide[j];
      if (S.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
    }
    if (!can2) {
      can = false;
      break;
    }
  }

  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

string divide[4] = {"dream", "dreamer", "erase", "eraser"};

int main() {
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++)
    reverse(divide[i].begin(), divide[i].end());

  bool can = 1;

  for (int i = 0; i < S.size();) {
    bool can2 = 0;
    for (int j = 0; j < 4; j++) {
      string d = divide[4];
      if (S.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
    }
    if (!can2) {
      can = 0;
      break;
    }
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

string divide[4] = {"dream", "dreamer", "erase", "eraser"};

int main() {
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  for (int i = 0; i < 4; i++)
    reverse(divide[i].begin(), divide[i].end());

  bool can = 1;

  for (int i = 0; i < S.size();) {
    bool can2 = 0;
    for (int j = 0; j < 4; j++) {
      string d = divide[j];
      if (S.substr(i, d.size()) == d) {
        can2 = true;
        i += d.size();
      }
    }
    if (!can2) {
      can = 0;
      break;
    }
  }
  if (can)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03854 (C++)

Buggy submission (status: Runtime Error):
// finish date: 2018/01/20
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <stack>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int(i) = a; (i) < (b); (i)++)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
#define bigmod 1000000007
#define INF 500000000

int main() {
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  int pos = 0;
  string aho = S.substr(34, 9);
  while (pos != S.length()) {
    if (S.substr(pos, 5) == "maerd")
      pos += 5;
    else if (S.substr(pos, 7) == "remaerd")
      pos += 7;
    else if (S.substr(pos, 5) == "esare")
      pos += 5;
    else if (S.substr(pos, 6) == "resare")
      pos += 6;
    else {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;

  return 0;
}


Fixed submission:
// finish date: 2018/01/20
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <stack>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int(i) = a; (i) < (b); (i)++)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
#define bigmod 1000000007
#define INF 500000000

int main() {
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  int pos = 0;
  while (pos != S.length()) {
    if (S.substr(pos, 5) == "maerd")
      pos += 5;
    else if (S.substr(pos, 7) == "remaerd")
      pos += 7;
    else if (S.substr(pos, 5) == "esare")
      pos += 5;
    else if (S.substr(pos, 6) == "resare")
      pos += 6;
    else {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;

  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
using namespace std;

int unionF1[100001], unionF2[100001], rank1[100001], rank2[100001];
int n, k, l;

void init() {
  for (int i = 1; i <= n; i++) {
    unionF1[i] = i;
    unionF2[i] = i;
  }
}

int find(int u, int x) { // uの方のunionfindで根を見つける
  int *uni;
  if (u == 0) {
    uni = unionF1;
  } else {
    uni = unionF2;
  }

  if (x == uni[x]) {
    return uni[x];
  } else {
    return uni[x] = find(u, uni[x]);
  }
}

void unite(int u, int x, int y) { // uの方のunionfindで集合を合体させる
  int *uni, *rank;
  if (u == 0) {
    uni = unionF1;
    rank = rank1;
  } else {
    uni = unionF2;
    rank = rank2;
  }

  if (rank[find(u, x)] >= rank[find(u, y)]) {
    rank[find(u, x)]++;
    uni[find(u, y)] = find(u, x);
  } else {
    rank[find(u, y)]++;
    uni[find(u, x)] = find(u, y);
  }
}

bool same(int u, int x, int y) { return find(u, x) == find(u, y); }

int main() {
  cin >> n >> k >> l;
  init();
  for (int i = 0; i < k; i++) {
    int p, q;
    cin >> p >> q;
    unite(0, p, q);
  }
  for (int i = 0; i < l; i++) {
    int r, s;
    cin >> r >> s;
    unite(1, r, s);
  }
  map<pair<int, int>, int> m;
  for (int i = 1; i <= n; i++) {
    // cout << "0:" << find(0, i) << " 1:" << find(1, i) << endl;
    pair<int, int> p;
    p.first = find(0, i);
    p.second = find(1, i);
    m[p]++;
  }
  for (int i = 1; i <= n; i++) {
    // cout << "0:" << find(0, i) << " 1:" << find(1, i) << endl;
    pair<int, int> p;
    p.first = find(0, i);
    p.second = find(1, i);
    cout << m[p] << " ";
  }
}

Fixed submission:
#include <iostream>
#include <map>
using namespace std;

int unionF1[200001], unionF2[200001], rank1[200001], rank2[200001];
int n, k, l;

void init() {
  for (int i = 1; i <= n; i++) {
    unionF1[i] = i;
    unionF2[i] = i;
  }
}

int find(int u, int x) { // uの方のunionfindで根を見つける
  int *uni;
  if (u == 0) {
    uni = unionF1;
  } else {
    uni = unionF2;
  }

  if (x == uni[x]) {
    return uni[x];
  } else {
    return uni[x] = find(u, uni[x]);
  }
}

void unite(int u, int x, int y) { // uの方のunionfindで集合を合体させる
  int *uni, *rank;
  if (u == 0) {
    uni = unionF1;
    rank = rank1;
  } else {
    uni = unionF2;
    rank = rank2;
  }

  if (rank[find(u, x)] >= rank[find(u, y)]) {
    rank[find(u, x)]++;
    uni[find(u, y)] = find(u, x);
  } else {
    rank[find(u, y)]++;
    uni[find(u, x)] = find(u, y);
  }
}

bool same(int u, int x, int y) { return find(u, x) == find(u, y); }

int main() {
  cin >> n >> k >> l;
  init();
  for (int i = 0; i < k; i++) {
    int p, q;
    cin >> p >> q;
    unite(0, p, q);
  }
  for (int i = 0; i < l; i++) {
    int r, s;
    cin >> r >> s;
    unite(1, r, s);
  }
  map<pair<int, int>, int> m;
  for (int i = 1; i <= n; i++) {
    // cout << "0:" << find(0, i) << " 1:" << find(1, i) << endl;
    pair<int, int> p;
    p.first = find(0, i);
    p.second = find(1, i);
    m[p]++;
  }
  for (int i = 1; i <= n; i++) {
    // cout << "0:" << find(0, i) << " 1:" << find(1, i) << endl;
    pair<int, int> p;
    p.first = find(0, i);
    p.second = find(1, i);
    cout << m[p] << " ";
  }
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using Weight = long long;
using Vertex = int;
struct Edge {
  Vertex from, to;
  Weight weight;
};
using Graph = std::vector<std::vector<Edge>>;

std::vector<std::vector<Vertex>> connected_component(const Graph &G) {
  std::vector<std::vector<Vertex>> cc;
  std::vector<bool> used(G.size());
  std::function<void(int)> dfs = [&](int v) {
    cc.back().push_back(v);
    used[v] = true;
    for (const auto &e : G[v])
      if (!used[e.to])
        dfs(e.to);
  };
  for (Vertex v = 0; v < G.size(); ++v)
    if (!used[v]) {
      cc.push_back(std::vector<Vertex>());
      dfs(v);
    }
  return cc;
}

int main() {
  int N, K, L;
  cin >> N >> K >> L;

  Graph G(N);
  for (auto i = 0; i < K; ++i) {
    int p, q;
    cin >> p >> q;
    --p;
    --q;
    G[p].push_back({p, q});
    G[q].push_back({q, p});
  }

  Graph H(N);
  for (auto i = 0; i < L; ++i) {
    int r, s;
    cin >> r >> s;
    --r;
    --s;
    H[r].push_back({r, s});
    H[s].push_back({s, r});
  }

  auto ccg = connected_component(G);
  auto cch = connected_component(H);
  vector<int> g(N), h(N);
  for (auto i = 0; i < ccg.size(); ++i)
    for (auto v : ccg[i])
      g[v] = i;
  for (auto i = 0; i < cch.size(); ++i)
    for (auto v : cch[i])
      h[v] = i;

  vector<map<int, int>> cc(cch.size());
  for (auto i = 0; i < ccg.size(); ++i)
    for (auto v : ccg[i])
      ++cc[i][h[v]];

  for (auto i = 0; i < N; ++i)
    cout << (i ? " " : "") << cc[g[i]][h[i]];
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using Weight = long long;
using Vertex = int;
struct Edge {
  Vertex from, to;
  Weight weight;
};
using Graph = std::vector<std::vector<Edge>>;

std::vector<std::vector<Vertex>> connected_component(const Graph &G) {
  std::vector<std::vector<Vertex>> cc;
  std::vector<bool> used(G.size());
  std::function<void(int)> dfs = [&](int v) {
    cc.back().push_back(v);
    used[v] = true;
    for (const auto &e : G[v])
      if (!used[e.to])
        dfs(e.to);
  };
  for (Vertex v = 0; v < G.size(); ++v)
    if (!used[v]) {
      cc.push_back(std::vector<Vertex>());
      dfs(v);
    }
  return cc;
}

int main() {
  int N, K, L;
  cin >> N >> K >> L;

  Graph G(N);
  for (auto i = 0; i < K; ++i) {
    int p, q;
    cin >> p >> q;
    --p;
    --q;
    G[p].push_back({p, q});
    G[q].push_back({q, p});
  }

  Graph H(N);
  for (auto i = 0; i < L; ++i) {
    int r, s;
    cin >> r >> s;
    --r;
    --s;
    H[r].push_back({r, s});
    H[s].push_back({s, r});
  }

  auto ccg = connected_component(G);
  auto cch = connected_component(H);
  vector<int> g(N), h(N);
  for (auto i = 0; i < ccg.size(); ++i)
    for (auto v : ccg[i])
      g[v] = i;
  for (auto i = 0; i < cch.size(); ++i)
    for (auto v : cch[i])
      h[v] = i;

  vector<map<int, int>> cc(ccg.size());
  for (auto i = 0; i < ccg.size(); ++i)
    for (auto v : ccg[i])
      ++cc[i][h[v]];

  for (auto i = 0; i < N; ++i)
    cout << (i ? " " : "") << cc[g[i]][h[i]];
  cout << endl;
}
Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

class UnionFind {
public:
  vector<long long> root;

  UnionFind(long long N) { root = vector<long long>(N + 1, -1); }

  long long find_root(long long A) {
    if (root[A] < 0) {
      return A;
    } else {
      root[A] = find_root(root[A]);
    }
    return root[A];
  }

  long long size(long long A) { return -root[find_root(A)]; }

  bool unite(long long A, long long B) {
    A = find_root(A);
    B = find_root(B);
    if (A == B) {
      return false;
    }

    if (size(A) < size(B)) {
      swap(A, B);
    }

    root[A] += root[B];
    root[B] = A;

    return true;
  }

  bool has_same_root(long long A, long long B) {
    return find_root(A) == find_root(B);
  }
};

int main() {
  int N, K, L;
  cin >> N >> K >> L;

  UnionFind road(N);
  int p, q;
  for (int i = 0; i < K; i++) {
    cin >> p >> q;
    road.unite(p, q);
  }

  UnionFind rail(N);
  int r, s;
  for (int i = 0; i < L; i++) {
    cin >> r >> s;
    rail.unite(r, s);
  }

  for (int i = 1; i <= N; i++) {
    int ans = 0;
    for (int j = 1; j <= N; j++) {
      if (road.has_same_root(i, j) && rail.has_same_root(i, j)) {
        ans++;
      }
    }
    cout << ans << " ";
  }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

class UnionFind {
public:
  vector<long long> root;

  UnionFind(long long N) { root = vector<long long>(N + 1, -1); }

  long long find_root(long long A) {
    if (root[A] < 0) {
      return A;
    } else {
      root[A] = find_root(root[A]);
    }
    return root[A];
  }

  long long size(long long A) { return -root[find_root(A)]; }

  bool unite(long long A, long long B) {
    A = find_root(A);
    B = find_root(B);
    if (A == B) {
      return false;
    }

    if (size(A) < size(B)) {
      swap(A, B);
    }

    root[A] += root[B];
    root[B] = A;

    return true;
  }

  bool has_same_root(long long A, long long B) {
    return find_root(A) == find_root(B);
  }
};

int main() {
  int N, K, L;
  cin >> N >> K >> L;

  UnionFind road(N);
  int p, q;
  for (int i = 0; i < K; i++) {
    cin >> p >> q;
    road.unite(p, q);
  }

  UnionFind rail(N);
  int r, s;
  for (int i = 0; i < L; i++) {
    cin >> r >> s;
    rail.unite(r, s);
  }

  map<pair<long long, long long>, long long> count;
  for (long long i = 1; i <= N; i++) {
    count[{road.find_root(i), rail.find_root(i)}]++;
  }

  for (long long i = 1; i <= N; i++) {
    cout << count[{road.find_root(i), rail.find_root(i)}] << " ";
  }
  cout << endl;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include "iostream"
#include "map"
#include "vector"

using namespace std;

long long int N, K, L, F, S;

class UnionFind {
  vector<int> parent;
  vector<int> rank;

public:
  UnionFind(int num) {
    num++;
    parent.resize(num);
    rank.resize(num);
    for (int i = 0; i < num; i++) {
      parent[i] = i;
      rank[i] = 0;
    }
  }
  int Find(int node) {
    if (parent[node] == node)
      return node;
    else
      return parent[node] = Find(parent[node]);
  }
  void Unite(int u, int v) {
    u = Find(u);
    v = Find(v);
    if (u == v)
      return;
    if (rank[u] < rank[v])
      parent[u] = v;
    else {
      parent[v] = u;
      if (rank[u] == rank[v])
        rank[u]++;
    }
  }
  bool Check(int u, int v) { return Find(u) == Find(v); }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> K >> L;
  UnionFind road(N);
  UnionFind railway(N);
  for (int i = 0; i < K; i++) {
    cin >> F >> S;
    F--, S--;
    road.Unite(F, S);
  }
  for (int i = 0; i < L; i++) {
    cin >> F >> S;
    F--, S--;
    railway.Unite(F, S);
  }

  vector<int> ans(N * N);
  vector<pair<int, int>> parents(N);
  map<pair<int, int>, int> pair_map;
  for (int i = 0; i < N; i++) {
    parents[i].first = road.Find(i);
    parents[i].second = railway.Find(i);
    if (pair_map.count(parents[i]) == 0)
      pair_map.insert(make_pair(parents[i], 0));
    pair_map[parents[i]]++;
  }
  for (int i = 0; i < N; i++) {
    cout << pair_map[parents[i]] << " ";
  }

  cout << endl;
  return 0;
}

Fixed submission:
#include "iostream"
#include "map"
#include "vector"

using namespace std;

long long int N, K, L, F, S;

class UnionFind {
  vector<int> parent;
  vector<int> rank;

public:
  UnionFind(int num) {
    num++;
    parent.resize(num);
    rank.resize(num);
    for (int i = 0; i < num; i++) {
      parent[i] = i;
      rank[i] = 0;
    }
  }
  int Find(int node) {
    if (parent[node] == node)
      return node;
    else
      return parent[node] = Find(parent[node]);
  }
  void Unite(int u, int v) {
    u = Find(u);
    v = Find(v);
    if (u == v)
      return;
    if (rank[u] < rank[v])
      parent[u] = v;
    else {
      parent[v] = u;
      if (rank[u] == rank[v])
        rank[u]++;
    }
  }
  bool Check(int u, int v) { return Find(u) == Find(v); }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> K >> L;
  UnionFind road(N);
  UnionFind railway(N);
  for (int i = 0; i < K; i++) {
    cin >> F >> S;
    F--, S--;
    road.Unite(F, S);
  }
  for (int i = 0; i < L; i++) {
    cin >> F >> S;
    F--, S--;
    railway.Unite(F, S);
  }

  vector<pair<int, int>> parents(N);
  map<pair<int, int>, int> pair_map;
  for (int i = 0; i < N; i++) {
    parents[i].first = road.Find(i);
    parents[i].second = railway.Find(i);
    if (pair_map.count(parents[i]) == 0)
      pair_map.insert(make_pair(parents[i], 0));
    pair_map[parents[i]]++;
  }
  for (int i = 0; i < N; i++) {
    cout << pair_map[parents[i]] << " ";
  }

  cout << endl;
  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef vector<vector<int>> graph;
typedef vector<int> vi;

vi s;
void dfs(const graph &g, int v, int color) {
  s[v] = color;
  for (int next_v : g[v]) {
    if (s[next_v] < 0)
      ;
    dfs(g, next_v, color);
  }
}

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  graph g(n), h(n);
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    g[a - 1].push_back(b - 1);
    g[b - 1].push_back(a - 1);
  }
  for (int i = 0; i < l; i++) {
    int a, b;
    cin >> a >> b;
    h[a - 1].push_back(b - 1);
    h[b - 1].push_back(a - 1);
  }

  s.assign(n, -1);
  for (int i = 0; i < n; i++) {
    if (s[i] < 0)
      dfs(g, i, i);
  }
  vi t(n);
  copy(s.begin(), s.end(), t.begin());
  s.assign(n, -1);
  for (int i = 0; i < n; i++) {
    if (s[i] < 0)
      dfs(h, i, i);
  }

  map<pair<int, int>, int> m;
  for (int i = 0; i < n; i++) {
    m[make_pair(t[i], s[i])]++;
  }
  for (int i = 0; i < n; i++) {
    pair<int, int> p = make_pair(t[i], s[i]);
    cout << m[p] << " ";
  }
  cout << endl;
}

Fixed submission:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef vector<vector<int>> graph;
typedef vector<int> vi;

vi s;
void dfs(const graph &g, int v, int color) {
  s[v] = color;
  for (int next_v : g[v]) {
    if (s[next_v] < 0)
      dfs(g, next_v, color);
  }
}

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  graph g(n), h(n);
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    g[a - 1].push_back(b - 1);
    g[b - 1].push_back(a - 1);
  }
  for (int i = 0; i < l; i++) {
    int a, b;
    cin >> a >> b;
    h[a - 1].push_back(b - 1);
    h[b - 1].push_back(a - 1);
  }

  s.assign(n, -1);
  for (int i = 0; i < n; i++) {
    if (s[i] < 0)
      dfs(g, i, i);
  }
  vi t(n);
  copy(s.begin(), s.end(), t.begin());
  s.assign(n, -1);
  for (int i = 0; i < n; i++) {
    if (s[i] < 0)
      dfs(h, i, i);
  }

  map<pair<int, int>, int> m;
  for (int i = 0; i < n; i++) {
    m[make_pair(t[i], s[i])]++;
  }
  for (int i = 0; i < n; i++) {
    pair<int, int> p = make_pair(t[i], s[i]);
    cout << m[p] << " ";
  }
  cout << endl;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <vector>

using namespace std;

struct UnionFind {
  vector<int> par;
  vector<int> height;

  UnionFind(int n) {
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      height.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (height[x] < height[y])
      par[x] = y;
    else {
      par[y] = x;
      if (height[x] == height[y])
        height[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  int N, K, L;
  int tmp_i, tmp_j;

  cin >> N >> K >> L;

  UnionFind road(K + 5);
  UnionFind rail(L + 5);

  for (int i = 1; i <= K; i++) {
    cin >> tmp_i >> tmp_j;
    road.unite(tmp_i, tmp_j);
  }
  for (int i = 1; i <= L; i++) {
    cin >> tmp_i >> tmp_j;
    rail.unite(tmp_i, tmp_j);
  }

  map<pair<int, int>, int> link;

  for (int i = 1; i <= N; i++) {
    link[make_pair(road.find(i), rail.find(i))]++;
  }

  for (int i = 1; i <= N; i++)
    cout << link[make_pair(road.find(i), rail.find(i))] << " ";

  cout << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <map>
#include <vector>

using namespace std;

struct UnionFind {
  vector<int> par;
  vector<int> height;

  UnionFind(int n) {
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      height.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (height[x] < height[y])
      par[x] = y;
    else {
      par[y] = x;
      if (height[x] == height[y])
        height[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  int N, K, L;
  int tmp_i, tmp_j;

  cin >> N >> K >> L;

  UnionFind road(N + 5);
  UnionFind rail(N + 5);

  for (int i = 1; i <= K; i++) {
    cin >> tmp_i >> tmp_j;
    road.unite(tmp_i, tmp_j);
  }
  for (int i = 1; i <= L; i++) {
    cin >> tmp_i >> tmp_j;
    rail.unite(tmp_i, tmp_j);
  }

  map<pair<int, int>, int> link;

  for (int i = 1; i <= N; i++) {
    link[make_pair(road.find(i), rail.find(i))]++;
  }

  for (int i = 1; i <= N; i++)
    cout << link[make_pair(road.find(i), rail.find(i))] << " ";

  cout << endl;

  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <utility>
using namespace std;
typedef pair<int, int> P;

class UnionFind {
  int par[100002], rank[100002];

public:
  UnionFind(int n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
    }
  }

  int find(int i) {
    if (i == par[i])
      return i;
    return par[i] = find(par[i]);
  }

  bool same(int i, int j) { return par[i] == par[j]; }

  void unite(int i, int j) {
    if (same(i, j))
      return;
    i = find(i), j = find(j);
    if (rank[i] < rank[j])
      par[i] = j;
    else {
      par[j] = i;
      if (rank[i] == rank[j])
        rank[i]++;
    }
  }
};

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  int p[100002], q[100002], r[100001], s[100002];
  UnionFind u(n), t(n);
  for (int i = 0; i < k; i++) {
    cin >> p[i] >> q[i];
    p[i]--;
    q[i]--;
    u.unite(p[i], q[i]);
  }
  for (int i = 0; i < l; i++) {
    cin >> r[i] >> s[i];
    r[i]--;
    s[i]--;
    t.unite(r[i], s[i]);
  }
  map<P, int> mp;
  for (int i = 0; i < n; i++) {
    mp[P(u.find(i), t.find(i))]++;
  }
  for (int i = 0; i < n; i++) {
    cout << mp[P(u.find(i), t.find(i))] << endl;
  }
}

Fixed submission:
#include <iostream>
#include <map>
#include <utility>
using namespace std;
typedef pair<int, int> P;

class UnionFind {
  int par[200002], rank[200002];

public:
  UnionFind(int n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
    }
  }

  int find(int i) {
    if (i == par[i])
      return i;
    return par[i] = find(par[i]);
  }

  bool same(int i, int j) { return par[i] == par[j]; }

  void unite(int i, int j) {
    if (same(i, j))
      return;
    i = find(i), j = find(j);
    if (rank[i] < rank[j])
      par[i] = j;
    else {
      par[j] = i;
      if (rank[i] == rank[j])
        rank[i]++;
    }
  }
};

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  int p[100002], q[100002], r[100001], s[100002];
  UnionFind u(n), t(n);
  for (int i = 0; i < k; i++) {
    cin >> p[i] >> q[i];
    p[i]--;
    q[i]--;
    u.unite(p[i], q[i]);
  }
  for (int i = 0; i < l; i++) {
    cin >> r[i] >> s[i];
    r[i]--;
    s[i]--;
    t.unite(r[i], s[i]);
  }
  map<P, int> mp;
  for (int i = 0; i < n; i++) {
    mp[P(u.find(i), t.find(i))]++;
  }
  for (int i = 0; i < n; i++) {
    cout << mp[P(u.find(i), t.find(i))] << endl;
  }
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define ll long long;
#define N 100010
class UF {
public:
  int parent[N], rank[N], size[N];
  UF(int n) {
    for (int i = 0; i <= n; i++) {
      parent[i] = i;
      rank[i] = 0;
      size[i] = 1;
    }
  }

  int find(int x) {
    if (parent[x] == x)
      return x;
    else
      return parent[x] = find(parent[x]);
  }

  bool same(int x, int y) { return find(x) == find(y); }

  void merge(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      parent[x] = y;
      size[y] += size[x];
    } else {
      parent[y] = x;
      size[x] += size[y];
      if (rank[x] == rank[y]) {
        rank[x]++;
      }
    }

    return;
  }
  int getSize(int x) { return size[find(x)]; }
};

map<pair<int, int>, int> tb;
int main(void) {
  int n, k, l, fs, sc;
  cin >> n >> k >> l;
  UF uf1(n);
  UF uf2(n);
  for (int i = 0; i < k; i++) {
    cin >> fs >> sc;
    uf1.merge(fs - 1, sc - 1);
  }
  for (int i = 0; i < l; i++) {
    cin >> fs >> sc;
    uf2.merge(fs - 1, sc - 1);
  }
  for (int i = 0; i < n; i++) {
    tb[make_pair(uf1.find(i), uf2.find(i))]++;
  }
  for (int i = 0; i < n; i++) {
    cout << tb[make_pair(uf1.find(i), uf2.find(i))] << " ";
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define ll long long;
#define N 200010
class UF {
public:
  int parent[N], rank[N], size[N];
  UF(int n) {
    for (int i = 0; i <= n; i++) {
      parent[i] = i;
      rank[i] = 0;
      size[i] = 1;
    }
  }

  int find(int x) {
    if (parent[x] == x)
      return x;
    else
      return parent[x] = find(parent[x]);
  }

  bool same(int x, int y) { return find(x) == find(y); }

  void merge(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      parent[x] = y;
      size[y] += size[x];
    } else {
      parent[y] = x;
      size[x] += size[y];
      if (rank[x] == rank[y]) {
        rank[x]++;
      }
    }

    return;
  }
  int getSize(int x) { return size[find(x)]; }
};

map<pair<int, int>, int> tb;
int main(void) {
  int n, k, l, fs, sc;
  cin >> n >> k >> l;
  UF uf1(n);
  UF uf2(n);
  for (int i = 0; i < k; i++) {
    cin >> fs >> sc;
    uf1.merge(fs - 1, sc - 1);
  }
  for (int i = 0; i < l; i++) {
    cin >> fs >> sc;
    uf2.merge(fs - 1, sc - 1);
  }
  for (int i = 0; i < n; i++) {
    tb[make_pair(uf1.find(i), uf2.find(i))]++;
  }
  for (int i = 0; i < n; i++) {
    cout << tb[make_pair(uf1.find(i), uf2.find(i))] << " ";
  }
  cout << endl;
  return 0;
}