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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

int n, m, g, z;
vector<pi> e;
int f[111][111], ans[111][111], t[111][111];

void prod(int a[111][111], int b[111][111]) {
  rep(i, n) rep(j, n) {
    t[i][j] = 0;
    rep(k, n) if (a[i][k] && b[k][j]) {
      t[i][j] = 1;
      goto NEXT;
    }
  NEXT:;
  }
  rep(i, n) rep(j, n) a[i][j] = t[i][j];
}
int main() {
  while (scanf("%d%d%d", &g, &m, &z), g) {
    e.clear();
    e.pb(mp(0, 0));
    rep(i, m) {
      int a, b;
      scanf("%d%d", &a, &b);
      e.pb(mp(a - 1, b - 1));
      e.pb(mp(b - 1, a - 1));
    }
    n = 2 * m + 1;
    rep(i, n) rep(j, n) f[i][j] =
        j > 0 && e[i].second == e[j].first && e[i].first != e[j].second;
    rep(i, n) rep(j, n) ans[i][j] = i == j;
    for (; z > 0; z /= 2) {
      if (z & 1)
        prod(ans, f);
      prod(f, f);
    }
    rep(i, n) if (e[i].second == g - 1 && ans[0][i]) {
      puts("yes");
      goto END;
    }
    puts("no");
  END:;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

int n, m, nn, z;
int e[50][50], f[2500][2500], ans[2500][2500], t[2500][2500];

void prod(int a[2500][2500], int b[2500][2500]) {
  rep(i, nn) rep(j, nn) {
    t[i][j] = 0;
    rep(k, nn) if (a[i][k] && b[k][j]) {
      t[i][j] = 1;
      goto NEXT;
    }
  NEXT:;
  }
  rep(i, nn) rep(j, nn) a[i][j] = t[i][j];
}
int main() {
  while (scanf("%d%d%d", &n, &m, &z), n) {
    rep(i, n) rep(j, n) e[i][j] = 0;
    rep(i, m) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--;
      b--;
      e[a][b] = e[b][a] = 1;
    }
    nn = n * n;
    rep(i, nn) rep(j, nn) f[i][j] = 0;
    rep(i, n) rep(j, n) rep(k, n) f[j * n + i][k * n + j] = i != k && e[j][k];

    rep(i, nn) rep(j, nn) ans[i][j] = 0;
    ans[0][0] = 1;
    for (; z > 0; z /= 2) {
      if (z & 1)
        prod(ans, f);
      prod(f, f);
    }
    bool ok = 0;
    rep(i, n) if (ans[0][i + n * (n - 1)]) {
      puts("yes");
      goto END;
    }
    puts("no");
  END:;
  }
  return 0;
}