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

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600
#define long long long // for codeforces

class UnionFind {
public:
  vector<int> par,
      rank; // parent(negative := its root and abs-value is its size), depth
  UnionFind(int sz) : par(sz, -1), rank(sz, 0) {}
  int find(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;                // already belong to same tree
    if (rank[x] < rank[y]) { // y becomes parent node
      par[y] += par[x];
      par[x] = y;
    } else { // x becomes parent node
      par[x] += par[y];
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -par[find(x)]; }
}; // END class UnionFind

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> vec(n);
    rep(i, n) cin >> vec[i];
    vector<int> in(26, 0), out(26, 0);
    UnionFind uf(26);
    rep(i, n) {
      int s = vec[i][0] - 'a', t = vec[i][vec[i].size() - 1] - 'a';
      out[s]++;
      in[t]++;
      uf.unite(s, t);
    }

    bool ok = true;
    int sz = 0;
    rep(i, 26) {
      if (in[i] != out[i])
        ok = false;
      if (in[i] > 0)
        sz++;
    }
    if (uf.size(vec[0][0] - 'a') != sz)
      ok = false;

    if (ok)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600
#define long long long // for codeforces

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> vec(n);
    rep(i, n) cin >> vec[i];
    vector<vector<int>> v(n);
    rep(i, n) rep(j, n) if (i != j) if (vec[i][vec[i].size() - 1] == vec[j][0])
        v[i]
            .pb(j);

    vector<bool> visited(n, false);
    function<bool(int, int)> dfs = [&](int d, int dep) {
      if (dep == n) {
        for (auto p : v[d])
          if (p == 0)
            return true;
        return false;
      }
      visited[d] = true;
      for (auto p : v[d])
        if (!visited[p]) {
          if (dfs(p, dep + 1))
            return true;
        }
      visited[d] = false;
      return false;
    };

    if (dfs(0, 1))
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}