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

#include <iostream>
#include <set>
#include <utility>
#include <vector>
using namespace std;

typedef vector<bool> VB;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> PII;
typedef vector<PII> VPII;

VPII dominoes;
VVI connected;
VB visited;
int visited_count;
int n;
int now;
int cur;

bool is_match(int t, PII domino) {
  if (t == domino.first || t == domino.second)
    return true;
  else
    return false;
}

int get_next(int t, PII domino) {
  if (t == domino.first)
    return domino.second;
  else
    return domino.first;
}

bool search(int no, int c) {
  if (visited_count == n)
    return true;
  for (VI::iterator it = connected[cur].begin(); it != connected[cur].end();
       it++) {
    if (visited[*it])
      continue;
    if (is_match(now, dominoes[*it]) == false)
      continue;
    now = get_next(now, dominoes[*it]);
    cur = *it;
    visited[*it] = true;
    visited_count++;
    if (search(now, cur))
      return true;
    now = no;
    cur = c;
    visited_count--;
    visited[*it] = false;
  }
  return false;
}

bool solve() {
  visited = VB(n, false);
  visited_count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2; j++) {
      now = j ? dominoes[i].first : dominoes[i].second;
      cur = i;
      visited[i] = true;
      visited_count++;
      if (search(now, cur))
        return true;
      visited_count--;
      visited[i] = false;
    }
  }
  return false;
}

void input_dominoes(int n) {

  for (int i = 0; i < n; i++) {
    char left, right;
    cin >> left >> right;
    int l = (int)(left - '0'), r = (int)(right - '0');
    dominoes[i] = make_pair(l, r);
  }
}

int main(void) {
  while (cin.eof() == false) {
    cin >> n;
    if (cin.eof())
      break;

    dominoes = VPII(n);
    input_dominoes(n);

    connected = VVI(n);
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (i == j)
          continue;
        if (is_match(dominoes[i].first, dominoes[j]) ||
            is_match(dominoes[i].second, dominoes[j])) {
          connected[i].push_back(j);
          connected[j].push_back(i);
        }
      }
    }

    bool result = solve();
    if (result)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <utility>
#include <vector>
using namespace std;

typedef vector<bool> VB;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> PII;
typedef vector<PII> VPII;

VPII dominoes;
int n;
VB visited;
VB done;
VVI connected;
int now;
int current;
int visited_count;

bool is_match(PII domino) {
  if (now == domino.first)
    return true;
  else if (now == domino.second)
    return true;
  else
    return false;
}

int get_next(PII domino) {
  if (now == domino.first)
    return domino.second;
  else if (now == domino.second)
    return domino.first;
  else
    return -1;
}

bool search() {
  if (visited_count == n - 1)
    return true;
  if (done[current] == false) {
    for (int i = 0; i < n; i++) {
      if (is_match(dominoes[i]))
        connected[current].push_back(i);
      if (visited[i])
        continue;
      if (is_match(dominoes[i])) {
        int backup = now;
        int backup_current = now;
        now = get_next(dominoes[i]);
        current = i;
        visited[i] = true;
        visited_count++;
        if (search())
          return true;
        visited_count--;
        visited[i] = false;
        now = backup;
        current = backup_current;
      }
    }
    done[current] = true;
  } else {
    for (VI::iterator it = connected[current].begin();
         it != connected[current].end(); it++) {
      if (visited[*it])
        continue;
      if (is_match(dominoes[*it])) {
        int backup = now;
        int backup_current = current;
        now = get_next(dominoes[*it]);
        visited[*it] = true;
        visited_count++;
        if (search())
          return true;
        visited_count--;
        visited[*it] = false;
        now = backup;
        current = backup_current;
      }
    }
  }
  return false;
}

bool solve() {
  done = VB(n, false);
  connected = VVI(n);
  visited = VB(n, false);
  visited_count = 0;

  // ツ全ツづづ個ドツミツノツづ可づつつ「ツづ篠篠つキ
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2; j++) {
      // now = ( j ? dominoes[i].first : dominoes[i].second );
      now = dominoes[i].second;
      current = i;
      visited[i] = true;
      if (search())
        return true; // ツつキツづ猟づづ個ドツミツノツづーツ配ツ置ツづつォツづゥツつゥ
      visited[i] = false;
    }
  }
  return false;
}

void input_dominoes(int n) {
  dominoes = VPII(n);

  for (int i = 0; i < n; i++) {
    char left, right;
    cin >> left >> right;
    int l = (int)(left - '0'), r = (int)(right - '0');
    dominoes[i] = make_pair(l, r);
  }
}

int main(void) {
  while (cin.eof() == false) {
    cin >> n;
    if (cin.eof())
      break;
    input_dominoes(n);

    bool result = solve();
    if (result)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}