Problem p03199 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int MOD = 998244353;

long long power(long long a, long long b) {
  long long ret = 1;
  long long tmp = a;
  while (b > 0) {
    if (b & 1) {
      ret *= tmp;
      ret %= MOD;
    }
    tmp *= tmp;
    tmp %= MOD;
    b >>= 1;
  }
  return ret;
}

long long solve(int n, const map<pair<int, int>, int> &cell) {
  long long cnt = n * (long long)n - (5 * n - 6);
  for (const auto &p : cell) {
    pair<int, int> tmp;
    int a, b, c;
    tie(tmp, c) = p;
    tie(a, b) = tmp;
    if (abs(a - b) > 2) {
      auto it = cell.find(make_pair(b, a));
      if (it == cell.end())
        cnt -= 2;
      else if (c != it->second)
        return 0;
      else
        --cnt;
    }
  }
  cnt /= 2;

  vector<long long> dp(2, 0);
  dp[0] = power(2, cnt);
  for (int i = 0; i < n; ++i) {
    vector<long long> nextDp(2, 0);
    for (int a = 0; a < 2; ++a) {
      for (int b = 0; b < 2; ++b) {
        auto it = cell.find(make_pair(i, i));
        if (it != cell.end() && b != it->second)
          continue;

        long long tmp = 1;
        if (i > 0) {
          auto it1 = cell.find(make_pair(i - 1, i));
          auto it2 = cell.find(make_pair(i, i - 1));
          if (it1 == cell.end() && it2 == cell.end())
            tmp *= 2;
          else if (it1 != cell.end() && it2 != cell.end() &&
                   (it1->second + it2->second + a + b) % 2 != 0)
            continue;
        }
        if (i > 0 && i < n - 1) {
          auto it1 = cell.find(make_pair(i - 1, i + 1));
          auto it2 = cell.find(make_pair(i + 1, i - 1));
          if (it1 == cell.end() && it2 == cell.end())
            tmp *= 2;
          else if (it1 != cell.end() && it2 != cell.end() &&
                   (it1->second + it2->second + b) % 2 != 0)
            continue;
        }

        nextDp[b] += dp[a] * tmp;
        nextDp[b] %= MOD;
      }
    }
    dp = move(nextDp);
  }

  return (dp[0] + dp[1]) % MOD;
}

int main() {
  for (;;) {
    int n = 4;
    int m = rand() % (n * n + 1);

    map<pair<int, int>, int> cell1, cell2;
    while (cell1.size() != m) {
      int a = rand() % n;
      int b = rand() % n;
      if (cell1.find(make_pair(a, b)) != cell1.end())
        continue;
      int c = rand() % 2;
      cell1[make_pair(a, b)] = c;
      cell2[make_pair(b, a)] = c;
    }

    if (solve(n, cell1) != solve(n, cell2))
      return 0;
  }

  int n, m;
  cin >> n >> m;
  map<pair<int, int>, int> cell;
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    cell[make_pair(a, b)] = c;
  }

  cout << solve(n, cell) << endl;

  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int MOD = 998244353;

long long power(long long a, long long b) {
  long long ret = 1;
  long long tmp = a;
  while (b > 0) {
    if (b & 1) {
      ret *= tmp;
      ret %= MOD;
    }
    tmp *= tmp;
    tmp %= MOD;
    b >>= 1;
  }
  return ret;
}

long long solve(int n, const map<pair<int, int>, int> &cell) {
  long long cnt = n * (long long)n - (5 * n - 6);
  for (const auto &p : cell) {
    pair<int, int> tmp;
    int a, b, c;
    tie(tmp, c) = p;
    tie(a, b) = tmp;
    if (abs(a - b) > 2) {
      auto it = cell.find(make_pair(b, a));
      if (it == cell.end())
        cnt -= 2;
      else if (c != it->second)
        return 0;
      else
        --cnt;
    }
  }
  cnt /= 2;

  vector<long long> dp(2, 0);
  dp[0] = power(2, cnt);
  for (int i = 0; i < n; ++i) {
    vector<long long> nextDp(2, 0);
    for (int a = 0; a < 2; ++a) {
      for (int b = 0; b < 2; ++b) {
        auto it = cell.find(make_pair(i, i));
        if (it != cell.end() && b != it->second)
          continue;

        long long tmp = 1;
        if (i > 0) {
          auto it1 = cell.find(make_pair(i - 1, i));
          auto it2 = cell.find(make_pair(i, i - 1));
          if (it1 == cell.end() && it2 == cell.end())
            tmp *= 2;
          else if (it1 != cell.end() && it2 != cell.end() &&
                   (it1->second + it2->second + a + b) % 2 != 0)
            continue;
        }
        if (i > 0 && i < n - 1) {
          auto it1 = cell.find(make_pair(i - 1, i + 1));
          auto it2 = cell.find(make_pair(i + 1, i - 1));
          if (it1 == cell.end() && it2 == cell.end())
            tmp *= 2;
          else if (it1 != cell.end() && it2 != cell.end() &&
                   (it1->second + it2->second + b) % 2 != 0)
            continue;
        }

        nextDp[b] += dp[a] * tmp;
        nextDp[b] %= MOD;
      }
    }
    dp = move(nextDp);
  }

  return (dp[0] + dp[1]) % MOD;
}

int main() {
  int n, m;
  cin >> n >> m;
  map<pair<int, int>, int> cell;
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    cell[make_pair(a, b)] = c;
  }

  cout << solve(n, cell) << endl;

  return 0;
}