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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(0);
#define tie cin.tie(0);
#define x first
#define y second
#define ll long long
#define mod 666013

using namespace std;
int n, road[100100], m, rail[100100], k;
int x, a, b;
int A[200005];
map<pair<int, int>, int> M;

int find1(int nod) {
  if (road[nod] == nod)
    return nod;
  int p = find1(road[nod]);
  road[nod] = p;
  return p;
}

void unite1(int a, int b) {
  a = find1(a);
  b = find1(b);
  road[a] = b;
}

int find2(int nod) {
  if (rail[nod] == nod)
    return nod;
  int p = find2(rail[nod]);
  rail[nod] = p;
  return p;
}

void unite2(int a, int b) {
  a = find2(a);
  b = find2(b);
  rail[a] = b;
}

int main() {

  ios_base::sync_with_stdio(0);

  cin >> n >> m >> k;

  for (int i = 1; i <= n; i++)
    road[i] = rail[i] = i;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    unite1(a, b);
  }
  for (int i = 1; i <= k; i++) {
    cin >> a >> b;
    unite2(a, b);
  }

  for (int i = 1; i <= n; i++)
    M[{find1(i), find2(i)}]++;
  for (int i = 1; i <= n; i++)
    cout << M[{find1(i), find2(i)}] << " ";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(0);
#define tie cin.tie(0);
#define x first
#define y second
#define ll long long
#define mod 666013

using namespace std;
int n, road[200100], m, rail[200100], k;
int x, a, b;
int A[200005];
map<pair<int, int>, int> M;

int find1(int nod) {
  if (road[nod] == nod)
    return nod;
  int p = find1(road[nod]);
  road[nod] = p;
  return p;
}

void unite1(int a, int b) {
  a = find1(a);
  b = find1(b);
  road[a] = b;
}

int find2(int nod) {
  if (rail[nod] == nod)
    return nod;
  int p = find2(rail[nod]);
  rail[nod] = p;
  return p;
}

void unite2(int a, int b) {
  a = find2(a);
  b = find2(b);
  rail[a] = b;
}

int main() {

  ios_base::sync_with_stdio(0);

  cin >> n >> m >> k;

  for (int i = 1; i <= n; i++)
    road[i] = rail[i] = i;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    unite1(a, b);
  }
  for (int i = 1; i <= k; i++) {
    cin >> a >> b;
    unite2(a, b);
  }

  for (int i = 1; i <= n; i++)
    M[{find1(i), find2(i)}]++;
  for (int i = 1; i <= n; i++)
    cout << M[{find1(i), find2(i)}] << " ";
  return 0;
}
Problem p03855 (C++)

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

class UnionFind {
  vector<int> rank, root;

public:
  UnionFind(int n) {
    rank.resize(n, 0);
    root.resize(n);
    iota(root.begin(), root.end(), 0);
  }
  int find(int i) {
    if (root[i] == i)
      return i;
    else
      return root[i] = find(root[i]);
  }
  void join(int i, int j) {
    i = root[i];
    j = root[j];
    if (i == j)
      return;
    if (rank[i] > rank[j]) {
      root[j] = i;
    } else {
      root[i] = j;
      if (rank[i] == rank[j])
        rank[j]++;
    }
  }
};

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  UnionFind uf1(n + 1), uf2(n + 1);
  while (k--) {
    int a, b;
    cin >> a >> b;
    uf1.join(a, b);
  }
  while (l--) {
    int a, b;
    cin >> a >> b;
    uf2.join(a, b);
  }
  map<pair<int, int>, int> cnt;
  for (int i = 1; i <= n; i++)
    cnt[{uf1.find(i), uf2.find(i)}]++;
  for (int i = 1; i <= n; i++)
    cout << cnt[{uf1.find(i), uf2.find(i)}] << (i == n ? '\n' : ' ');
}


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

class UnionFind {
  vector<int> rank, root;

public:
  UnionFind(int n) {
    rank.resize(n, 0);
    root.resize(n);
    iota(root.begin(), root.end(), 0);
  }
  int find(int i) {
    if (root[i] == i)
      return i;
    else
      return root[i] = find(root[i]);
  }
  void join(int i, int j) {
    i = find(i);
    j = find(j);
    if (i == j)
      return;
    if (rank[i] > rank[j]) {
      root[j] = i;
    } else {
      root[i] = j;
      if (rank[i] == rank[j])
        rank[j]++;
    }
  }
};

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  UnionFind uf1(n + 1), uf2(n + 1);
  while (k--) {
    int a, b;
    cin >> a >> b;
    uf1.join(a, b);
  }
  while (l--) {
    int a, b;
    cin >> a >> b;
    uf2.join(a, b);
  }
  map<pair<int, int>, int> cnt;
  for (int i = 1; i <= n; i++)
    cnt[{uf1.find(i), uf2.find(i)}]++;
  for (int i = 1; i <= n; i++)
    cout << cnt[{uf1.find(i), uf2.find(i)}] << (i == n ? '\n' : ' ');
}
Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

using ll = long long;

int N, K, L, s, t;
std::vector<std::vector<int>> road, railway;
int c1[2 * 100001], c2[2 * 100001];

void dfs(std::vector<std::vector<int>> g, int c[], int v, int id) {
  c[v] = id;

  for (int i = 0; i < g[v].size(); ++i) {
    int nv = g[v][i];

    if (c[nv] == -1) {
      dfs(g, c, nv, id);
    }
  }
}

int main() {
  scanf("%d%d%d", &N, &K, &L);
  road.resize(N);
  railway.resize(N);
  for (int i = 0; i < K; ++i) {
    scanf("%d%d", &s, &t);
    s--, t--;
    road[s].push_back(t);
    road[t].push_back(s);
  }
  for (int i = 0; i < L; ++i) {
    scanf("%d%d", &s, &t);
    s--, t--;
    railway[s].push_back(t);
    railway[t].push_back(s);
  }

  std::memset(c1, -1, sizeof c1);
  std::memset(c2, -1, sizeof c2);

  int id = 0;

  for (int i = 0; i < N; ++i) {
    if (c1[i] == -1) {
      dfs(road, c1, i, id++);
    }
  }

  id = 0;

  for (int i = 0; i < N; ++i) {
    if (c2[i] == -1) {
      dfs(railway, c2, i, id++);
    }
  }

  std::map<std::pair<int, int>, int> mp;

  for (int i = 0; i < N; ++i) {
    mp[{c1[i], c2[i]}]++;
  }

  for (int i = 0; i < N; ++i) {
    printf("%d%c", mp[{c1[i], c2[i]}], (i == N - 1 ? '\n' : ' '));
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

using ll = long long;

int N, K, L, s, t;
std::vector<std::vector<int>> road, railway;
int c1[2 * 100001], c2[2 * 100001];

void dfs(std::vector<std::vector<int>> &g, int c[], int v, int id) {
  c[v] = id;

  for (int i = 0; i < g[v].size(); ++i) {
    int nv = g[v][i];

    if (c[nv] == -1) {
      dfs(g, c, nv, id);
    }
  }
}

int main() {
  scanf("%d%d%d", &N, &K, &L);
  road.resize(N);
  railway.resize(N);
  for (int i = 0; i < K; ++i) {
    scanf("%d%d", &s, &t);
    s--, t--;
    road[s].push_back(t);
    road[t].push_back(s);
  }
  for (int i = 0; i < L; ++i) {
    scanf("%d%d", &s, &t);
    s--, t--;
    railway[s].push_back(t);
    railway[t].push_back(s);
  }

  std::memset(c1, -1, sizeof c1);
  std::memset(c2, -1, sizeof c2);

  int id = 0;

  for (int i = 0; i < N; ++i) {
    if (c1[i] == -1) {
      dfs(road, c1, i, id++);
    }
  }

  id = 0;

  for (int i = 0; i < N; ++i) {
    if (c2[i] == -1) {
      dfs(railway, c2, i, id++);
    }
  }

  std::map<std::pair<int, int>, int> mp;

  for (int i = 0; i < N; ++i) {
    mp[{c1[i], c2[i]}]++;
  }

  for (int i = 0; i < N; ++i) {
    printf("%d%c", mp[{c1[i], c2[i]}], (i == N - 1 ? '\n' : ' '));
  }

  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct UnionFind {
  vector<int> par, rank;
  UnionFind(int n) : par(n), rank(n) { rep(i, n) par[i] = i; }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      par[x] = y;
    else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

void solve() {
  int n, k, l;
  cin >> n >> k >> l;
  vector<int> p(k), q(k);
  rep(i, k) cin >> p[i] >> q[i];
  vector<int> r(l), s(l);
  rep(i, l) cin >> r[i] >> s[i];
  UnionFind road(n);
  UnionFind rail(n);
  rep(i, k) road.unite(p[i] - 1, q[i] - 1);
  rep(i, k) rail.unite(r[i] - 1, s[i] - 1);
  vector<pii> v(n);
  map<pii, int> mp;
  rep(i, n) {
    v[i].first = road.find(i);
    v[i].second = rail.find(i);
    mp[v[i]]++;
  }
  rep(i, n) { cout << mp[v[i]] << endl; }
}

int main() {
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct UnionFind {
  vector<int> par, rank;
  UnionFind(int n) : par(n), rank(n) { rep(i, n) par[i] = i; }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      par[x] = y;
    else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

void solve() {
  int n, k, l;
  cin >> n >> k >> l;
  vector<int> p(k), q(k);
  rep(i, k) cin >> p[i] >> q[i];
  vector<int> r(l), s(l);
  rep(i, l) cin >> r[i] >> s[i];
  UnionFind road(n);
  UnionFind rail(n);
  rep(i, k) road.unite(p[i] - 1, q[i] - 1);
  rep(i, l) rail.unite(r[i] - 1, s[i] - 1);
  vector<pii> v(n);
  map<pii, int> mp;
  rep(i, n) {
    v[i].first = road.find(i);
    v[i].second = rail.find(i);
    mp[v[i]]++;
  }
  rep(i, n) { cout << mp[v[i]] << endl; }
}

int main() {
  solve();
  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAX (100000)
int ar[MAX], br[MAX];

struct UF {
  int N, K;
  int p[MAX], rank[MAX];
  UF(int N, int K) {
    this->N = N;
    this->K = K;
    for (int i = 0; i < N; i++) {
      p[i] = i;
      rank[i] = 0;
    }
  }
  int find(int i) {
    if (p[i] == i)
      return i;
    else
      return p[i] = find(p[i]);
  }
  bool same(int i, int j) { return find(i) == find(j); }
  void unite(int i, int j) {
    int x = find(i), y = find(j);
    if (x == y)
      return;
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y])
        rank[y]++;
    }
  }
};

int main(void) {
  int N, K, L;
  cin >> N >> K >> L;
  UF road(N, K), rail(N, L);
  for (int i = 0; i < K; i++) {
    int p, q;
    cin >> p >> q;
    p--;
    q--;
    road.unite(p, q);
  }
  for (int i = 0; i < L; i++) {
    int p, q;
    cin >> p >> q;
    p--;
    q--;
    rail.unite(p, q);
  }
  map<pair<int, int>, int> m;
  for (int i = 0; i < N; i++) {
    pair<int, int> p = pair<int, int>(road.find(i), rail.find(i));
    if (m.find(p) == m.end()) {
      m[p] = 1;
    } else {
      m[p]++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (i != 0)
      cout << " ";
    cout << m[pair<int, int>(road.find(i), rail.find(i))];
  }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAX (1000000)

struct UF {
  int N, K;
  int p[MAX], rank[MAX];
  UF(int N, int K) {
    this->N = N;
    this->K = K;
    for (int i = 0; i < N; i++) {
      p[i] = i;
      rank[i] = 0;
    }
  }
  int find(int i) {
    if (p[i] == i)
      return i;
    else
      return p[i] = find(p[i]);
  }
  bool same(int i, int j) { return find(i) == find(j); }
  void unite(int i, int j) {
    int x = find(i), y = find(j);
    if (x == y)
      return;
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y])
        rank[y]++;
    }
  }
};

int main(void) {
  int N, K, L;
  cin >> N >> K >> L;
  UF road(N, K), rail(N, L);
  for (int i = 0; i < K; i++) {
    int p, q;
    cin >> p >> q;
    p--;
    q--;
    road.unite(p, q);
  }
  for (int i = 0; i < L; i++) {
    int p, q;
    cin >> p >> q;
    p--;
    q--;
    rail.unite(p, q);
  }
  map<pair<int, int>, int> m;
  for (int i = 0; i < N; i++) {
    pair<int, int> p = pair<int, int>(road.find(i), rail.find(i));
    if (m.find(p) == m.end()) {
      m[p] = 1;
    } else {
      m[p]++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (i != 0)
      cout << " ";
    cout << m[pair<int, int>(road.find(i), rail.find(i))];
  }
  cout << endl;
}
Problem p03855 (C++)

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

int par[MAX_N];
int myrank[MAX_N];

namespace std {
template <> class hash<std::pair<int, int>> {
public:
  size_t operator()(const std::pair<int, int> &x) const {
    return hash<int>()(x.first) ^ hash<int>()(x.second);
  }
};
} // namespace std

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    myrank[i] = 0;
  }
}

int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }

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

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

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

signed main() {
  int N, K, L, a, b;
  // map<pair<int,int>,int> mp;
  unordered_map<pair<int, int>, int> mp;
  cin >> N >> K >> L;

  init(N * 2 + 5);

  rep(i, K) {
    cin >> a >> b;
    unite(a, b);
  }
  rep(i, L) {
    cin >> a >> b;
    unite(a + N, b + N);
  }

  rep(i, N) mp[make_pair(find(i), find(N + i))]++;

  rep(i, N) cout << (i - 1 ? " " : "") << mp[make_pair(find(i), find(N + i))];

  cout << 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++)
#define MAX_N 400100

int par[MAX_N];
int myrank[MAX_N];

namespace std {
template <> class hash<std::pair<int, int>> {
public:
  size_t operator()(const std::pair<int, int> &x) const {
    return hash<int>()(x.first) ^ hash<int>()(x.second);
  }
};
} // namespace std

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    myrank[i] = 0;
  }
}

int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }

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

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

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

signed main() {
  int N, K, L, a, b;
  // map<pair<int,int>,int> mp;
  unordered_map<pair<int, int>, int> mp;
  cin >> N >> K >> L;

  init(N * 2 + 5);

  rep(i, K) {
    cin >> a >> b;
    unite(a, b);
  }
  rep(i, L) {
    cin >> a >> b;
    unite(a + N, b + N);
  }

  rep(i, N) mp[make_pair(find(i), find(N + i))]++;

  rep(i, N) cout << (i - 1 ? " " : "") << mp[make_pair(find(i), find(N + i))];

  cout << endl;

  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using graph = vvi;

graph read_graph(int n_nodes, int n_edges, int off_set) {
  graph g(n_nodes, vi());
  for (int i = 0; i != n_edges; ++i) {
    int a, b;
    cin >> a >> b;
    g[a - off_set].push_back(b - off_set);
    g[b - off_set].push_back(a - off_set);
  }
  return g;
}

vi find_groups(graph g) {
  int n_nodes = g.size();
  vi groups(n_nodes, 0);
  for (int i = 0; i != n_nodes; ++i)
    groups[i] = i;
  for (int v = 0; v != n_nodes; ++v) {
    if (groups[v] != v)
      continue;
    vi stk(g[v]);
    while (not stk.empty()) {
      int u = stk.back();
      stk.pop_back();
      groups[u] = v;
      for (auto w : g[u]) {
        if (groups[w] == w)
          stk.push_back(w);
      }
    }
  }
  return groups;
}

int main() {
  ios::sync_with_stdio(false);
  int N, K, L;
  cin >> N >> K >> L;
  auto bus_routes = read_graph(N, K, 1);
  auto train_routes = read_graph(N, L, 1);
  auto bus_groups = find_groups(bus_routes);
  auto train_groups = find_groups(train_routes);
  vector<unordered_map<int, int>> counter(N);
  for (int v = 0; v != N; ++v) {
    counter[bus_groups[v]][train_groups[v]] += 1;
  }
  for (int v = 0; v != N; ++v) {
    cout << counter[bus_groups[v]][train_groups[v]]
         << (v == N - 1 ? '\n' : ' ');
  }
  return 0;
}


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

using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using graph = vvi;

graph read_graph(int n_nodes, int n_edges, int off_set) {
  graph g(n_nodes, vi());
  for (int i = 0; i != n_edges; ++i) {
    int a, b;
    cin >> a >> b;
    g[a - off_set].push_back(b - off_set);
    g[b - off_set].push_back(a - off_set);
  }
  return g;
}

vi find_groups(graph g) {
  int n_nodes = g.size();
  vi groups(n_nodes, 0);
  for (int i = 0; i != n_nodes; ++i)
    groups[i] = i;
  for (int v = 0; v != n_nodes; ++v) {
    if (groups[v] != v)
      continue;
    vi stk(g[v]);
    while (not stk.empty()) {
      int u = stk.back();
      stk.pop_back();
      groups[u] = v;
      for (auto w : g[u]) {
        if (groups[w] != v)
          stk.push_back(w);
      }
    }
  }
  return groups;
}

int main() {
  ios::sync_with_stdio(false);
  int N, K, L;
  cin >> N >> K >> L;
  auto bus_routes = read_graph(N, K, 1);
  auto train_routes = read_graph(N, L, 1);
  auto bus_groups = find_groups(bus_routes);
  auto train_groups = find_groups(train_routes);
  vector<unordered_map<int, int>> counter(N);
  for (int v = 0; v != N; ++v) {
    counter[bus_groups[v]][train_groups[v]] += 1;
  }
  for (int v = 0; v != N; ++v) {
    cout << counter[bus_groups[v]][train_groups[v]]
         << (v == N - 1 ? '\n' : ' ');
  }
  return 0;
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct UF {
  vector<int> p;
  UF(int n) : p(n, -1) {}
  int r(int x) {
    if (p[x] < 0)
      return x;
    return p[x] = r(p[x]);
  }
  void m(int x, int y) {
    x = r(x);
    y = r(y);
    if (-p[x] > -p[y])
      swap(x, y);
    p[y] += p[x];
    p[x] = y;
  }
};

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  UF a(n), b(n);
  while (k--) {
    int f, t;
    cin >> f >> t;
    f--;
    t--;
    a.m(f, t);
  }
  while (l--) {
    int f, t;
    cin >> f >> t;
    f--;
    t--;
    b.m(f, t);
  }
  map<pair<int, int>, int> m;
  for (int i = 0; i < n; i++) {
    m[{a.r(i), b.r(i)}]++;
  }
  for (int i = 0; i < n; i++) {
    cout << m[{a.r(i), b.r(i)}] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct UF {
  vector<int> p;
  UF(int n) : p(n, -1) {}
  int r(int x) {
    if (p[x] < 0)
      return x;
    return p[x] = r(p[x]);
  }
  void m(int x, int y) {
    x = r(x);
    y = r(y);
    if (x == y)
      return;
    if (-p[x] > -p[y])
      swap(x, y);
    p[y] += p[x];
    p[x] = y;
  }
};

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  UF a(n), b(n);
  while (k--) {
    int f, t;
    cin >> f >> t;
    f--;
    t--;
    a.m(f, t);
  }
  while (l--) {
    int f, t;
    cin >> f >> t;
    f--;
    t--;
    b.m(f, t);
  }
  map<pair<int, int>, int> m;
  for (int i = 0; i < n; i++) {
    m[{a.r(i), b.r(i)}]++;
  }
  for (int i = 0; i < n; i++) {
    cout << m[{a.r(i), b.r(i)}] << endl;
  }
}
Problem p03855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, n) for (int i = (a); i < (n); ++i)
using namespace std;
typedef long long ll;

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) { return par[x] < 0 ? x : par[x] = root(par[x]); }

  bool is_same(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};

void Main() {
  int N, K, L;
  cin >> N >> K >> L;
  UnionFind uf1(N);
  rep(i, K) {
    int p, q;
    cin >> p >> q;
    --p, --q;
    uf1.merge(p, q);
  }
  UnionFind uf2(N);
  rep(i, K) {
    int p, q;
    cin >> p >> q;
    --p, --q;
    uf2.merge(p, q);
  }
  map<pair<int, int>, int> mp;
  rep(i, N) { ++mp[make_pair(uf1.root(i), uf2.root(i))]; }
  rep(i, N) {
    cout << mp[make_pair(uf1.root(i), uf2.root(i))];
    if (i != N - 1)
      cout << " ";
  }
  cout << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}


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

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) { return par[x] < 0 ? x : par[x] = root(par[x]); }

  bool is_same(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};

void Main() {
  int N, K, L;
  cin >> N >> K >> L;
  UnionFind uf1(N);
  rep(i, K) {
    int p, q;
    cin >> p >> q;
    --p, --q;
    uf1.merge(p, q);
  }
  UnionFind uf2(N);
  rep(i, L) {
    int p, q;
    cin >> p >> q;
    --p, --q;
    uf2.merge(p, q);
  }
  map<pair<int, int>, int> mp;
  rep(i, N) { ++mp[make_pair(uf1.root(i), uf2.root(i))]; }
  rep(i, N) {
    cout << mp[make_pair(uf1.root(i), uf2.root(i))];
    if (i != N - 1)
      cout << " ";
  }
  cout << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
Problem p03856 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include "IntMod.h"
// typedef IntMod<1000000007> MInt;

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define REP(i, a, n) for (int i = a; i < n; ++i)
#define REPM(i, n, a) for (int i = n - 1; i >= a; --i)
#define EPS 0.0001
#define INF 0x3FFFFFFF
#define INFD 1.0e+308
typedef long long LL;
typedef unsigned long long ULL;

typedef pair<LL, LL> PP;

string S;
int main() {
  cin >> S;
  reverse(S.begin(), S.end());
  int i = 0;
  bool flg = true;
  while (i < S.size()) {
    switch (S[i]) {
    case 'e':
      if (S.substr(i, 5) != "esare")
        flg = false;
      i += 5;
      break;
    case 'm':
      if (S.substr(i, 5) != "maerd")
        flg = false;
      i += 5;
      break;
    case 'r':
      if (S[i + 1] == 'e') {
        if (S.substr(i, 6) == "resare")
          i += 6;
        else if (S.substr(i, 7) == "remaerd")
          i += 7;
        else {
          flg = false;
          i = INF;
        }
      } else {
        flg = false;
      }
      break;
    default:
      flg = false;
      i = INF;
    }
  }
  cout << (flg ? "YES" : "NO") << endl;
  return 0;
}

Fixed submission:
// #include "IntMod.h"
// typedef IntMod<1000000007> MInt;

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define REP(i, a, n) for (int i = a; i < n; ++i)
#define REPM(i, n, a) for (int i = n - 1; i >= a; --i)
#define EPS 0.0001
#define INF 0x3FFFFFFF
#define INFD 1.0e+308
typedef long long LL;
typedef unsigned long long ULL;

typedef pair<LL, LL> PP;

string S;
int main() {
  cin >> S;
  reverse(S.begin(), S.end());
  int i = 0;
  bool flg = true;
  while (i < S.size()) {
    switch (S[i]) {
    case 'e':
      if (S.substr(i, 5) != "esare")
        flg = false;
      i += 5;
      break;
    case 'm':
      if (S.substr(i, 5) != "maerd")
        flg = false;
      i += 5;
      break;
    case 'r':
      if (S[i + 1] == 'e') {
        if (S.substr(i, 6) == "resare")
          i += 6;
        else if (S.substr(i, 7) == "remaerd")
          i += 7;
        else {
          flg = false;
          i = INF;
        }
      } else {
        flg = false;
        i = INF;
      }
      break;
    default:
      flg = false;
      i = INF;
    }
  }
  cout << (flg ? "YES" : "NO") << endl;
  return 0;
}
Problem p03856 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

string S;

int main(int argc, char const *argv[]) {
  cin >> S;
  int size = S.size();
  string s;
  for (int i = 0; i < size; i++)
    s[i] = S[size - i - 1];

  while (s.size()) {
    if (s.substr(0, 5) == "maerd" || s.substr(0, 5) == "esare")
      s = s.substr(5);
    else if (s.substr(0, 6) == "resare")
      s = s.substr(6);
    else if (s.substr(0, 7) == "remaerd")
      s = s.substr(7);
    else {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

string S;

int main(int argc, char const *argv[]) {
  cin >> S;
  int size = S.size();
  string s(size, '0');
  for (int i = 0; i < size; i++)
    s[i] = S[size - i - 1];

  while (s.size()) {
    if (s.substr(0, 5) == "maerd" || s.substr(0, 5) == "esare")
      s = s.substr(5);
    else if (s.substr(0, 6) == "resare")
      s = s.substr(6);
    else if (s.substr(0, 7) == "remaerd")
      s = s.substr(7);
    else {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;

  return 0;
}
Problem p03856 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

#define INF 10e10
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep_r(i, n, m) for (int i = m; i < n; i++)
#define MAX 100

string s;
string t = "";

int main() {
  cin >> s;

  for (int i = s.size() - 1; i >= 0; --i) {
    t += s[i];
    if (t.size() >= 5) {
      string temp = t;
      for (int j = 0; j < t.size() / 2; ++j) {
        swap(temp[j], temp[t.size() - 1 - j]);
      }
      if (temp == "dream") {
        t = "";
      } else if (temp == "dreamer") {
        t = "";
      } else if (temp == "erase") {
        t = "";
      } else if (temp == "eraser") {
        t = "";
      }
    }
  }

  if (t.empty()) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

#define INF 10e10
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep_r(i, n, m) for (int i = m; i < n; i++)
#define MAX 100

string s;
string t = "";

int main() {
  cin >> s;

  for (int i = s.size() - 1; i >= 0; --i) {
    t += s[i];
    if (t.size() >= 5 && t.size() <= 7) {
      string temp = t;
      for (int j = 0; j < t.size() / 2; ++j) {
        swap(temp[j], temp[t.size() - 1 - j]);
      }
      if (temp == "dream") {
        t = "";
      } else if (temp == "dreamer") {
        t = "";
      } else if (temp == "erase") {
        t = "";
      } else if (temp == "eraser") {
        t = "";
      }
    }
  }

  if (t.empty()) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}
Problem p03856 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;

const int maxn = 0;
int n;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  string z;
  cin >> z;
  map<string, bool> mp;
  mp["maerd"] = 1;
  mp["remaerd"] = 1;
  mp["esare"] = 1;
  mp["resare"] = 1;
  reverse(z.begin(), z.end());
  string s = "", ans = "";
  for (int i = 0; i < z.size(); i++) {
    s += z[i];
    if (mp[s]) {
      ans += s;
      s = "";
    }
  }
  if (z == ans)
    cout << "YES";
  else
    cout << "NO";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;

const int maxn = 0;
int n;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  string z;
  cin >> z;
  map<string, bool> mp;
  mp["maerd"] = 1;
  mp["remaerd"] = 1;
  mp["esare"] = 1;
  mp["resare"] = 1;
  reverse(z.begin(), z.end());
  string s = "", ans = "";
  for (int i = 0; i < z.size(); i++) {
    s += z[i];
    if (mp[s]) {
      ans += s;
      s = "";
    }
    if (s.size() > 10)
      break;
  }
  if (z == ans)
    cout << "YES";
  else
    cout << "NO";
}
Problem p03856 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
using namespace std;
#define int long long int
#define inf 1000000000000000
int function(string str, int initial, int final, int memo[]) {
  if (initial == final + 1) {
    return 1;
  }
  if (initial > final)
    return 0;
  if (memo[initial] != -1)
    return memo[initial];
  else {
    string s = "";
    int j;
    for (j = initial; str[j] != '\0' && j <= initial + 4; j++) {
      s += str[j];
    }
    int a = 0, b = 0, c = 0, d = 0;
    if (s == "dream") {
      a = function(str, initial + 5, final, memo);
    }
    if (s == "erase") {
      b = function(str, initial + 5, final, memo);
    }
    s = "";
    for (j = initial; str[j] != '\0' && j <= initial + 5; j++)
      s += str[j];
    if (s == "eraser") {
      c = function(str, initial + 6, final, memo);
    }
    s = "";
    for (j = initial; str[j] != '\0' && j <= initial + 6; j++)
      s += str[j];
    if (s == "dreamer")
      d = function(str, initial + 7, final, memo);
    if (a == 1 || b == 1 || c == 1 || d == 1)
      memo[initial] = 1;
    else
      memo[initial] = 0;
    return memo[initial];
  }
}
int32_t main() {
  string str;
  cin >> str;
  int n = str.size();
  int memo[n + 1];
  int j;
  for (j = 0; j <= n; j++) {
    memo[j] = -1;
  }
  int result = function(str, 0, n - 1, memo);
  if (result == 1)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
#define int long long int
#define inf 1000000000000000
int function(string &str, int initial, int final, int memo[]) {
  if (initial == final + 1) {
    return 1;
  }
  if (initial > final)
    return 0;
  if (memo[initial] != -1)
    return memo[initial];
  else {
    string s = "";
    int j;
    for (j = initial; str[j] != '\0' && j <= initial + 4; j++) {
      s += str[j];
    }
    int a = 0, b = 0, c = 0, d = 0;
    if (s == "dream") {
      a = function(str, initial + 5, final, memo);
    }
    if (s == "erase") {
      b = function(str, initial + 5, final, memo);
    }
    s = "";
    for (j = initial; str[j] != '\0' && j <= initial + 5; j++)
      s += str[j];
    if (s == "eraser") {
      c = function(str, initial + 6, final, memo);
    }
    s = "";
    for (j = initial; str[j] != '\0' && j <= initial + 6; j++)
      s += str[j];
    if (s == "dreamer")
      d = function(str, initial + 7, final, memo);
    if (a == 1 || b == 1 || c == 1 || d == 1)
      memo[initial] = 1;
    else
      memo[initial] = 0;
    return memo[initial];
  }
}
int32_t main() {
  string str;
  cin >> str;
  int n = str.size();
  int memo[n + 1];
  int j;
  for (j = 0; j <= n; j++) {
    memo[j] = -1;
  }
  int result = function(str, 0, n - 1, memo);
  if (result == 1)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
Problem p03856 (C++)

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

int main() {
  string s;
  cin >> s;
  string dic[] = {"maerd", "remaerd", "esare", "resare"};
  reverse(s.begin(), s.end());
  for (int i = 0; i < s.size(); i++) {
    bool no = true;
    for (int j = 0; j < dic->size(); j++) {
      if (i + dic[j].size() - 1 < s.size() &&
          s.substr(i, dic[j].size()) == dic[j])
        i += dic[j].size() - 1, no = false;
    }
    if (no) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;

  return 0;
}

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

int main() {
  string s;
  cin >> s;
  string dic[] = {"maerd", "remaerd", "esare", "resare"};
  reverse(s.begin(), s.end());
  for (int i = 0; i < s.size(); i++) {
    bool no = true;
    for (int j = 0; j < 4; j++) {
      if (i + dic[j].size() - 1 < s.size() &&
          s.substr(i, dic[j].size()) == dic[j])
        i += dic[j].size() - 1, no = false;
    }
    if (no) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;

  return 0;
}
Problem p03856 (C++)

Buggy submission (status: Runtime Error):
#include "iostream"
using namespace std;

char problem[10011];
int far;

int main() {
  cin >> problem;
  for (int i = 0; problem[i] != NULL; i++) {
    far = i;
  }
  for (int i = 0; i <= far;) {
    // cout << i << "\n";
    if (problem[i] == 'd') {
      if (problem[i + 1] == 'r' && problem[i + 2] == 'e' &&
          problem[i + 3] == 'a' && problem[i + 4] == 'm') {
        if (problem[i + 5] == 'e' && problem[i + 6] == 'r') {
          if (problem[i + 7] == 'a')
            i += 5;
          else
            i += 7;
        } else
          i += 5;
      } else {
        cout << "NO\n";
        return 0;
      }
    } else if (problem[i] == 'e') {
      if (problem[i + 1] == 'r' && problem[i + 2] == 'a' &&
          problem[i + 3] == 's' && problem[i + 4] == 'e') {
        if (problem[i + 5] == 'r')
          i += 6;
        else
          i += 5;
      } else {
        cout << "NO\n";
        return 0;
      }
    } else {
      cout << "NO\n";
      return 0;
    }
  }
  cout << "YES\n";
  return 0;
}

Fixed submission:
#include "iostream"
using namespace std;

char problem[100011];
int far;

int main() {
  cin >> problem;
  for (int i = 0; problem[i] != NULL; i++) {
    far = i;
  }
  for (int i = 0; i <= far;) {
    // cout << i << "\n";
    if (problem[i] == 'd') {
      if (problem[i + 1] == 'r' && problem[i + 2] == 'e' &&
          problem[i + 3] == 'a' && problem[i + 4] == 'm') {
        if (problem[i + 5] == 'e' && problem[i + 6] == 'r') {
          if (problem[i + 7] == 'a')
            i += 5;
          else
            i += 7;
        } else
          i += 5;
      } else {
        cout << "NO\n";
        return 0;
      }
    } else if (problem[i] == 'e') {
      if (problem[i + 1] == 'r' && problem[i + 2] == 'a' &&
          problem[i + 3] == 's' && problem[i + 4] == 'e') {
        if (problem[i + 5] == 'r')
          i += 6;
        else
          i += 5;
      } else {
        cout << "NO\n";
        return 0;
      }
    } else {
      cout << "NO\n";
      return 0;
    }
  }
  cout << "YES\n";
  return 0;
}
Problem p03856 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define ll long long
using namespace std;
static const int INF = (1 << 21);

int main() {
  string S;
  cin >> S;
  int ptr = 0;
  while (1) {
    if (S[ptr] == 'd') {
      if (S.substr(ptr, 5) == "dream") {
        if (S.substr(ptr + 5, 2) == "er" && S.substr(ptr + 7, 3) != "ase")
          ptr += 2;
        ptr += 5;
      } else {
        cout << "NO" << endl;
        return 0;
      }
    } else if (S[ptr] == 'e') {
      if (S.substr(ptr, 5) == "erase") {
        if (S.substr(ptr + 5, 1) == "r")
          ptr += 1;
        ptr += 5;
      } else {
        cout << "NO" << endl;
        return 0;
      }
    } else {
      cout << "NO" << endl;
    }

    if (ptr >= S.length()) {
      cout << "YES" << endl;
      return 0;
    }
  }
}

Fixed submission:
#include "bits/stdc++.h"
#define ll long long
using namespace std;
static const int INF = (1 << 21);

int main() {
  string S;
  cin >> S;
  int ptr = 0;
  while (1) {
    if (S[ptr] == 'd') {
      if (S.substr(ptr, 5) == "dream") {
        if (S.substr(ptr + 5, 2) == "er" && S.substr(ptr + 7, 3) != "ase")
          ptr += 2;
        ptr += 5;
      } else {
        cout << "NO" << endl;
        return 0;
      }
    } else if (S[ptr] == 'e') {
      if (S.substr(ptr, 5) == "erase") {
        if (S.substr(ptr + 5, 1) == "r")
          ptr += 1;
        ptr += 5;
      } else {
        cout << "NO" << endl;
        return 0;
      }
    } else {
      cout << "NO" << endl;
      return 0;
    }

    if (ptr >= S.length()) {
      cout << "YES" << endl;
      return 0;
    }
  }
}
Problem p03856 (C++)

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

int main() {
  string s;
  cin >> s;
  bool t = true;
  for (int i = 0; i < s.size();) {
    string sb = s.substr(i, 5);
    if (sb == "dream") {
      if (s.substr(i + 5, 2) == "er") {
        if (s[i + 7] == 'a')
          i += 5;
        else
          i += 7;
      }
    } else if (sb == "erase") {
      if (s[i + 5] == 'r')
        i += 6;
      else
        i += 5;
    } else {
      t = false;
      break;
    }
  }
  puts(t ? "YES" : "NO");
}

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

int main() {
  string s;
  cin >> s;
  bool t = true;
  for (int i = 0; i < s.size();) {
    string sb = s.substr(i, 5);
    if (sb == "dream") {
      if (s.substr(i + 5, 2) == "er") {
        if (s[i + 7] == 'a')
          i += 5;
        else
          i += 7;
      } else
        i += 5;
    } else if (sb == "erase") {
      if (s[i + 5] == 'r')
        i += 6;
      else
        i += 5;
    } else {
      t = false;
      break;
    }
  }
  puts(t ? "YES" : "NO");
}
Problem p03856 (C++)

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

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

  bool flag = true;
  bool preD = false;
  for (int i = 0; flag && i < s.size();) {
    if (s[i] == 'd') {
      string sub = s.substr(i, 5);
      if (sub == "dream")
        i += 5;
      else
        flag = false;
      preD = true;
    } else if (s[i] == 'e') {
      string sub = s.substr(i, 6);
      if (sub == "eraser") {
        i += 6;
        preD = false;
        continue;
      }
      sub = s.substr(i, 5);
      if (sub == "erase") {
        i += 5;
        preD = false;
        continue;
      }
      sub = s.substr(i, 2);
      if (sub == "er" && preD) {
        i += 2;
        preD = false;
        continue;
      }
      flag = false;
    }
  }
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}


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

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

  bool flag = true;
  bool preD = false;
  for (int i = 0; flag && i < s.size();) {
    if (s[i] == 'd') {
      string sub = s.substr(i, 5);
      if (sub == "dream")
        i += 5;
      else
        flag = false;
      preD = true;
    } else if (s[i] == 'e') {
      string sub = s.substr(i, 6);
      if (sub == "eraser") {
        i += 6;
        preD = false;
        continue;
      }
      sub = s.substr(i, 5);
      if (sub == "erase") {
        i += 5;
        preD = false;
        continue;
      }
      sub = s.substr(i, 2);
      if (sub == "er" && preD) {
        i += 2;
        preD = false;
        continue;
      }
      flag = false;
    } else
      flag = false;
  }
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}
Problem p03856 (C++)

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

string a;
int main() {
  int i;
  cin >> a;
  for (i = a.size(); i > 0;) {
    if (i >= 5 && a.substr(i - 5, 5) == "dream")
      i -= 5;
    else if (i >= 5 && a.substr(i - 5, 5) == "erase")
      i -= 5;
    else if (i >= 7 && a.substr(i - 7, 7) == "dreamer")
      i -= 7;
    else if (i >= 6 && a.substr(i - 6, 6) == "eraser")
      i -= 6;
    else
      return puts("NO");
  }
  cout << "YES";
  return 0;
}

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

string a;
int main() {
  int i;
  cin >> a;
  for (i = a.size(); i > 0;) {
    if (i >= 5 && a.substr(i - 5, 5) == "dream")
      i -= 5;
    else if (i >= 5 && a.substr(i - 5, 5) == "erase")
      i -= 5;
    else if (i >= 7 && a.substr(i - 7, 7) == "dreamer")
      i -= 7;
    else if (i >= 6 && a.substr(i - 6, 6) == "eraser")
      i -= 6;
    else {
      puts("NO");
      return 0;
    }
  }
  cout << "YES";
  return 0;
}
Problem p03856 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
#define MAX_MOD 1000000007
#define REP(i, n) for (long long i = 0; i < n; ++i)
#define LONG_INF 1000000000
int main() {
  string ans[4] = {"maerd", "remaerd", "esare", "resare"};
  string s;
  cin >> s;
  for (int i = s.length() - 1; i >= 0; --i) {
    for (int q = 0; q < 4; ++q) {
      int hoge = i - ans[q].length();
      if (hoge >= -1) {
        for (int j = 0; j < ans[q].length(); ++j) {
          if (ans[q][j] != s[i - j])
            goto omg;
        }
        i -= ans[i].length() - 1;
        goto ok;
      }
    omg:;
    }
    cout << "NO" << endl;
    return 0;
  ok:;
  }
  cout << "YES" << endl;
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
#define MAX_MOD 1000000007
#define REP(i, n) for (long long i = 0; i < n; ++i)
#define LONG_INF 1000000000
int main() {
  string ans[4] = {"maerd", "remaerd", "esare", "resare"};
  string s;
  cin >> s;
  for (int i = s.length() - 1; i >= 0; --i) {
    for (int q = 0; q < 4; ++q) {
      int hoge = i - ans[q].length();
      if (hoge >= -1) {
        for (int j = 0; j < ans[q].length(); ++j) {
          if (ans[q][j] != s[i - j])
            goto omg;
        }
        i -= ans[q].length() - 1;
        goto ok;
      }
    omg:;
    }
    cout << "NO" << endl;
    return 0;
  ok:;
  }
  cout << "YES" << endl;
  return 0;
}
Problem p03857 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n;
#define ran 111111
struct graph {
  int id[ran];
  int ancestor(int x) { return x - id[x] ? id[x] = ancestor(id[x]) : x; }
  void init(int m) {
    for (int i = 1; i <= n; i++)
      id[i] = i;
    while (m--) {
      int x, y;
      scanf("%d%d", &x, &y);
      id[ancestor(x)] = ancestor(y);
    }
    for (int i = 1; i <= n; i++)
      ancestor(i);
  }
} g1, g2;
map<pair<int, int>, int> Map;
int main() {
  int m1, m2;
  scanf("%d%d%d", &n, &m1, &m2);
  g1.init(m1);
  g2.init(m2);
  for (int i = 1; i <= n; i++) {
    Map[make_pair(g1.id[i], g2.id[i])]++;
  }
  for (int i = 1; i <= n; i++)
    printf("%d%c", Map[make_pair(g1.id[i], g2.id[i])], i < n ? ' ' : '\n');
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n;
#define ran 222222
struct graph {
  int id[ran];
  int ancestor(int x) { return x - id[x] ? id[x] = ancestor(id[x]) : x; }
  void init(int m) {
    for (int i = 1; i <= n; i++)
      id[i] = i;
    while (m--) {
      int x, y;
      scanf("%d%d", &x, &y);
      id[ancestor(x)] = ancestor(y);
    }
    for (int i = 1; i <= n; i++)
      ancestor(i);
  }
} g1, g2;
map<pair<int, int>, int> Map;
int main() {
  int m1, m2;
  scanf("%d%d%d", &n, &m1, &m2);
  g1.init(m1);
  g2.init(m2);
  for (int i = 1; i <= n; i++) {
    Map[make_pair(g1.id[i], g2.id[i])]++;
  }
  for (int i = 1; i <= n; i++)
    printf("%d%c", Map[make_pair(g1.id[i], g2.id[i])], i < n ? ' ' : '\n');
  return 0;
}
Problem p03857 (C++)

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

using namespace std;

// #define FILE_IO

typedef pair<int, int> pii;

int N, M, K;
map<pii, int> mp;

class disjointForest {
public:
  int f[100005];

  disjointForest() {
    for (int i = 1; i <= N; i++)
      f[i] = i;
  }

  int fth(int x) {
    if (x == f[x])
      return x;
    return f[x] = fth(f[x]);
  }

  void unite(int x, int y) {
    int f1 = fth(x);
    int f2 = fth(y);
    if (f1 == f2)
      return;
    f[f1] = f2;
  }
};

int main() {
#ifdef FILE_IO
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  scanf("%d%d%d", &N, &M, &K);

  disjointForest froad;
  for (int i = 1; i <= M; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    froad.unite(x, y);
  }

  disjointForest frailway;
  for (int i = 1; i <= K; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    frailway.unite(x, y);
  }

  for (int i = 1; i <= N; i++)
    mp[{froad.fth(i), frailway.fth(i)}]++;

  for (int i = 1; i <= N; i++)
    printf("%d ", mp[{froad.fth(i), frailway.fth(i)}]);

  return 0;
}


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

using namespace std;

// #define FILE_IO

typedef pair<int, int> pii;

int N, M, K;
map<pii, int> mp;

class disjointForest {
public:
  int f[200005];

  disjointForest() {
    for (int i = 1; i <= N; i++)
      f[i] = i;
  }

  int fth(int x) {
    if (x == f[x])
      return x;
    return f[x] = fth(f[x]);
  }

  void unite(int x, int y) {
    int f1 = fth(x);
    int f2 = fth(y);
    if (f1 == f2)
      return;
    f[f1] = f2;
  }
};

int main() {
#ifdef FILE_IO
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  scanf("%d%d%d", &N, &M, &K);

  disjointForest froad;
  for (int i = 1; i <= M; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    froad.unite(x, y);
  }

  disjointForest frailway;
  for (int i = 1; i <= K; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    frailway.unite(x, y);
  }

  for (int i = 1; i <= N; i++)
    mp[{froad.fth(i), frailway.fth(i)}]++;

  for (int i = 1; i <= N; i++)
    printf("%d ", mp[{froad.fth(i), frailway.fth(i)}]);

  return 0;
}