Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iretator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n - 1)
const double EPS = 1e-10;
const double PI = acos(-1.0);

typedef vector<vvi> vvvi;

int main() {
  int N, K;

  cin >> N >> K;
  vvvi dp(N + 1, vvi(4, vi(4, 0)));
  map<int, int> info;

  REP(i, K) {
    int a, b;
    cin >> a >> b;
    info[a] = b;
  }

  if (info[1] && info[2]) {
    dp[2][info[1]][info[2]] = 1;
  } else if (info[1]) {
    FOR(i, 1, 3) { dp[2][info[1]][i] = 1; }
  } else if (info[2]) {
    FOR(i, 1, 3) { dp[2][i][info[2]] = 1; }
  } else {
    FOR(d, 1, 2) {
      FOR(i, 1, 3) {
        FOR(j, 1, 3) { dp[d][i][j] = 1; }
      }
    }
  }

  FOR(d, 3, N) {
    if (info[d]) {
      int tod = info[d];
      FOR(yes, 1, 3) {
        FOR(bef, 1, 3) {
          if (bef == yes && yes == tod) {

          } else {
            dp[d][yes][tod] += dp[d - 1][bef][yes];
          }
        }
        dp[d][yes][tod] %= 10000;
      }
    } else {
      FOR(tod, 1, 3) {
        FOR(yes, 1, 3) {
          FOR(bef, 1, 3) {
            if (bef == yes && yes == tod) {

            } else {
              dp[d][yes][tod] += dp[d - 1][bef][yes];
            }
          }
          dp[d][yes][tod] %= 10000;
        }
      }
    }
  }

  int ans = 0;
  FOR(i, 1, 3) {
    FOR(j, 1, 3) { ans += dp[N][i][j]; }
  }

  cout << ans % 10000 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iretator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n - 1)
const double EPS = 1e-10;
const double PI = acos(-1.0);

typedef vector<vvi> vvvi;

int main() {
  int N, K;

  while (cin >> N >> K, N) {
    vvvi dp(N + 1, vvi(4, vi(4, 0)));
    map<int, int> info;

    REP(i, K) {
      int a, b;
      cin >> a >> b;
      info[a] = b;
    }

    if (info[1] && info[2]) {
      dp[2][info[1]][info[2]] = 1;
    } else if (info[1]) {
      FOR(i, 1, 3) { dp[2][info[1]][i] = 1; }
    } else if (info[2]) {
      FOR(i, 1, 3) { dp[2][i][info[2]] = 1; }
    } else {
      FOR(d, 1, 2) {
        FOR(i, 1, 3) {
          FOR(j, 1, 3) { dp[d][i][j] = 1; }
        }
      }
    }

    FOR(d, 3, N) {
      if (info[d]) {
        int tod = info[d];
        FOR(yes, 1, 3) {
          FOR(bef, 1, 3) {
            if (bef == yes && yes == tod) {

            } else {
              dp[d][yes][tod] += dp[d - 1][bef][yes];
            }
          }
          dp[d][yes][tod] %= 10000;
        }
      } else {
        FOR(tod, 1, 3) {
          FOR(yes, 1, 3) {
            FOR(bef, 1, 3) {
              if (bef == yes && yes == tod) {

              } else {
                dp[d][yes][tod] += dp[d - 1][bef][yes];
              }
            }
            dp[d][yes][tod] %= 10000;
          }
        }
      }
    }

    int ans = 0;
    FOR(i, 1, 3) {
      FOR(j, 1, 3) { ans += dp[N][i][j]; }
    }

    cout << ans % 10000 << endl;
  }

  return 0;
}