Problem p03230 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define REP(i, a, b) for (int i = (a), _end_ = (b); i < _end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define X first
#define Y second
#define mp make_pair
#define eb emplace_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}

typedef long long LL;
typedef long double LD;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

const int MAX_N = 1e5 + 5;
const int INF = 0x3f3f3f3f;

int G[500][500];

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

  int n;
  Read(n);
  set<int> s;
  for (int i = 1; i < 500; i++)
    s.insert(i * (i - 1) / 2);
  if (n == 1) {
    puts("Yes");
    printf("2\n");
    printf("1 1\n");
    printf("1 1\n");
  } else if (s.find(n) != s.end()) {
    puts("Yes");
    int k = int(sqrt(n * (n - 1))) + 1;
    printf("%d\n", k);
    int cnt = 0;
    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        if (i != j && !G[i][j] && !G[j][i]) {
          G[i][j] = G[j][i] = ++cnt;
        }
      }
    }
    for (int i = 1; i <= k; i++) {
      printf("%d", k - 1);
      for (int j = 1; j <= k; j++) {
        if (i != j)
          printf(" %d", G[i][j]);
      }
      puts("");
    }
  } else
    puts("No");

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define REP(i, a, b) for (int i = (a), _end_ = (b); i < _end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define X first
#define Y second
#define mp make_pair
#define eb emplace_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}

typedef long long LL;
typedef long double LD;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

const int MAX_N = 1e5 + 5;
const int INF = 0x3f3f3f3f;

int G[500][500];

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

  int n;
  Read(n);
  set<int> s;
  for (int i = 1; i < 500; i++)
    s.insert(i * (i - 1) / 2);
  if (n == 1) {
    puts("Yes");
    printf("2\n");
    printf("1 1\n");
    printf("1 1\n");
  } else if (s.find(n) != s.end()) {
    puts("Yes");
    int k = int(sqrt(n * 2)) + 1;
    printf("%d\n", k);
    int cnt = 0;
    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        if (i != j && !G[i][j] && !G[j][i]) {
          G[i][j] = G[j][i] = ++cnt;
        }
      }
    }
    for (int i = 1; i <= k; i++) {
      printf("%d", k - 1);
      for (int j = 1; j <= k; j++) {
        if (i != j)
          printf(" %d", G[i][j]);
      }
      puts("");
    }
  } else
    puts("No");

  return 0;
}