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; // sửa lỗi sai khai báo
  24.  
  25. int check (string s) {
  26. unordered_map<char,int> pos;
  27. for (int i = 0; i < n; ++i)
  28. pos[s[i]] = i;
  29. for (auto &[type, x, y] : a) {
  30. if (type == 1 && pos[x] >= pos[y]) return 0;
  31. if (type == 2 && pos[x] <= pos[y]) return 0;
  32. if (type == 3 && abs(pos[x] - pos[y]) == 1) return 0;
  33. }
  34. return 1;
  35. }
  36.  
  37. void Solve(){
  38. cin >> n >> r;
  39. for (int i = 1, type; i <= r; i++) {
  40. char x, y;
  41. cin >> type >> x >> y;
  42. a.pb({type, x, y});
  43. }
  44.  
  45. string s = "";
  46. for (int i = 'A'; i <= 'J'; i++) {
  47. if (sz(s) == n) break;
  48. s += (char)i;
  49. }
  50.  
  51. int res = 0;
  52. do {
  53. if (check(s)) res++;
  54. } while (next_permutation(all(s)));
  55.  
  56. cout << res << el;
  57. }
  58.  
  59. signed main (){
  60. ios_base::sync_with_stdio(false);
  61. cin.tie(0), cout.tie(0);
  62.  
  63. int T(1);
  64. // cin >> T;
  65. while(T--) Solve();
  66.  
  67. return 0;
  68. }
Success #stdin #stdout 0.01s 5316KB
stdin
6 3
1 A B
2 F A
3 C D
stdout
160