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

#include <iostream>
#include <set>
#include <string.h>
#include <vector>
using namespace std;

const int SIZE = 101;

int solve(int table[SIZE][SIZE], int s, int e, int initTtl, int numRooter) {
  if (initTtl < 2) {
    return 0;
  }
  bool bPast[SIZE];
  memset(bPast, 0, sizeof(bPast));
  bPast[s] = true;
  vector<int> pList;
  for (int i = 1; i <= numRooter; ++i) {
    if (table[s][i]) {
      if (i == e) {
        return 2;
      }
      bPast[i] = true;
      pList.push_back(i);
    }
  }

  int ttl = initTtl;
  while (pList.size() > 0) {
    --ttl;
    if (ttl < 1) {
      return 0;
    }
    vector<int> pList2;
    for (int i = 0; i < pList.size(); ++i) {
      int p = pList[i];
      if (p == e) {
        return initTtl - ttl + 1;
      }
      for (int j = 1; j <= numRooter; ++j) {
        if (table[p][j] && !bPast[j]) {
          bPast[j] = true;
          pList2.push_back(j);
        }
      }
    }
    pList = pList2;
  }

  return 0;
}

int main() {
  int table[SIZE][SIZE];

  int n;
  while (cin >> n) {
    memset(table, 0, sizeof(table));
    for (int i = 0; i < n; ++i) {
      int id, numRooter;
      cin >> id >> numRooter;
      for (int j = 0; j < numRooter; ++j) {
        int to;
        cin >> to;
        table[id][to] = true;
      }
    }
    int p;
    cin >> p;
    for (int i = 0; i < p; ++i) {
      int from, to, ttl;
      cin >> from >> to >> ttl;
      int result = solve(table, from, to, ttl, n);
      if (result > 0) {
        cout << result << endl;
      } else {
        cout << "NA" << endl;
      }
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <set>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

const int SIZE = 101;

class Packet {
public:
  Packet(int _now, set<int> _via) : now(_now), via(_via) {}
  bool isPast(int p) {
    if (via.find(p) != via.end()) {
      return true;
    }
    return false;
  }

  int now;
  set<int> via;
};

int solve(int table[SIZE][SIZE], int s, int e, int initTtl, int numRooter) {
  if (initTtl < 2) {
    return 0;
  }
  vector<Packet> pList;
  for (int i = 1; i <= numRooter; ++i) {
    set<int> via;
    via.insert(s);
    if (table[s][i]) {
      if (i == e) {
        return 2;
      }
      set<int> via2 = via;
      via2.insert(i);
      pList.push_back(Packet(i, via2));
    }
  }

  int ttl = initTtl;
  while (pList.size() > 0) {
    --ttl;
    if (ttl < 1) {
      return 0;
    }
    vector<Packet> pList2;
    for (int i = 0; i < pList.size(); ++i) {
      Packet &p = pList[i];
      if (p.now == e) {
        return p.via.size();
      }
      for (int j = 1; j <= numRooter; ++j) {
        if (table[p.now][j] && !p.isPast(j)) {
          set<int> via = p.via;
          via.insert(j);
          pList2.push_back(Packet(j, via));
        }
      }
    }
    pList = pList2;
  }

  return 0;
}

int main() {
  int table[SIZE][SIZE];

  int n;
  while (cin >> n) {
    cin.ignore(100, '\\n');
    memset(table, 0, sizeof(table));
    string s;
    for (int i = 0; i < n; ++i) {
      getline(cin, s);
      int o = s[0] - '0';
      for (int j = 4; j < s.size(); j += 2) {
        table[o][s[j] - '0'] = true;
      }
    }
    int p;
    cin >> p;
    for (int i = 0; i < p; ++i) {
      int from, to, ttl;
      cin >> from >> to >> ttl;
      int result = solve(table, from, to, ttl, n);
      if (result > 0) {
        cout << result << endl;
      } else {
        cout << "NA" << endl;
      }
    }
  }

  return 0;
}