Problem p02761 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
const int INF = 1 << 29;
const ll MOD = 1000000007;
const int MAX = 200000;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 法mでのaの逆元を計算
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
// 最大公約数
int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }
// 最小公倍数
int lcm(int x, int y) { return x / gcd(x, y) * y; }
// nが大きく,kが10^7程度の時の二項係数
long long com[200005];
void combination(long long n, long long k) {
  com[0] = 1;
  for (int i = 1; i <= k; i++) {
    com[i] = (com[i - 1] * (n - i + 1) % MOD) * modinv(i, MOD) % MOD;
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> s(M);
  vector<int> c(M);
  for (int i = 0; i < M; i++) {
    cin >> s[i] >> c[i];
  }
  bool ff = false;
  if (N == 1) {
    bool f = true;
    for (int i = 1; i < M; i++) {
      if (c[0] != c[i]) {
        f = false;
      }
    }
    if (f) {
      cout << c[0] << endl;
      ff = true;
    }
  } else if (N == 2) {
    for (int i = 10; i < 100; i++) {
      int x = i / 10;
      int y = i % 10;
      bool f = true;
      for (int j = 0; j < M; j++) {
        if (s[j] == 1) {
          if (x != c[j])
            f = false;
        } else {
          if (y != c[j])
            f = false;
        }
        if (!f)
          break;
      }
      if (f) {
        cout << i << endl;
        ff = true;
        break;
      }
    }
  } else {
    for (int i = 100; i < 1000; i++) {
      int x = i / 100;
      int y = (i % 100) / 10;
      int z = i % 10;
      // cerr << i << "\t" << x << y << z << endl;
      bool f = true;
      for (int j = 0; j < M; j++) {
        if (s[j] == 1) {
          if (x != c[j])
            f = false;
        } else if (s[j] == 2) {
          if (y != c[j])
            f = false;
        } else {
          if (z != c[j])
            f = false;
        }
        if (!f)
          break;
      }
      if (f) {
        cout << i << endl;
        ff = true;
        break;
      }
    }
  }
  if (!ff)
    cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
const int INF = 1 << 29;
const ll MOD = 1000000007;
const int MAX = 200000;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 法mでのaの逆元を計算
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
// 最大公約数
int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }
// 最小公倍数
int lcm(int x, int y) { return x / gcd(x, y) * y; }
// nが大きく,kが10^7程度の時の二項係数
long long com[200005];
void combination(long long n, long long k) {
  com[0] = 1;
  for (int i = 1; i <= k; i++) {
    com[i] = (com[i - 1] * (n - i + 1) % MOD) * modinv(i, MOD) % MOD;
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> s(M);
  vector<int> c(M);
  for (int i = 0; i < M; i++) {
    cin >> s[i] >> c[i];
  }
  bool ff = false;

  if (M == 0) {
    if (N == 1)
      cout << 0 << endl;
    if (N == 2)
      cout << 10 << endl;
    if (N == 3)
      cout << 100 << endl;
    return 0;
  }
  if (N == 1) {
    bool f = true;
    for (int i = 1; i < M; i++) {
      if (c[0] != c[i]) {
        f = false;
      }
    }
    if (f) {
      cout << c[0] << endl;
      ff = true;
    }
  } else if (N == 2) {
    for (int i = 10; i < 100; i++) {
      int x = i / 10;
      int y = i % 10;
      bool f = true;
      for (int j = 0; j < M; j++) {
        if (s[j] == 1) {
          if (x != c[j])
            f = false;
        } else {
          if (y != c[j])
            f = false;
        }
        if (!f)
          break;
      }
      if (f) {
        cout << i << endl;
        ff = true;
        break;
      }
    }
  } else {
    for (int i = 100; i < 1000; i++) {
      int x = i / 100;
      int y = (i % 100) / 10;
      int z = i % 10;
      // cerr << i << "\t" << x << y << z << endl;
      bool f = true;
      for (int j = 0; j < M; j++) {
        if (s[j] == 1) {
          if (x != c[j])
            f = false;
        } else if (s[j] == 2) {
          if (y != c[j])
            f = false;
        } else {
          if (z != c[j])
            f = false;
        }
        if (!f)
          break;
      }
      if (f) {
        cout << i << endl;
        ff = true;
        break;
      }
    }
  }
  if (!ff)
    cout << -1 << endl;
  return 0;
}