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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

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

int deg
    [26]; // Îx
          // æª¶ÈçvXAÅãÌ¶Èç}CiX
vector<int> edge
    [26]; // æªÌ¶©çÅãÌ¶ÖÌGbW
bool vis[26]; // KâtO

bool dfs(int curr) {
  vis[curr] = true;
  rep(i, edge[curr].size()) if (!vis[edge[curr][i]]) dfs(edge[curr][i]);
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.Kobutanukitsuneko", "r", stdin );
  int n;
  while (cin >> n && n) {
    rep(i, 26) deg[i] = 0, edge[i].clear(), vis[i] = true;
    int a, b;
    rep(i, n) {
      string word = "";
      cin >> word;
      a = word[0] - 'a', b = word[word.length() - 1] - 'a';
      deg[a]++, deg[b]--;
      vis[a] = false, vis[b] = false;
      edge[a].push_back(b);
    } // end rep
    dfs(a);

    bool found = true;
    rep(i, 26) {
      if (deg[i] != 0 || !vis[i]) {
        found = false;
        break;
      } // end if
    }   // end rep

    if (found) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    } // end if
  }   // end while

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

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

int n;
vector<string> word;
vector<bool> used;

bool bt(string curr, int m) {
  if ((int)count(ALL(used), true) == n) {
    if (curr[curr.size() - 1] == word[0][0]) {
      return true;
    } else { // end if
      return false;
    } // end if
  }   // end if

  rep(i, n) {
    if (word[i][0] == curr[curr.size() - 1] && !used[i]) {
      used[i] = true;
      if (bt(word[i], m + 1))
        return true;
      else
        used[i] = false;
    } // end if
  }   // end rep

  return false;
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.Kobutanukitsuneko", "r", stdin );
  while (cin >> n && n) {
    word.clear();
    word.resize(n);
    used.clear();
    used.resize(n, false);
    rep(i, n) cin >> word[i];

    used[0] = true;
    if (bt(word[0], 0)) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    } // end if
  }   // end while

  return 0;
}