Problem p03090 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<ll>> vvll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define fin(ans) cout << (ans) << endl
#define STI(s) atoi(s.c_str())
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define itn int
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
struct io {
  io() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  };
};
const int MOD = 1000000007;
const int INF = INT_MAX;
const ll LLINF = 1LL << 62;

int main(void) {

  int n;
  cin >> n;
  vector<vector<int>> allG(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      allG[i].push_back(j);
    }
  }
  vi allsum(n);
  for (int i = 0; i < n; i++) {
    allsum[i] = n * (n + 1) / 2 - (i + 1);
  }
  for (int s = 2 * n; s >= 0; s--) {
    // cout<<s<<endl;
    vvi chG = allG;
    vi sumG = allsum;
    bool ch = true;
    while (ch) {
      bool fin = true;
      for (int i = 0; i < n; i++) {
        if (sumG[i] < s) {
          ch = false;
          break;
        }
      }
      // for(int i=0;i<n;i++){
      //     cout<<sumG[i]<<" ";
      // }
      // cout<<endl;
      int maxpos = 0;
      int maxval = 0;
      int reduce = 0;
      for (int i = 0; i < n; i++) {
        if (maxval <= sumG[i]) {
          maxval = sumG[i];
          maxpos = i;
        }
      }
      // cout<<"AA"<<endl;
      // rem
      bool ischange = false;
      for (int i = 0; i < chG[maxpos].size(); i++) {
        // cout<<"i:"<<i<<endl;
        if (sumG[maxpos] - chG[maxpos][i] - 1 == s) {
          sumG[maxpos] -= chG[maxpos][i] + 1;
          sumG[chG[maxpos][i]] -= maxpos + 1;

          int from = maxpos + 1;
          int to = chG[maxpos][i] + 1;
          // cout<<"FT "<<from<<" "<<to<<endl;

          chG[from - 1].erase(find(all(chG[from - 1]), to - 1));
          chG[to - 1].erase(find(all(chG[to - 1]), from - 1));
          ischange = true;
          break;
        }
      }
      // cout<<"BB"<<endl;
      if (!ischange) {
        Sort(chG[maxpos]);
        sumG[maxpos] -= chG[maxpos][0] + 1;
        chG[maxpos].erase(chG[maxpos].begin());
      }
      for (int i = 0; i < n; i++) {
        if (sumG[i] != s) {
          fin = false;
          break;
        }
      }
      // for(int i=0;i<n;i++){
      //     cout<<"i: "<<i+1<<" =";
      //     for(int j=0;j<chG[i].size();j++){
      //         cout<<chG[i][j]+1<<" ";
      //     }
      //     cout<<endl;
      // }
      if (fin) {
        // 出力
        set<pair<int, int>> sp;
        vector<pair<int, int>> ans;
        for (int i = 0; i < n; i++) {
          for (int j = 0; j < chG[i].size(); j++) {
            if (sp.find(mp(i + 1, chG[i][j] + 1)) != sp.end())
              continue;
            ans.push_back(mp(i + 1, chG[i][j] + 1));
            sp.insert(mp(i + 1, chG[i][j] + 1));
            sp.insert(mp(chG[i][j] + 1, i + 1));
          }
        }
        cout << (int)ans.size() << endl;
        for (int i = 0; i < ans.size(); i++) {
          cout << ans[i].first << " " << ans[i].second << endl;
        }
        return 0;
      }
    }
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<ll>> vvll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define fin(ans) cout << (ans) << endl
#define STI(s) atoi(s.c_str())
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define itn int
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
struct io {
  io() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  };
};
const int MOD = 1000000007;
const int INF = INT_MAX;
const ll LLINF = 1LL << 62;

int main(void) {

  int n;
  cin >> n;
  vector<vector<int>> allG(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      allG[i].push_back(j);
    }
  }
  vi allsum(n);
  for (int i = 0; i < n; i++) {
    allsum[i] = n * (n + 1) / 2 - (i + 1);
  }
  for (int s = n * n; s >= 0; s--) {
    // cout<<s<<endl;
    vvi chG = allG;
    vi sumG = allsum;
    bool ch = true;
    while (ch) {
      bool fin = true;
      for (int i = 0; i < n; i++) {
        if (sumG[i] < s) {
          ch = false;
          break;
        }
      }
      // for(int i=0;i<n;i++){
      //     cout<<sumG[i]<<" ";
      // }
      // cout<<endl;
      int maxpos = 0;
      int maxval = 0;
      int reduce = 0;
      for (int i = 0; i < n; i++) {
        if (maxval <= sumG[i]) {
          maxval = sumG[i];
          maxpos = i;
        }
      }
      // cout<<"AA"<<endl;
      // rem
      bool ischange = false;
      for (int i = 0; i < chG[maxpos].size(); i++) {
        // cout<<"i:"<<i<<endl;
        if (sumG[maxpos] - chG[maxpos][i] - 1 == s) {
          sumG[maxpos] -= chG[maxpos][i] + 1;
          sumG[chG[maxpos][i]] -= maxpos + 1;

          int from = maxpos + 1;
          int to = chG[maxpos][i] + 1;
          // cout<<"FT "<<from<<" "<<to<<endl;

          chG[from - 1].erase(find(all(chG[from - 1]), to - 1));
          chG[to - 1].erase(find(all(chG[to - 1]), from - 1));
          ischange = true;
          break;
        }
      }
      // cout<<"BB"<<endl;
      if (!ischange) {
        Sort(chG[maxpos]);
        sumG[maxpos] -= chG[maxpos][0] + 1;
        chG[maxpos].erase(chG[maxpos].begin());
      }
      for (int i = 0; i < n; i++) {
        if (sumG[i] != s) {
          fin = false;
          break;
        }
      }
      // for(int i=0;i<n;i++){
      //     cout<<"i: "<<i+1<<" =";
      //     for(int j=0;j<chG[i].size();j++){
      //         cout<<chG[i][j]+1<<" ";
      //     }
      //     cout<<endl;
      // }
      if (fin) {
        // 出力
        set<pair<int, int>> sp;
        vector<pair<int, int>> ans;
        for (int i = 0; i < n; i++) {
          for (int j = 0; j < chG[i].size(); j++) {
            if (sp.find(mp(i + 1, chG[i][j] + 1)) != sp.end())
              continue;
            ans.push_back(mp(i + 1, chG[i][j] + 1));
            sp.insert(mp(i + 1, chG[i][j] + 1));
            sp.insert(mp(chG[i][j] + 1, i + 1));
          }
        }
        cout << (int)ans.size() << endl;
        for (int i = 0; i < ans.size(); i++) {
          cout << ans[i].first << " " << ans[i].second << endl;
        }
        return 0;
      }
    }
  }
}