This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/two_sat"
#define FAST_IO
#include "../../template/template.hpp"
#include "../../graph/two_sat.hpp"
int main() {
string dummy;
cin >> dummy >> dummy;
i32 n, m;
cin >> n >> m;
TwoSat sat;
Vec<TwoSat::Variable> vars = sat.add_variables(n);
REP(ci, m) {
i32 a, b;
cin >> a >> b >> dummy;
TwoSat::Variable a_ = (a > 0 ? vars[a - 1] : !vars[-a - 1]);
TwoSat::Variable b_ = (b > 0 ? vars[b - 1] : !vars[-b - 1]);
sat.add_clause(a_, b_);
}
optional<Vec<bool>> ans = sat.solve();
if (ans.has_value()) {
cout << "s SATISFIABLE\n";
cout << "v ";
REP(i, n) {
bool b = (*ans)[vars[i].index()];
cout << (b ? i + 1 : -i - 1) << ' ';
}
cout << "0\n";
} else {
cout << "s UNSATISFIABLE\n";
}
}#line 1 "graph/test/two_sat.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/two_sat"
#define FAST_IO
#line 1 "template/template.hpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32) (n); ++i)
#define REP3(i, m, n) for (i32 i = (i32) (m); i < (i32) (n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER(i, n) for (i32 i = (i32) (n) - 1; i >= 0; --i)
#define ALL(x) begin(x), end(x)
using namespace std;
using u32 = unsigned int;
using u64 = unsigned long long;
using u128 = __uint128_t;
using i32 = signed int;
using i64 = signed long long;
using i128 = __int128_t;
using f64 = double;
using f80 = long double;
template <typename T>
using Vec = vector<T>;
template <typename T>
bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
istream &operator>>(istream &is, i128 &x) {
i64 v;
is >> v;
x = v;
return is;
}
ostream &operator<<(ostream &os, i128 x) {
os << (i64) x;
return os;
}
istream &operator>>(istream &is, u128 &x) {
u64 v;
is >> v;
x = v;
return is;
}
ostream &operator<<(ostream &os, u128 x) {
os << (u64) x;
return os;
}
[[maybe_unused]] constexpr i32 INF = 1000000100;
[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;
struct SetUpIO {
SetUpIO() {
#ifdef FAST_IO
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cout << fixed << setprecision(15);
}
} set_up_io;
#line 2 "graph/two_sat.hpp"
#line 2 "graph/strongly_connected_components.hpp"
#line 4 "graph/strongly_connected_components.hpp"
template <typename G>
class StronglyConnectedComponents {
std::vector<int> comp_id;
int comp_num;
public:
StronglyConnectedComponents(const G &g) : comp_id(g.size(), -1), comp_num(0) {
int now = 0;
std::vector<int> vs;
std::vector<int> ord(g.size(), -1);
std::vector<int> low(g.size(), -1);
const auto dfs = [&](const auto &dfs, int v) -> void {
vs.push_back(v);
ord[v] = now++;
low[v] = ord[v];
for (int u : g[v]) {
if (comp_id[u] != -1) {
continue;
}
if (ord[u] == -1) {
dfs(dfs, u);
}
low[v] = std::min(low[v], low[u]);
}
if (low[v] == ord[v]) {
while (true) {
int u = vs.back();
vs.pop_back();
comp_id[u] = comp_num;
if (u == v) {
break;
}
}
++comp_num;
}
};
for (int v = 0; v < (int) g.size(); ++v) {
if (ord[v] == -1) {
dfs(dfs, v);
}
}
for (int v = 0; v < (int) g.size(); ++v) {
comp_id[v] = comp_num - 1 - comp_id[v];
}
}
int comps() const {
return comp_num;
}
int operator[](int v) const {
assert(v >= 0 && v < (int) comp_id.size());
return comp_id[v];
}
std::vector<std::vector<int>> groups() const {
std::vector<std::vector<int>> ret(comp_num);
for (int v = 0; v < (int) comp_id.size(); ++v) {
ret[comp_id[v]].push_back(v);
}
return ret;
}
};
#line 4 "graph/two_sat.hpp"
#include <optional>
class TwoSat {
public:
struct Variable {
private:
int idx;
Variable(int i) : idx(i) {}
public:
Variable operator!() const noexcept {
return Variable(idx ^ 1);
}
int index() const {
return idx / 2;
}
friend class TwoSat;
};
private:
std::vector<std::vector<int>> graph;
public:
TwoSat() : graph() {}
TwoSat::Variable add_variable() {
Variable var(graph.size());
graph.resize(graph.size() + 2, std::vector<int>());
return var;
}
std::vector<TwoSat::Variable> add_variables(int num) {
std::vector<TwoSat::Variable> vars;
vars.reserve(num);
for (int i = 0; i < num; ++i) {
vars.emplace_back(Variable((int) graph.size() + 2 * i));
}
graph.resize((int) graph.size() + 2 * num, std::vector<int>());
return vars;
}
// x or y
void add_clause(Variable x, Variable y) {
graph[(!x).idx].push_back(y.idx);
graph[(!y).idx].push_back(x.idx);
}
// x implies y
void implies(Variable x, Variable y) {
add_clause(!x, y);
}
void at_most_one(const std::vector<Variable> &vars) {
if (vars.size() <= 1) {
return;
}
std::vector<Variable> sum = add_variables((int) vars.size() - 1);
for (int i = 0; i < (int) sum.size(); ++i) {
implies(vars[i], sum[i]);
implies(sum[i], !vars[i + 1]);
}
for (int i = 0; i < (int) sum.size() - 1; ++i) {
implies(sum[i], sum[i + 1]);
}
}
std::optional<std::vector<bool>> solve() {
StronglyConnectedComponents scc(graph);
std::vector<bool> ans(graph.size() / 2, false);
for (int i = 0; i < (int) graph.size() / 2; ++i) {
if (scc[2 * i] == scc[2 * i + 1]) {
return std::nullopt;
}
if (scc[2 * i] > scc[2 * i + 1]) {
ans[i] = true;
}
}
return ans;
}
};
#line 7 "graph/test/two_sat.test.cpp"
int main() {
string dummy;
cin >> dummy >> dummy;
i32 n, m;
cin >> n >> m;
TwoSat sat;
Vec<TwoSat::Variable> vars = sat.add_variables(n);
REP(ci, m) {
i32 a, b;
cin >> a >> b >> dummy;
TwoSat::Variable a_ = (a > 0 ? vars[a - 1] : !vars[-a - 1]);
TwoSat::Variable b_ = (b > 0 ? vars[b - 1] : !vars[-b - 1]);
sat.add_clause(a_, b_);
}
optional<Vec<bool>> ans = sat.solve();
if (ans.has_value()) {
cout << "s SATISFIABLE\n";
cout << "v ";
REP(i, n) {
bool b = (*ans)[vars[i].index()];
cout << (b ? i + 1 : -i - 1) << ' ';
}
cout << "0\n";
} else {
cout << "s UNSATISFIABLE\n";
}
}