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

#include <bits/stdc++.h>

using namespace std;

// typedef long long ll;
typedef int ll;
#define MAX INT_MAX
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int, int> pii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = 1; i < n; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define MOD (ll)(1e9 + 7)
#define ADD(a, b) a = ((a) + (b)) % MOD
#define FIX(a) ((a) % MOD + MOD) % MOD

typedef vector<ll> vec;
typedef vector<vec> mat;
#define CREATE_MATRIX(n, m) vector<vec>(n, vec(m, 0ll))

mat mul(mat &a, mat &b, ll md = MAX, ll clamp = MAX) {
  if (a.size() == 0 || b.size() == 0)
    assert(false && "ERROR: ????????????????????????????????????");
  // a(n*l) * b(l*m)
  int n = a.size();
  int l = a[0].size();
  int m = b[0].size();
  if (b.size() != l)
    assert(false && "ERROR: ????????????????????????????????????");
  mat ret = CREATE_MATRIX(n, m);
  REP(i, n) REP(j, m) {
    REP(k, l) {
      ret[i][j] += a[i][k] * b[k][j] % md;
      if (ret[i][j] >= clamp - 1)
        break;
      ret[i][j] %= md;
    }
    ret[i][j] %= clamp;
  }
  return ret;
}

void out_mat(mat a) {
  REP(i, a.size()) {
    REP(j, a[i].size()) cout << a[i][j] << " ";
    cout << endl;
  }
}
void out_vec(mat v) {
  assert(v[0].size() == 1);
  REP(i, v.size()) cout << v[i][0] << " ";
  cout << endl;
}

int main() {
  // verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2107
  while (true) {
    int n, m;
    ll z;
    cin >> n >> m >> z;
    if (!n && !m && !z)
      break;
    vector<pii> es(m);
    REP(i, m) {
      int x, y;
      cin >> x >> y;
      es[i].first = x - 1;
      es[i].second = y - 1;
    }
    mat M = CREATE_MATRIX(2 * m, 2 * m);
    REP(i, m) REP(j, m) {
      if (i == j)
        continue;
      if (es[i].second == es[j].first)
        M[j][i] = M[m + i][m + j] = 1;
      if (es[i].first == es[j].second)
        M[i][j] = M[m + j][m + i] = 1;
      if (es[i].first == es[j].first)
        M[j][m + i] = M[i][m + j] = 1;
      if (es[i].second == es[j].second)
        M[m + j][i] = M[m + i][j] = 1;
    }
    mat X = CREATE_MATRIX(2 * m, 1);
    REP(i, m) if (es[i].first == 0) X[i][0] = 1;
    REP(i, m) if (es[i].second == 0) X[m + i][0] = 1;
    --z;
    mat ans = CREATE_MATRIX(2 * m, 2 * m);
    REP(i, 2 * m) ans[i][i] = 1;
    while (z) {
      if (z & 1)
        ans = mul(ans, M, MAX, 2);
      M = mul(M, M, MAX, 2);
      z >>= 1;
    }
    ans = mul(ans, X, MAX, 2);
    bool flag = false;
    REP(i, m) {
      if (es[i].second == n - 1 && ans[i][0] >= 1) {
        flag = true;
        break;
      } else if (es[i].first == n - 1 && ans[i + m][0] >= 1) {
        flag = true;
        break;
      }
    }
    if (flag)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int, int> pii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = 1; i < n; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define MOD (ll)(1e9 + 7)
#define ADD(a, b) a = ((a) + (b)) % MOD
#define FIX(a) ((a) % MOD + MOD) % MOD

typedef vector<ll> vec;
typedef vector<vec> mat;
#define CREATE_MATRIX(n, m) vector<vec>(n, vec(m, 0ll))

mat mul(mat &a, mat &b, ll md = LONG_MAX, ll clamp = LONG_MAX) {
  if (a.size() == 0 || b.size() == 0)
    assert(false && "ERROR: ????????????????????????????????????");
  // a(n*l) * b(l*m)
  int n = a.size();
  int l = a[0].size();
  int m = b[0].size();
  if (b.size() != l)
    assert(false && "ERROR: ????????????????????????????????????");
  mat ret = CREATE_MATRIX(n, m);
  REP(i, n) REP(j, m) {
    REP(k, l) {
      ret[i][j] += a[i][k] * b[k][j] % md;
      if (ret[i][j] >= clamp - 1)
        break;
      ret[i][j] %= md;
    }
    ret[i][j] %= clamp;
  }
  return ret;
}

void out_mat(mat a) {
  REP(i, a.size()) {
    REP(j, a[i].size()) cout << a[i][j] << " ";
    cout << endl;
  }
}
void out_vec(mat v) {
  assert(v[0].size() == 1);
  REP(i, v.size()) cout << v[i][0] << " ";
  cout << endl;
}

int main() {
  // verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2107
  while (true) {
    int n, m;
    ll z;
    cin >> n >> m >> z;
    if (!n && !m && !z)
      break;
    vector<pii> es(m);
    REP(i, m) {
      int x, y;
      cin >> x >> y;
      es[i].first = x - 1;
      es[i].second = y - 1;
    }
    mat M = CREATE_MATRIX(2 * m, 2 * m);
    REP(i, m) REP(j, m) {
      if (i == j)
        continue;
      if (es[i].second == es[j].first)
        M[j][i] = M[m + i][m + j] = 1;
      if (es[i].first == es[j].second)
        M[i][j] = M[m + j][m + i] = 1;
      if (es[i].first == es[j].first)
        M[j][m + i] = M[i][m + j] = 1;
      if (es[i].second == es[j].second)
        M[m + j][i] = M[m + i][j] = 1;
    }
    mat X = CREATE_MATRIX(2 * m, 1);
    REP(i, m) if (es[i].first == 0) X[i][0] = 1;
    REP(i, m) if (es[i].second == 0) X[m + i][0] = 1;
    --z;
    mat ans = CREATE_MATRIX(2 * m, 2 * m);
    REP(i, 2 * m) ans[i][i] = 1;
    while (z) {
      if (z & 1)
        ans = mul(ans, M, LONG_MAX, 2);
      M = mul(M, M, LONG_MAX, 2);
      z >>= 1;
    }
    ans = mul(ans, X, LONG_MAX, 2);
    bool flag = false;
    REP(i, m) {
      if (es[i].second == n - 1 && ans[i][0] >= 1) {
        flag = true;
        break;
      } else if (es[i].first == n - 1 && ans[i + m][0] >= 1) {
        flag = true;
        break;
      }
    }
    if (flag)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}