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

#include <iostream>
#include <list>
#include <queue>
#define MAX 8
using namespace std;
struct domino {
  int a, b;
  domino() : a(0), b(0) {}
  domino(int t) : a(t / 10), b(t % 10) {}
  void swap() { std::swap(a, b); }
};
bool dfs(list<domino> now, domino ds[], bool vis[], int n, int num) {
  if (num == n) {
    /*
for(list<domino>::iterator itl = now.begin();
        itl != now.end(); ++itl){
      cout << '[' << itl->a << ',' << itl->b << "] ";
    }
    cout << endl;
    */
    return true;
  }
  /*
  for(list<domino>::iterator itl = now.begin();
      itl != now.end(); ++itl){
    cout << '[' << itl->a << ',' << itl->b << "] ";
  }
  cout << endl;
  */
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) {
      domino t = ds[i];

      /*if( t.b == now.front().a ){
        now.push_front( t );
        vis[i] = true;
        if( dfs( now, ds, vis, n, num+1) ) return true;
        vis[i] = false;
        now.pop_front();
      }else if( t.a == now.front().a ){
        t.swap();
        now.push_front( t );
        vis[i] = true;
        if( dfs( now, ds, vis, n, num+1 ) ) return true;
        vis[i] = false;
        now.pop_front();
        }else
      */
      if (t.b == now.back().b) {
        t.swap();
        now.push_back(t);
        vis[i] = true;
        if (dfs(now, ds, vis, n, num + 1))
          return true;
        vis[i] = false;
        now.pop_back();
      } else if (t.a == now.back().b) {
        now.push_back(t);
        vis[i] = true;
        if (dfs(now, ds, vis, n, num + 1))
          return true;
        vis[i] = false;
        now.pop_back();
      }
      // return false;
    }
  }
  return false;
}

bool isConnected(bool adj[][MAX], bool vis[], bool exist[]) {
  int st;
  bool ret = true;
  for (int i = 0; i < MAX; ++i) {
    if (exist[i])
      st = i;
    vis[i] = false;
  }
  queue<int> q;
  q.push(st);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    // cout << now << endl;
    for (int i = 0; i < MAX; ++i) {
      // cout << "NOW : " << now << " to " << i << " : " << adj[now][i] << endl;
      if (adj[now][i] && !vis[i]) {
        vis[i] = true;
        q.push(i);
      }
    }
  }
  for (int i = 0; i < MAX; ++i) {
    if (exist[i]) {
      // cout << i << " : " << vis[i] << endl;
      if (!vis[i])
        ret = false;
    }
  }
  // cout << endl;
  return ret;
}

int main() {
  while (true) {
    int n;
    bool ans = false;
    cin >> n;
    if (cin.eof())
      break;
    int dim[MAX] = {
        0,
    };
    bool exist[MAX] = {
        false,
    };
    bool vis[MAX];
    bool adj[MAX][MAX] = {{
        false,
    }};
    for (int i = 0; i < n; ++i) {
      int t;
      cin >> t;
      exist[t / 10] = exist[t % 10] = true;
      adj[t / 10][t % 10] = adj[t % 10][t / 10] = true;
      dim[t / 10]++;
      dim[t % 10]++;
    }
    int even = 0;
    int odd = 0;
    for (int i = 0; i < 7; ++i) {
      if (exist[i]) {
        // cout << i << " : " << dim[i] << endl;
        if (dim[i] % 2 == 0)
          ++even;
        else
          odd++;
      }
    }
    if (isConnected(adj, vis, exist) && (odd == 0 || odd == 2))
      ans = true;

    if (ans)
      puts("Yes");
    else
      puts("No");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <list>
#define MAX 30
using namespace std;
struct domino {
  int a, b;
  domino() : a(0), b(0) {}
  domino(int t) : a(t / 10), b(t % 10) {}
  void swap() { std::swap(a, b); }
};
bool dfs(list<domino> now, domino ds[], bool vis[], int n, int num) {
  if (num == n) {
    /*
for(list<domino>::iterator itl = now.begin();
        itl != now.end(); ++itl){
      cout << '[' << itl->a << ',' << itl->b << "] ";
    }
    cout << endl;
    */
    return true;
  }
  /*
  for(list<domino>::iterator itl = now.begin();
      itl != now.end(); ++itl){
    cout << '[' << itl->a << ',' << itl->b << "] ";
  }
  cout << endl;
  */
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) {
      domino t = ds[i];

      /*if( t.b == now.front().a ){
        now.push_front( t );
        vis[i] = true;
        if( dfs( now, ds, vis, n, num+1) ) return true;
        vis[i] = false;
        now.pop_front();
      }else if( t.a == now.front().a ){
        t.swap();
        now.push_front( t );
        vis[i] = true;
        if( dfs( now, ds, vis, n, num+1 ) ) return true;
        vis[i] = false;
        now.pop_front();
        }else
      */
      if (t.b == now.back().b) {
        t.swap();
        now.push_back(t);
        vis[i] = true;
        if (dfs(now, ds, vis, n, num + 1))
          return true;
        vis[i] = false;
        now.pop_back();
      } else if (t.a == now.back().b) {
        now.push_back(t);
        vis[i] = true;
        if (dfs(now, ds, vis, n, num + 1))
          return true;
        vis[i] = false;
        now.pop_back();
      }
      // return false;
    }
  }
  return false;
}
int main() {
  while (true) {
    int n;
    cin >> n;
    if (cin.eof())
      break;
    bool vis[MAX] = {
        false,
    };
    list<domino> init;
    domino ds[n];
    for (int i = 0; i < n; ++i) {
      int t;
      cin >> t;
      ds[i] = domino(t);
    }
    bool ans = false;
    for (int i = 0; i < n; ++i) {
      vis[i] = true;
      init.clear();
      init.push_back(ds[i]);
      ans = dfs(init, ds, vis, n, 1);
      if (!ans) {
        ds[i].swap();
        init.clear();
        init.push_back(ds[i]);
        ans = dfs(init, ds, vis, n, 1);
      }
      break;
      vis[i] = false;
    }
    if (ans)
      puts("Yes");
    else
      puts("No");
  }
  return 0;
}