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

#include <bits/stdc++.h>

#ifndef LOCAL_
#define fprintf                                                                \\
  if (false)                                                                   \\
  fprintf
#endif // LOCAL_
#define dump() fprintf(stderr, "#%s.%d\\n", __func__, __LINE__);
#define dumpl(x1)                                                              \\
  fprintf(stderr, "#%s.%d (%s) = (%ld)\\n", __func__, __LINE__, #x1, x1);
#define dumpll(x1, x2)                                                         \\
  fprintf(stderr, "#%s.%d (%s, %s) = (%ld, %ld)\\n", __func__, __LINE__, #x1,   \\
          #x2, x1, x2);
#define dumplll(x1, x2, x3)                                                    \\
  fprintf(stderr, "#%s.%d (%s, %s, %s) = (%ld, %ld, %ld)\\n", __func__,         \\
          __LINE__, #x1, #x2, #x3, x1, x2, x3);
#define dumpd(x1)                                                              \\
  fprintf(stderr, "#%s.%d (%s) = (%lf)\\n", __func__, __LINE__, #x1, x1);
#define dumpdd(x1, x2)                                                         \\
  fprintf(stderr, "#%s.%d (%s, %s) = (%lf, %lf)\\n", __func__, __LINE__, #x1,   \\
          #x2, x1, x2);
#define loop for (;;)
typedef int64_t Int;
typedef std::vector<Int> LI;
const double pi = 3.14159265358979323846264338;
const Int mod = 1000000007;

template <typename T> void scan1(T &x) { fprintf(stderr, "unknown type\\n"); }
template <> void scan1(Int &x) {
  if (scanf("%" PRId64, &x) < 0)
    exit(0);
}
template <> void scan1(std::string &x) {
  if (not(std::cin >> x))
    exit(0);
}
template <> void scan1(LI &xs) {
  for (Int &x : xs)
    scan1(x);
}
void scan() {}
template <typename Head, typename... Tail> void scan(Head &x, Tail &...xs) {
  scan1(x);
  scan(xs...);
}

struct Solver1 {
  Int n;
  std::vector<Int> ps, ns;
  Solver1(Int n_) : n(n_ + 1), ps(n, -1), ns(n, 1) {}
  void unite(Int x, Int y) {
    Int x2 = find(x);
    Int y2 = find(y);
    if (x2 == y2)
      return;
    if (not(ns[x2] <= ns[y2]))
      std::swap(x2, y2);
    ps[x2] = y2;
    ns[y2] += ns[x2];
  }
  bool same(Int x, Int y) { return find(x) == find(y); }
  Int find(Int x) { return ps[x] == -1 ? x : (ps[x] = find(ps[x])); }
  Int getSize(Int x) { return ns[find(x)]; }
};

struct Solver {
  Solver() { fprintf(stderr, "--------Solver begin--------\\n"); }
  ~Solver() { fprintf(stderr, "--------Solver end--------\\n"); }
  void solve() {
    Int n, q;
    scan(n, q);
    Solver1 uf(n);
    for (Int i = 0; i < q; ++i) {
      Int a, b, c;
      scan(a, b, c);
      if (a == 0) {
        uf.unite(b, c);
      }
      if (a == 1) {
        puts(uf.same(b, c) ? "1" : "0");
      }
    }
  }
};

int main() { loop std::unique_ptr<Solver>(new Solver())->solve(); }

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#ifndef LOCAL_
#define fprintf                                                                \\
  if (false)                                                                   \\
  fprintf
#endif // LOCAL_
#define dump() fprintf(stderr, "#%s.%d\\n", __func__, __LINE__);
#define dumpl(x1)                                                              \\
  fprintf(stderr, "#%s.%d (%s) = (%ld)\\n", __func__, __LINE__, #x1, x1);
#define dumpll(x1, x2)                                                         \\
  fprintf(stderr, "#%s.%d (%s, %s) = (%ld, %ld)\\n", __func__, __LINE__, #x1,   \\
          #x2, x1, x2);
#define dumplll(x1, x2, x3)                                                    \\
  fprintf(stderr, "#%s.%d (%s, %s, %s) = (%ld, %ld, %ld)\\n", __func__,         \\
          __LINE__, #x1, #x2, #x3, x1, x2, x3);
#define dumpd(x1)                                                              \\
  fprintf(stderr, "#%s.%d (%s) = (%lf)\\n", __func__, __LINE__, #x1, x1);
#define dumpdd(x1, x2)                                                         \\
  fprintf(stderr, "#%s.%d (%s, %s) = (%lf, %lf)\\n", __func__, __LINE__, #x1,   \\
          #x2, x1, x2);
#define loop for (;;)
typedef int64_t Int;
typedef std::vector<Int> LI;
const double pi = 3.14159265358979323846264338;
const Int mod = 1000000007;

template <typename T> void scan1(T &x) { fprintf(stderr, "unknown type\\n"); }
template <> void scan1(Int &x) {
  if (scanf("%" PRId64, &x) < 0)
    exit(0);
}
template <> void scan1(std::string &x) {
  if (not(std::cin >> x))
    exit(0);
}
template <> void scan1(LI &xs) {
  for (Int &x : xs)
    scan1(x);
}
void scan() {}
template <typename Head, typename... Tail> void scan(Head &x, Tail &...xs) {
  scan1(x);
  scan(xs...);
}

struct Solver1 {
  Int n;
  std::vector<Int> ps, ns;
  Solver1(Int n_) : n(n_ + 1), ps(n, -1), ns(n, 1) {}
  void unite(Int x, Int y) {
    Int x2 = find(x);
    Int y2 = find(y);
    if (not(ns[x2] <= ns[y2]))
      std::swap(x2, y2);
    ps[x2] = y2;
    ns[y2] += ns[x2];
  }
  bool same(Int x, Int y) { return find(x) == find(y); }
  Int find(Int x) { return ps[x] == -1 ? x : (ps[x] = find(ps[x])); }
  Int getSize(Int x) { return ns[find(x)]; }
};

struct Solver {
  Solver() { fprintf(stderr, "--------Solver begin--------\\n"); }
  ~Solver() { fprintf(stderr, "--------Solver end--------\\n"); }
  void solve() {
    Int n, q;
    scan(n, q);
    Solver1 uf(n);
    for (Int i = 0; i < q; ++i) {
      Int a, b, c;
      scan(a, b, c);
      if (a == 0) {
        uf.unite(b, c);
      }
      if (a == 1) {
        puts(uf.same(b, c) ? "1" : "0");
      }
    }
  }
};

int main() { loop std::unique_ptr<Solver>(new Solver())->solve(); }