fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int ll;
  5. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  6.  
  7. #include <ext/pb_ds/assoc_container.hpp>
  8. #include <ext/pb_ds/tree_policy.hpp>
  9. using namespace __gnu_pbds;
  10.  
  11. typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>order_set;
  12. typedef pair<int, int>pr;
  13. #define all(i) i.begin() , i.end()
  14. #define ft first
  15. #define sn second
  16. #define pb push_back
  17. #define totalone(mask) __builtin_popcount(mask)
  18. #define chkbit(mask,bit) (mask&(1LL << bit))
  19.  
  20. // debug section start
  21. void __print(int x) {cerr << x;}
  22. void __print(long x) {cerr << x;}
  23. void __print(long long x) {cerr << x;}
  24. void __print(unsigned x) {cerr << x;}
  25. void __print(unsigned long x) {cerr << x;}
  26. void __print(unsigned long long x) {cerr << x;}
  27. void __print(float x) {cerr << x;}
  28. void __print(double x) {cerr << x;}
  29. void __print(long double x) {cerr << x;}
  30. void __print(char x) {cerr << '\'' << x << '\'';}
  31. void __print(const char *x) {cerr << '\"' << x << '\"';}
  32. void __print(const string &x) {cerr << '\"' << x << '\"';}
  33. void __print(bool x) {cerr << (x ? "true" : "false");}
  34.  
  35. template<typename T, typename V>
  36. void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  37. template<typename T>
  38. void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  39. void _print() {cerr << "]\n";}
  40. template <typename T, typename... V>
  41. void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  42. #ifndef ONLINE_JUDGE
  43. #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  44. #else
  45. #define debug(x...)
  46. #endif
  47. // debug section closed
  48.  
  49. #define en "\n"
  50. #define sum(n) ((1LL*(n)*(n+1))/ 2LL)
  51. #define sqr(n) (1LL*(n)*(n))
  52. #define vag(a,b) ((a + b - 1)/b)
  53.  
  54. #define MAXN 200010
  55. #define inf 1e6+5
  56. const int mod = 1e9 + 7;
  57.  
  58. int n;
  59. char first[23];
  60. int ache[23][26];
  61.  
  62. int mem[26][(1 << 22)][2];
  63. int vis[26][(1 << 22)][2];
  64. int T = 0;
  65.  
  66. int f(int ch, int mask, int st)
  67. {
  68. if (mask == (1 << n) - 1) {
  69. return !st;
  70. }
  71.  
  72. int &ret = mem[ch][mask][st];
  73. if (vis[ch][mask][st] == T) {
  74. return ret;
  75. }
  76.  
  77. vis[ch][mask][st] = T;
  78.  
  79. ret = !st;
  80.  
  81. if (st)
  82. {
  83. for (int i = 0; i < n; i++) {
  84. if (first[i] == ch && !chkbit(mask, i)) {
  85. for (int j = 0; j < 26 && !ret; j++) {
  86. if (ache[i][j]) ret |= f(j, mask | (1 << i), !st);
  87. }
  88. }
  89. }
  90. }
  91. else {
  92. for (int i = 0; i < n; i++)
  93. {
  94. if (first[i] == ch && !chkbit(mask, i)) {
  95. for (int j = 0; j < 26 && ret; j++) {
  96. if (ache[i][j]) ret &= f(j, mask | (1 << i), !st);
  97. }
  98. }
  99. }
  100. }
  101.  
  102. return ret;
  103. }
  104. void solve()
  105. {
  106. cin >> n;
  107. for (int i = 0; i < n; i++)
  108. {
  109. string s;
  110. cin >> s;
  111. first[i] = s[0] - 'a';
  112.  
  113. memset(ache[i], 0, sizeof(ache[i]));
  114.  
  115. for (auto c : s)
  116. {
  117. ache[i][c - 'a'] = 1;
  118. }
  119. }
  120.  
  121. T++;
  122. int win = 0;
  123. for (int i = 0; i < 26; i++)
  124. {
  125. win |= f(i, 0, 1); // f(char, mask, turn);
  126. }
  127. if (win) cout << "YES\n";
  128. else cout << "NO\n";
  129.  
  130. }
  131. int main()
  132. {
  133. IOS;
  134. ll t;
  135. t = 1;
  136. cin >> t;
  137.  
  138. int c = 0;
  139. while ( t-- )
  140. {
  141. cout << "Case " << ++c << ": ";
  142. solve();
  143. }
  144. return 0;
  145. }
Success #stdin #stdout 0.01s 5544KB
stdin
Standard input is empty
stdout
Case 1: NO