#include #include #include #include struct card { char rank; char color; bool used; }; //std::vector cards; int num_cards; std::map color_count; std::map rank_count; bool try_next(card last, std::vector cards) { if (num_cards == 0) return true; std::sort(cards.begin(), cards.end(), [&](const card& l, const card& r){ 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 : cards){ if (!c.used && (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, cards)) 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 (try_next({0, 0, true}, cards)){ std::cout << "YES\n"; } else { std::cout << "NO\n"; } } return 0; }