Problem p03670 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
ll dp[2][20][1 << 19];
int to[1 << 19][19];
int d[20][20];
int main() {
  int num, kai, res;
  scanf("%d%d%d", &num, &kai, &res);
  for (int i = 0; i < res; i++) {
    int za, zb, zc;
    scanf("%d%d%d", &za, &zb, &zc);
    za--, zb--;
    if (zc == 0)
      d[za][zb] = -1;
    else
      d[za][zb] = 1;
  }
  num--;
  for (int p = 0; p < (1 << num); p++) {
    for (int i = 0; i < num; i++) {
      if (p & (1 << i))
        continue;
      int t = -1;
      for (int j = i + 1; j < num; j++) {
        if (p & (1 << j)) {
          t = j;
          break;
        }
      }
      if (t == -1)
        to[p][i] = p + (1 << i);
      else
        to[p][i] = p + (1 << i) - (1 << t);
      // printf("%d %d %d\n", p, i, to[p][i]);
    }
  }
  dp[0][num][0] = 1;
  int cur = 0;
  for (int i = 0; i < kai; i++) {
    for (int j = 0; j < (1 << num); j++)
      dp[1 - cur][0][j] = dp[cur][num][j];
    for (int j = 0; j <= num; j++)
      for (int k = 0; k < (1 << num); k++)
        dp[cur][j][k] = 0;
    cur = 1 - cur;
    for (int j = 0; j < num; j++) {
      for (int k = 0; k < (1 << num); k++) {
        dp[cur][j + 1][k] = (dp[cur][j + 1][k] + dp[cur][j][k]) % mod;
        if ((k & (1 << j)) == 0)
          dp[cur][j + 1][to[k][j]] =
              (dp[cur][j + 1][to[k][j]] + dp[cur][j][k]) % mod;
      }
    }
    for (int j = 0; j < num; j++) {
      for (int k = 0; k < (1 << num); k++) {
        if (d[i][j] == -1 && (k & (1 << j)) != 0)
          dp[cur][num][k] = 0;
        if (d[i][j] == 1 && (k & (1 << j)) == 0)
          dp[cur][num][k] = 0;
      }
    }
    // for (int j = 0; j < (1 << num); j++)printf("%lld ", dp[cur][num][j]);
    // printf("\n");
  }
  ll ans = 0;
  for (int i = 0; i < (1 << num); i++)
    ans = (ans + dp[cur][num][i]) % mod;
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
ll dp[2][20][1 << 19];
int to[1 << 19][19];
int d[20][20];
int main() {
  int num, kai, res;
  scanf("%d%d%d", &num, &kai, &res);
  for (int i = 0; i < res; i++) {
    int za, zb, zc;
    scanf("%d%d%d", &za, &zb, &zc);
    za--, zb--;
    if (zc == 0)
      d[za][zb] = -1;
    else
      d[za][zb] = 1;
  }
  num--;
  for (int p = 0; p < (1 << num); p++) {
    for (int i = 0; i < num; i++) {
      if (p & (1 << i))
        continue;
      int t = -1;
      for (int j = i + 1; j < num; j++) {
        if (p & (1 << j)) {
          t = j;
          break;
        }
      }
      if (t == -1)
        to[p][i] = p + (1 << i);
      else
        to[p][i] = p + (1 << i) - (1 << t);
      // printf("%d %d %d\n", p, i, to[p][i]);
    }
  }
  dp[0][num][0] = 1;
  int cur = 0;
  for (int i = 0; i < kai; i++) {
    for (int j = 0; j < (1 << num); j++)
      dp[1 - cur][0][j] = dp[cur][num][j];
    for (int j = 0; j <= num; j++)
      for (int k = 0; k < (1 << num); k++)
        dp[cur][j][k] = 0;
    cur = 1 - cur;
    for (int j = 0; j < num; j++) {
      for (int k = 0; k < (1 << num); k++) {
        dp[cur][j + 1][k] += dp[cur][j][k];
        if (dp[cur][j + 1][k] >= mod)
          dp[cur][j + 1][k] -= mod;
        if ((k & (1 << j)) == 0) {
          dp[cur][j + 1][to[k][j]] += dp[cur][j][k];
          if (dp[cur][j + 1][to[k][j]] >= mod)
            dp[cur][j + 1][to[k][j]] -= mod;
        }
      }
    }
    for (int j = 0; j < num; j++) {
      for (int k = 0; k < (1 << num); k++) {
        if (d[i][j] == -1 && (k & (1 << j)) != 0)
          dp[cur][num][k] = 0;
        if (d[i][j] == 1 && (k & (1 << j)) == 0)
          dp[cur][num][k] = 0;
      }
    }
    // for (int j = 0; j < (1 << num); j++)printf("%lld ", dp[cur][num][j]);
    // printf("\n");
  }
  ll ans = 0;
  for (int i = 0; i < (1 << num); i++)
    ans = (ans + dp[cur][num][i]) % mod;
  printf("%lld\n", ans);
}