#include #include #include #include #include #include #include #include #include #include #include int main(int argc, char* argv[]) { std::ios_base::sync_with_stdio(false); int n; while ((std::cin >> n).eof() == false) { std::vector > edges; std::vector points; std::string card; std::map pointNum; for (int i = 0; i < n; ++i) { std::cin >> card; pointNum.insert({card,i}); for (int j = 0; j < points.size(); ++j) { if (points[j][0] == card[0] || points[j][1] == card[1]) { edges.push_back({ card, points[j] }); edges.push_back({ points[j], card }); } } points.push_back(card); } /*for (auto &edge : edges) { std::cout << edge.first << " -> " << edge.second << std::endl; }*/ std::map > graph; for (auto &edge : edges) { graph[edge.first].push_back(edge.second); } bool yes = false; for (auto a : graph) { bool visitedPoints[52]; int counters[52]; int depth = 0; std::string curPoint[53]; curPoint[0] = a.first; memset(visitedPoints,false,sizeof(bool)*52); memset(counters, 0, sizeof(int)*52); while (depth >= 0) { auto vektor = graph[curPoint[depth]]; int vis = pointNum[vektor[counters[depth]]]; if (!visitedPoints[vis]) { visitedPoints[vis] = true; std::string temp = vektor[counters[depth]]; counters[depth]++; depth++; if (depth == n) { yes = true; break; } curPoint[depth] = temp; } else { counters[depth]++; } while (depth > -1 && counters[depth] == graph[curPoint[depth]].size()) { visitedPoints[pointNum[curPoint[depth]]] = false; counters[depth] = 0; depth--; } } if (yes) break; } if (yes) { std::cout << "YES" << std::endl; } else { std::cout << "NO" << std::endl; } } return 0; }