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

#define _USE_MATH_DEFINES
#define INF 10000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef pair<int, PP> PPP;

static const double eps = 1e-8;

int path[26][26];
int visited[26];
bool isConnect;

int dfs(int src, int *visited) {
  visited[src] = 1;
  int res = 1;
  for (int i = 0; i < 26; i++) {
    if (path[src][i] && visited[i] == 0) {
      res += dfs(i, visited);
    }
  }
  return res;
}

int main() {
  int n;

  while (~scanf("%d", &n)) {
    if (n == 0)
      break;
    memset(path, 0, sizeof(path));
    memset(visited, 0, sizeof(visited));
    set<int> src;
    for (int i = 0; i < n; i++) {
      string str;
      cin >> str;
      if (*str.begin() - 'a' != *(str.end() - 1) - 'a') {
        path[*str.begin() - 'a'][*(str.end() - 1) - 'a']++;
      }

      src.insert(*(str.end() - 1) - 'a');
      src.insert(*str.begin() - 'a');
    }

    if (src.size() != dfs(*src.begin(), visited)) {
      printf("NG\\n");
      continue;
    }

    bool isok = true;

    for (set<int>::iterator it = src.begin(); it != src.end(); it++) {
      int out = 0;
      for (int j = 0; j < 26; j++) {
        out += path[*it][j];
      }

      int in = 0;
      for (int j = 0; j < 26; j++) {
        in += path[j][*it];
      }

      if (out != in)
        isok = false;
    }

    printf("%s\\n", isok ? "OK" : "NG");
  }
}

Error: MLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 10000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef pair<int, PP> PPP;

static const double eps = 1e-8;

int path[26][26];
bool isConnect;

void dfs(int src, ll remain, map<ll, int> &visited) {
  if (remain == 0) {
    isConnect = true;
    return;
  }

  if (visited.find(remain) != visited.end())
    return;
  visited[remain] = 1;

  for (int i = 0; i < 26; i++) {
    if (src == i)
      continue;
    if (remain & (1LL << i)) {
      if (path[src][i]) {
        ll next = remain & ~(1LL << i);
        // bitset<26> bit((unsigned long long)next);
        // cout << bit << endl;
        dfs(i, next, visited);
      }
    }
  }
}

int main() {
  int n;

  while (~scanf("%d", &n)) {
    if (n == 0)
      break;
    memset(path, 0, sizeof(path));
    set<int> src;
    for (int i = 0; i < n; i++) {
      string str;
      cin >> str;
      path[*str.begin() - 'a'][*(str.end() - 1) - 'a']++;

      src.insert(*(str.end() - 1) - 'a');
      src.insert(*str.begin() - 'a');
    }

    ll remain = 0;
    for (set<int>::iterator it = src.begin(); it != src.end(); it++) {
      remain |= (1LL << *it);
    }

    map<ll, int> visited;
    isConnect = false;
    dfs(*src.begin(), remain & ~(1LL << *src.begin()), visited);

    if (!isConnect) {
      printf("NG\\n");
      continue;
    }
    bool isok = true;

    for (int i = 0; i < 26; i++) {
      if (src.find(i) == src.end())
        continue;
      int out = 0;
      for (int j = 0; j < 26; j++) {
        out += path[i][j];
      }

      int in = 0;
      for (int j = 0; j < 26; j++) {
        in += path[j][i];
      }

      if (out != in)
        isok = false;
    }

    printf("%s\\n", isok ? "OK" : "NG");
  }
}