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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int connectedNum(Graph g, int s) {
  int n = g.size();
  if (n <= 1)
    return true;
  vector<bool> visited(n);
  priority_queue<int> Q;
  int res = 0;
  for (Q.push(s); !Q.empty();) {
    int now = Q.top();
    Q.pop();
    if (visited[now])
      continue;
    visited[now] = 1;
    res++;
    FOR(f, g[now])
    if (!visited[f->dst])
      Q.push(f->dst);
  }
  return res;
}

int main() {
  int n;

  while (cin >> n, n) {
    Graph g(26);
    vector<int> v(26); // m[hi Ìü
    set<int> se;
    REP(i, n) {
      string s;
      cin >> s;
      int a = s[0] - 'a';
      int b = s[s.size() - 1] - 'a';
      g[a].push_back(Edge(a, b, 1));
      v[b]++;
      se.insert(a);
      se.insert(b);
    }
    bool f = 0;
    int ma = 0;
    FOR(it, se) { ma = max(ma, connectedNum(g, *it)); }
    if (ma == se.size()) {
      FOR(it, se) {
        if (g[*it].size() != v[*it]) {
          f = 1;
          break;
        }
      }
    } else
      f = 1;
    if (f)
      cout << "NG" << endl;
    else
      cout << "OK" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

vector<string> se[26];
vector<bool> used[26];

int n;

int solve2(int a, int nest) {
  //  cout << nest << string(nest, ' ') << (char)(a+'a') << endl;
  if (nest == n) {
    return a;
  }
  REP(i, se[a].size()) {
    if (!used[a][i]) {
      used[a][i] = 1;
      int aa = solve2(se[a][i][se[a][i].size() - 1] - 'a', nest + 1);
      if (aa >= 0)
        return aa;
      used[a][i] = 0;
    }
  }
  return -1;
}

bool solve() {
  REP(i, 26) {
    REP(j, 26)
    used[j] = vector<bool>(se[j].size());
    int a = solve2(i, 0);
    if (a == i) {
      return true;
    }
  }
  return false;
}

int main() {
  while (cin >> n, n) {
    REP(i, 26) se[i].clear();
    REP(i, n) {
      string s;
      cin >> s;
      se[s[0] - 'a'].push_back(s);
    }
    if (solve())
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
}