#include #include #include #include struct card { char rank; char color; bool used; }; int num_cards; std::map color_count; std::map rank_count; bool try_next(card last, const std::vector& cards) { if (num_cards == 0) return true; std::vector send_down(cards); std::sort(send_down.begin(), send_down.end(), [&](const card& l, const card& r){ if (l.used && !r.used){ return false; } if (!l.used && r.used){ return true; } auto l_possible = color_count[l.color] + rank_count[l.rank]; auto r_possible = color_count[r.color] + rank_count[r.rank]; return l_possible < r_possible; }); for (auto& c : send_down){ if (c.used){ break; } if (last.color == 0 || c.color == last.color || c.rank == last.rank) { color_count[c.color]--; rank_count[c.rank]--; c.used = true; num_cards--; if (try_next(c, send_down)) return true; num_cards++; c.used = false; rank_count[c.rank]++; color_count[c.color]++; } } return false; } int main() { std::ios_base::sync_with_stdio(false); int n; while (std::cin >> n) { std::vector cards; rank_count.clear(); color_count.clear(); std::string tmp; for (int i=0; i> tmp; auto color = tmp[1]; auto rank = tmp[0]; color_count[color]++; rank_count[rank]++; cards.push_back({rank, color, false}); } num_cards = n; if (n >= 37 || try_next({0, 0, true}, cards)){ std::cout << "YES\n"; } else { std::cout << "NO\n"; } } return 0; }