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

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int N, M, Z;

typedef vector<vector<int>> matrix;

matrix mult(matrix lhs, matrix rhs) {
  int n = lhs.size();
  matrix r(n, vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        r[i][j] += rhs[i][k] * lhs[k][j];
      }
    }
  }
  return r;
}

matrix pow(matrix A, int p) {
  if (p == 1) {
    return A;
  } else if (p % 2) {
    matrix T = pow(A, p - 1);
    return mult(A, T);
  } else {
    matrix T = pow(A, p / 2);
    return mult(T, T);
  }
}

int main() {
  while (cin >> N >> M >> Z && N > 0) {
    vector<pair<int, int>> edges;
    matrix a(2 * M + 1, vector<int>(2 * M + 1));
    edges.push_back(make_pair(0, 1));
    for (int i = 0; i < M; ++i) {
      int s, d;
      cin >> s >> d;
      edges.push_back(make_pair(s, d));
      edges.push_back(make_pair(d, s));
    }
    for (int i = 0; i < edges.size(); ++i) {
      for (int j = 0; j < edges.size(); ++j) {
        if (i != j && edges[i].second == edges[j].first &&
            edges[i].first != edges[j].second) {
          a[i][j] = 1;
        } else {
          a[i][j] = 0;
        }
      }
    }

    a = pow(a, Z);
    bool ans = false;
    for (int i = 0; i < edges.size(); ++i) {
      if (edges[i].second == N and a[0][i] != 0) {
        ans = true;
      }
    }
    if (ans) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <iterator>
#include <set>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int N, M, Z;
  while (cin >> N >> M >> Z && N > 0) {
    bool cyclic = false;
    pair<int, int> edges[2 * M];
    set<vector<pair<int, int>>> hist;
    vector<bool> hist_bool;
    for (int i = 0; i < M; ++i) {
      int s, d;
      cin >> s >> d;
      edges[2 * i] = make_pair(s - 1, d - 1);
      edges[2 * i + 1] = make_pair(d - 1, s - 1);
    }
    vector<pair<int, int>> before;
    for (int i = 0; i < 2 * M; ++i) {
      if (edges[i].first == 0) {
        before.push_back(edges[i]);
      }
    }
    hist.insert(before);
    if (before.size() == 0) {
      hist_bool.push_back(false);
    }
    for (int i = 0; i < before.size(); i++) {
      if (before[i].second == N - 1) {
        hist_bool.push_back(true);
        break;
      }
      if (i == before.size() - 1) {
        hist_bool.push_back(false);
      }
    }
    for (int k = 0; k < Z - 1; ++k) {
      vector<pair<int, int>> after;
      for (int i = 0; i < before.size(); i++) {
        for (int j = 0; j < 2 * M; j++) {
          if (edges[j].first == before[i].second) {
            if (edges[j].second != before[i].first) {
              after.push_back(edges[j]);
              // cout << edges[j].first << ", " << edges[j].second << endl;
            }
          }
        }
      }
      before.swap(after);
      if (before.size() == 0) {
        hist_bool.push_back(false);
      }
      for (int i = 0; i < before.size(); i++) {
        if (before[i].second == N - 1) {
          hist_bool.push_back(true);
          break;
        }
        if (i == before.size() - 1) {
          hist_bool.push_back(false);
        }
      }
      if (!hist.insert(before).second) {
        int start = distance(hist.begin(), hist.find(before));
        int cycle = hist.size() - start;
        cout << k + 1 << " " << cycle << "  " << start << endl;
        int phase = (Z - 1 - start) % cycle;
        cout << phase << endl;
        /*auto itr = hist.find(before);
        for (int i = 0; i < phase; i++){
            itr++;
        }
        vector<pair<int, int> > ans = *itr;
        cout << "a" << endl;
        for(int i = 0; i < ans.size(); i++){
            if(ans[i].second == N-1){
                cout << "yes" << endl;
                break;
            }
            if(i == ans.size()-1){
                cout << "no" << endl;
            }
        }*/
        if (hist_bool[start + phase]) {
          cout << "yes" << endl;
        } else {
          cout << "no" << endl;
        }
        cyclic = true;
        break;
      }
    }
    if (!cyclic) {
      if (before.size() == 0) {
        cout << "no" << endl;
      }
      for (int i = 0; i < before.size(); i++) {
        if (before[i].second == N - 1) {
          cout << "yes" << endl;
          break;
        }
        if (i == before.size() - 1) {
          cout << "no" << endl;
        }
      }
    }
  }
}