fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define lb double long
  5. #define el '\n'
  6. #define pb push_back
  7. #define all(x) begin(x), end(x)
  8. #define sz(s) (s.size())
  9. #define oo LLONG_MAX
  10. #define pp pair<int, int>
  11.  
  12. void setIO(string s) {
  13. #ifndef ONLINE_JUDGE
  14. freopen((s + ".in").c_str(), "r", stdin);
  15. freopen((s + ".out").c_str(), "w", stdout);
  16. #endif
  17. }
  18.  
  19. const int MOD = (int) 1e9+7; //998244353;
  20. const int MAX = 1e5 + 5;
  21.  
  22. int n, r;
  23. vector<tuple<int,int,int>> a;
  24.  
  25. void Solve(){
  26. cin >> n >> r;
  27. for (int i = 1, type; i <= r; i++) {
  28. char x, y;
  29. cin >> type >> x >> y;
  30. a.pb({type, x, y});
  31. }
  32.  
  33. string s = "";
  34. for (int i = 'A'; i <= 'J'; i++) {
  35. if (sz(s) == n) break;
  36. s += (char)i;
  37. }
  38.  
  39. int res = 0;
  40. sort(all(s));
  41. do {
  42. bool ok = 1;
  43. int pos[256];
  44. for (int i = 0; i < n; i++) pos[s[i]] = i;
  45. for (auto &[type, x, y] : a) {
  46. if (type == 1 && pos[x] >= pos[y]) { ok = 0; break; }
  47. if (type == 2 && pos[x] <= pos[y]) { ok = 0; break; }
  48. if (type == 3 && abs(pos[x] - pos[y]) == 1) { ok = 0; break; }
  49. }
  50. res += ok;
  51. } while (next_permutation(all(s)));
  52.  
  53. cout << res << el;
  54. }
  55.  
  56. signed main (){
  57. ios_base::sync_with_stdio(false);
  58. cin.tie(0), cout.tie(0);
  59.  
  60. int T(1);
  61. // cin >> T;
  62. while(T--) Solve();
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0.01s 5288KB
stdin
6 3
1 A B
2 F A
3 C D
stdout
160