Advertisement
Mlxa

### 2-SAT

Feb 2nd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define long ll
  5. #define all(x) begin(x), end(x)
  6.  
  7.  
  8. namespace sat {
  9.     const int N = 512;
  10.     vector<int> g[N];
  11.     vector<int> r[N];
  12.     bool flag[N];
  13.     bool value[N];
  14.     int no(int x) {
  15.         return x ^ 256;
  16.     }
  17.     int id(int x, int v) {
  18.         return v ? x : no(x);
  19.     }
  20.     void add(int x, int y) {
  21.         //cout << "ADD\t" << x << " -> " << y << endl;
  22.         //cout << "ADD\t" << no(y) << " -> " << no(x) << endl;
  23.         g[x].push_back(y);
  24.         g[no(y)].push_back(no(x));
  25.         r[y].push_back(x);
  26.         r[no(x)].push_back(no(y));
  27.     }
  28.     vector<int> order;
  29.     bool used_topsort[N];
  30.     int comp[N];
  31.     int cur_comp = 1;
  32.     void dfs_topsort(int v) {
  33.         used_topsort[v] = true;
  34.         for (int u : g[v]) {
  35.             if (!used_topsort[u]) {
  36.                 dfs_topsort(u);
  37.             }
  38.         }
  39.         order.push_back(v);
  40.     }
  41.     void dfs_csc(int v) {
  42.         comp[v] = cur_comp;
  43.         for (int u : r[v]) {
  44.             if (!comp[u]) {
  45.                 dfs_csc(u);
  46.             }
  47.         }
  48.     }
  49.     void set(int x) {
  50.         flag[x] = true;
  51.     }
  52.     void reset(int x) {
  53.         flag[x] = false;
  54.     }
  55.     void dfs_impl(int v) {
  56.         value[v] = true;
  57.         for (int u : g[v]) {
  58.             if (!value[u]) {
  59.                 dfs_impl(u);
  60.             }
  61.         }
  62.     }
  63.     bool check() {
  64.         order.clear();
  65.         fill_n(used_topsort, N, false);
  66.         fill_n(comp, N, 0);
  67.         fill_n(value, N, false);
  68.         for (int i = 0; i < N; ++i) {
  69.             if (!used_topsort[i]) {
  70.                 dfs_topsort(i);
  71.             }
  72.             if (flag[i] && !value[i]) {
  73.                 dfs_impl(i);
  74.             }
  75.         }
  76.         reverse(all(order));
  77.         for (int i : order) {
  78.             dfs_csc(i);
  79.             ++cur_comp;
  80.         }
  81.         for (int i = 0; i < N; ++i) {
  82.             if (comp[i] == comp[no(i)]) {
  83.                 return false;
  84.             }
  85.             if (value[i] && value[no(i)]) {
  86.                 return false;
  87.             }
  88.         }
  89.         return true;
  90.     }
  91. }
  92.  
  93. const int N = 300;
  94. bool alph[N];
  95. int l, n, m;
  96. string str;
  97.  
  98. void read_input() {
  99.     cin >> str;
  100.     l = (int)str.size();
  101.     for (int i = 0; i < l; ++i) {
  102.         alph['a' + i] = (str[i] == 'C');
  103.     }
  104.     cin >> n >> m;
  105.     int a, b;
  106.     char c, h;
  107.     for (int i = 0; i < m; ++i) {
  108.         cin >> a >> c >> b >> h;
  109.         sat::add(sat::id(a - 1, c == 'C'), sat::id(b - 1, h == 'C'));
  110.     }
  111.     cin >> str;
  112. }
  113.  
  114. int main() {
  115. #ifdef LC
  116.     assert(freopen("input.txt", "r", stdin));
  117. #endif
  118.     ios::sync_with_stdio(false);
  119.     cin.tie(nullptr);
  120.    
  121.     read_input();
  122.     if (!sat::check()) {
  123.         cout << "-1\n";
  124.         return 0;
  125.     }
  126.     for (int i = 0; i < n; ++i) {
  127.         sat::set(sat::id(i, alph[(int)str[i]]));
  128.     }
  129.     if (sat::check()) {
  130.         cout << str << '\n';
  131.         return 0;
  132.     }
  133.     for (int i = 0; i < n; ++i) {
  134.         sat::reset(sat::id(i, alph[(int)str[i]]));
  135.     }
  136.    
  137.     int len = n - 1;
  138.     for (; len > 0; --len) {
  139.         for (int i = 0; i < len; ++i) {
  140.             sat::set(sat::id(i, alph[(int)str[i]]));
  141.         }
  142.         bool can = false;
  143.         for (int c = str[len] + 1; c < 'a' + l; ++c) {
  144.             if (alph[c] == 0) {
  145.                 sat::set(sat::id(len, 0));
  146.                 if (sat::check()) {
  147.                     can = true;
  148.                 }
  149.                 sat::reset(sat::id(len, 0));
  150.                 break;
  151.             }
  152.         }
  153.         for (int c = str[len] + 1; c < 'a' + l; ++c) {
  154.             if (alph[c] == 1) {
  155.                 sat::set(sat::id(len, 1));
  156.                 if (sat::check()) {
  157.                     can = true;
  158.                 }
  159.                 sat::reset(sat::id(len, 1));
  160.                 break;
  161.             }
  162.         }
  163.         for (int i = 0; i < len; ++i) {
  164.             sat::reset(sat::id(i, alph[(int)str[i]]));
  165.         }
  166.         if (can) {
  167.             break;
  168.         }
  169.     }
  170.     //cerr << len << endl;
  171.     assert(len < n);
  172.     for (int i = 0; i < len; ++i) {
  173.         sat::set(sat::id(i, alph[(int)str[i]]));
  174.     }
  175.     string answer(str.size(), '-');
  176.     ++str[len];
  177.     for (int i = len + 1; i < n; ++i) {
  178.         str[i] = 'a';
  179.     }
  180.     for (int i = 0; i < len; ++i) {
  181.         answer[i] = str[i];
  182.     }  
  183.     for (int i = len; i < n; ++i) {
  184.         string vec;
  185.         for (int c = str[i]; c < 'a' + l; ++c) {
  186.             if (alph[c] == 0) {
  187.                 vec.push_back((char)c);
  188.                 break;
  189.             }
  190.         }
  191.         for (int c = str[i]; c < 'a' + l; ++c) {
  192.             if (alph[c] == 1) {
  193.                 vec.push_back((char)c);
  194.                 break;
  195.             }
  196.         }
  197.         sort(all(vec));
  198.         //cerr << i + 1 << '\t' << vec << endl;
  199.         for (char c : vec) {
  200.             sat::set(sat::id(i, alph[(int)c]));
  201.             if (sat::check()) {
  202.                 answer[i] = c;
  203.                 break;
  204.             } else {
  205.                 sat::reset(sat::id(i, alph[(int)c]));
  206.             }
  207.         }
  208.         if (answer[i] == '-') {
  209.             cout << "-1\n";
  210.             return 0;
  211.         }
  212.     }
  213.     cout << answer << '\n';
  214.     return 0;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement