Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define sbits(a) __builtin_popcount(a)
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define gcd __gcd
#define lcm(a, b) ((a * b) / gcd(a, b))
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define nani(x) cerr << #x << " is " << x << endl;
#define ch(x) (x - 'a' + 1)
#define mp make_pair
#define pb push_back
#define loop(i, begin, end)                                                    \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define scan(x, n) loop(i, 0, n) cin >> x[i];
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({v, w}), g[v].pb({u, w});
#define print(x, n) loop(i, 0, n) cout << x[i] << " ";
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<long, long> pll;
typedef vector<ii> vii;
typedef vector<pll> vll;
typedef um<int, int> umii;
typedef um<char, int> umci;
typedef map<char, int> mci;
typedef map<int, int> mii;

const int N = 1e5 + 1;
vi G[N];
int in[N];

int unique(int a, int b) { return min(a, b) * 1000 + max(a, b); }

int main() {
  fast;
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    int prev;
    cin >> prev;
    for (int j = 2; j < n; j++) {
      int cur;
      cin >> cur;
      int a = unique(i, cur);
      int b = unique(i, prev);
      in[a]++;
      G[b].pb(a);
      prev = cur;
    }
  }

  vi v;

  for (int i = 0; i < N; i++)
    if (!in[i])
      v.pb(i);

  int total = 0;
  int day = 0;

  while (v.sz) {
    total += v.sz;
    ++day;
    vi z;
    for (auto &i : v) {
      for (auto &j : G[i]) {
        --in[j];
        if (!in[j])
          z.pb(j);
      }
    }
    v = z;
  }

  if (total != N)
    cout << -1;
  else
    cout << day;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define sbits(a) __builtin_popcount(a)
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define gcd __gcd
#define lcm(a, b) ((a * b) / gcd(a, b))
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define nani(x) cerr << #x << " is " << x << endl;
#define ch(x) (x - 'a' + 1)
#define mp make_pair
#define pb push_back
#define loop(i, begin, end)                                                    \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define scan(x, n) loop(i, 0, n) cin >> x[i];
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({v, w}), g[v].pb({u, w});
#define print(x, n) loop(i, 0, n) cout << x[i] << " ";
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<long, long> pll;
typedef vector<ii> vii;
typedef vector<pll> vll;
typedef um<int, int> umii;
typedef um<char, int> umci;
typedef map<char, int> mci;
typedef map<int, int> mii;

const int N = 1e6 + 1;
vi G[N];
int in[N];

int unique(int a, int b) { return min(a, b) * 1000 + max(a, b); }

int main() {
  fast;
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    int prev;
    cin >> prev;
    for (int j = 2; j < n; j++) {
      int cur;
      cin >> cur;
      int a = unique(i, cur);
      int b = unique(i, prev);
      in[a]++;
      G[b].pb(a);
      prev = cur;
    }
  }

  vi v;

  for (int i = 0; i < N; i++)
    if (!in[i])
      v.pb(i);

  int total = 0;
  int day = 0;

  while (v.sz) {
    total += v.sz;
    ++day;
    vi z;
    for (auto &i : v) {
      for (auto &j : G[i]) {
        --in[j];
        if (!in[j])
          z.pb(j);
      }
    }
    v = z;
  }

  if (total != N)
    cout << -1;
  else
    cout << day;

  return 0;
}