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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<ll, ll> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main() {
  int n;
  while (cin >> n, n) {
    vector<vi> g(27);
    vs v;
    REP(i, n) {
      string s;
      cin >> s;
      while (s.size() < 10)
        s += 'z' + 1;
      v.push_back(s);
    }
    REP(i, n) REP(j, i) {
      string u = v[i], d = v[j];
      int t = 0;
      while (u[t] == d[t] && t < 10)
        t++;
      if (t == 10)
        continue;
      int un = u[t] - 'a', dn = d[t] - 'a';
      if (un != dn)
        g[un].push_back(dn);
    }
    bool f = true;
    if (g[26].size() > 0)
      f = false;
    REP(i, 26) {
      sort(ALL(g[i]));
      g[i].erase(unique(ALL(g[i])), g[i].end());
    }
    vector<bool> vis(26);
    REP(i, 26) {
      stack<pii> st;
      st.push(pii(i, 0));
      while (st.size()) {
        int tmp = st.top().first, cnt = st.top().second;
        st.pop();
        if (cnt > 30) {
          f = false;
          goto end;
        }
        REP(j, g[tmp].size()) { st.push(pii(g[tmp][j], cnt + 1)); }
      }
    }
  end:;
    puts(f ? "yes" : "no");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<ll, ll> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main() {
  int n;
  while (cin >> n, n) {
    vector<vi> g(27);
    vs v;
    REP(i, n) {
      string s;
      cin >> s;
      while (s.size() < 10)
        s += 'z' + 1;
      v.push_back(s);
    }
    REP(i, n) REP(j, i) {
      string u = v[i], d = v[j];
      int t = 0;
      while (u[t] == d[t] && t < 10)
        t++;
      if (t == 10)
        continue;
      int un = u[t] - 'a', dn = d[t] - 'a';
      if (un != dn)
        g[un].push_back(dn);
    }
    bool f = true;
    if (g[26].size() > 0)
      f = false;
    REP(i, 26) {
      sort(ALL(g[i]));
      g[i].erase(unique(ALL(g[i])), g[i].end());
    }
    vector<bool> vis(26);
    REP(i, 26) {
      stack<int> st;
      st.push(i);
      while (st.size()) {
        int tmp = st.top();
        st.pop();
        REP(j, g[tmp].size()) {
          if (g[tmp][j] == i) {
            f = false;
            goto end;
          }
          st.push(g[tmp][j]);
        }
      }
    }
  end:;
    puts(f ? "yes" : "no");
  }
  return 0;
}