This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/two_sat"
#include "../../template/template.hpp"
#include "../../other/scanner.hpp"
#include "../../other/printer.hpp"
#include "../../graph/others/two-satisfiability.hpp"
int main() {
Scanner input(stdin);
Printer output(stdout);
string s;
int N, M;
input.read(s, s, N, M);
TwoSatisfiability two(N);
for(int i = 0; i < M; i++) {
int a, b, c;
input.read(a, b, c);
if(a < 0) a = two.rev(-a - 1);
else --a;
if(b < 0) b = two.rev(-b - 1);
else --b;
two.add_or(a, b);
}
auto ret = two.solve();
if(ret.empty()) {
output.writeln("s UNSATISFIABLE");
} else {
output.writeln("s SATISFIABLE");
output.write("v ");
for(size_t i = 0; i < ret.size(); i++) {
if(ret[i]) ret[i] = i + 1;
else ret[i] = -i - 1;
}
output.write(ret);
output.writeln(" 0");
}
}
#line 1 "test/verify/yosupo-two-sat.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/two_sat"
#line 1 "template/template.hpp"
#include<bits/stdc++.h>
using namespace std;
using int64 = long long;
const int mod = 1e9 + 7;
const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
os << p.first << " " << p.second;
return os;
}
template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
is >> p.first >> p.second;
return is;
}
template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
for(int i = 0; i < (int) v.size(); i++) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
for(T &in : v) is >> in;
return is;
}
template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
template< typename T = int64 >
vector< T > make_v(size_t a) {
return vector< T >(a);
}
template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}
template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
t = v;
}
template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
for(auto &e : t) fill_v(e, v);
}
template< typename F >
struct FixPoint : F {
explicit FixPoint(F &&f) : F(forward< F >(f)) {}
template< typename... Args >
decltype(auto) operator()(Args &&... args) const {
return F::operator()(*this, forward< Args >(args)...);
}
};
template< typename F >
inline decltype(auto) MFP(F &&f) {
return FixPoint< F >{forward< F >(f)};
}
#line 4 "test/verify/yosupo-two-sat.test.cpp"
#line 1 "other/scanner.hpp"
/**
* @brief Scanner(高速入力)
*/
struct Scanner {
public:
explicit Scanner(FILE *fp) : fp(fp) {}
template< typename T, typename... E >
void read(T &t, E &... e) {
read_single(t);
read(e...);
}
private:
static constexpr size_t line_size = 1 << 16;
static constexpr size_t int_digits = 20;
char line[line_size + 1] = {};
FILE *fp = nullptr;
char *st = line;
char *ed = line;
void read() {}
static inline bool is_space(char c) {
return c <= ' ';
}
void reread() {
ptrdiff_t len = ed - st;
memmove(line, st, len);
char *tmp = line + len;
ed = tmp + fread(tmp, 1, line_size - len, fp);
*ed = 0;
st = line;
}
void skip_space() {
while(true) {
if(st == ed) reread();
while(*st && is_space(*st)) ++st;
if(st != ed) return;
}
}
template< typename T, enable_if_t< is_integral< T >::value, int > = 0 >
void read_single(T &s) {
skip_space();
if(st + int_digits >= ed) reread();
bool neg = false;
if(is_signed< T >::value && *st == '-') {
neg = true;
++st;
}
typename make_unsigned< T >::type y = *st++ - '0';
while(*st >= '0') {
y = 10 * y + *st++ - '0';
}
s = (neg ? -y : y);
}
template< typename T, enable_if_t< is_same< T, string >::value, int > = 0 >
void read_single(T &s) {
s = "";
skip_space();
while(true) {
char *base = st;
while(*st && !is_space(*st)) ++st;
s += string(base, st);
if(st != ed) return;
reread();
}
}
template< typename T >
void read_single(vector< T > &s) {
for(auto &d : s) read(d);
}
};
#line 1 "other/printer.hpp"
/**
* @brief Printer(高速出力)
*/
struct Printer {
public:
explicit Printer(FILE *fp) : fp(fp) {}
~Printer() { flush(); }
template< bool f = false, typename T, typename... E >
void write(const T &t, const E &... e) {
if(f) write_single(' ');
write_single(t);
write< true >(e...);
}
template< typename... T >
void writeln(const T &...t) {
write(t...);
write_single('\n');
}
void flush() {
fwrite(line, 1, st - line, fp);
st = line;
}
private:
FILE *fp = nullptr;
static constexpr size_t line_size = 1 << 16;
static constexpr size_t int_digits = 20;
char line[line_size + 1] = {};
char *st = line;
template< bool f = false >
void write() {}
void write_single(const char &t) {
if(st + 1 >= line + line_size) flush();
*st++ = t;
}
template< typename T, enable_if_t< is_integral< T >::value, int > = 0 >
void write_single(T s) {
if(st + int_digits >= line + line_size) flush();
st += to_chars(st, st + int_digits, s).ptr - st;
}
void write_single(const string &s) {
for(auto &c: s) write_single(c);
}
void write_single(const char *s) {
while(*s != 0) write_single(*s++);
}
template< typename T >
void write_single(const vector< T > &s) {
for(size_t i = 0; i < s.size(); i++) {
if(i) write_single(' ');
write_single(s[i]);
}
}
};
#line 7 "test/verify/yosupo-two-sat.test.cpp"
#line 2 "graph/others/two-satisfiability.hpp"
#line 2 "graph/connected-components/strongly-connected-components.hpp"
#line 2 "graph/graph-template.hpp"
/**
* @brief Graph Template(グラフテンプレート)
*/
template< typename T = int >
struct Edge {
int from, to;
T cost;
int idx;
Edge() = default;
Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {}
operator int() const { return to; }
};
template< typename T = int >
struct Graph {
vector< vector< Edge< T > > > g;
int es;
Graph() = default;
explicit Graph(int n) : g(n), es(0) {}
size_t size() const {
return g.size();
}
void add_directed_edge(int from, int to, T cost = 1) {
g[from].emplace_back(from, to, cost, es++);
}
void add_edge(int from, int to, T cost = 1) {
g[from].emplace_back(from, to, cost, es);
g[to].emplace_back(to, from, cost, es++);
}
void read(int M, int padding = -1, bool weighted = false, bool directed = false) {
for(int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
a += padding;
b += padding;
T c = T(1);
if(weighted) cin >> c;
if(directed) add_directed_edge(a, b, c);
else add_edge(a, b, c);
}
}
inline vector< Edge< T > > &operator[](const int &k) {
return g[k];
}
inline const vector< Edge< T > > &operator[](const int &k) const {
return g[k];
}
};
template< typename T = int >
using Edges = vector< Edge< T > >;
#line 4 "graph/connected-components/strongly-connected-components.hpp"
template< typename T = int >
struct StronglyConnectedComponents : Graph< T > {
public:
using Graph< T >::Graph;
using Graph< T >::g;
vector< int > comp;
Graph< T > dag;
vector< vector< int > > group;
void build() {
rg = Graph< T >(g.size());
for(size_t i = 0; i < g.size(); i++) {
for(auto &e : g[i]) {
rg.add_directed_edge(e.to, e.from, e.cost);
}
}
comp.assign(g.size(), -1);
used.assign(g.size(), 0);
for(size_t i = 0; i < g.size(); i++) dfs(i);
reverse(begin(order), end(order));
int ptr = 0;
for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;
dag = Graph< T >(ptr);
for(size_t i = 0; i < g.size(); i++) {
for(auto &e : g[i]) {
int x = comp[e.from], y = comp[e.to];
if(x == y) continue;
dag.add_directed_edge(x, y, e.cost);
}
}
group.resize(ptr);
for(size_t i = 0; i < g.size(); i++) {
group[comp[i]].emplace_back(i);
}
}
int operator[](int k) const {
return comp[k];
}
private:
vector< int > order, used;
Graph< T > rg;
void dfs(int idx) {
if(exchange(used[idx], true)) return;
for(auto &to : g[idx]) dfs(to);
order.push_back(idx);
}
void rdfs(int idx, int cnt) {
if(comp[idx] != -1) return;
comp[idx] = cnt;
for(auto &to : rg.g[idx]) rdfs(to, cnt);
}
};
#line 4 "graph/others/two-satisfiability.hpp"
/**
* @brief Two Satisfiability(2-SAT)
* @docs docs/two-satisfiability.md
*/
struct TwoSatisfiability : StronglyConnectedComponents< bool > {
public:
using StronglyConnectedComponents< bool >::g;
using StronglyConnectedComponents< bool >::comp;
using StronglyConnectedComponents< bool >::add_edge;
size_t sz;
explicit TwoSatisfiability(size_t v) : StronglyConnectedComponents< bool >(v + v), sz(v) {}
void add_if(int u, int v) {
// u -> v <=> !v -> !u
add_directed_edge(u, v);
add_directed_edge(rev(v), rev(u));
}
void add_or(int u, int v) {
// u or v <=> !u -> v
add_if(rev(u), v);
}
void add_nand(int u, int v) {
// u nand v <=> u -> !v
add_if(u, rev(v));
}
void set_true(int u) {
// u <=> !u -> u
add_directed_edge(rev(u), u);
}
void set_false(int u) {
// !u <=> u -> !u
add_directed_edge(u, rev(u));
}
inline int rev(int x) {
if(x >= (int)sz) return x - sz;
return x + sz;
}
vector< int > solve() {
StronglyConnectedComponents< bool >::build();
vector< int > ret(sz);
for(size_t i = 0; i < sz; i++) {
if(comp[i] == comp[rev(i)]) return {};
ret[i] = comp[i] > comp[rev(i)];
}
return ret;
}
};
#line 9 "test/verify/yosupo-two-sat.test.cpp"
int main() {
Scanner input(stdin);
Printer output(stdout);
string s;
int N, M;
input.read(s, s, N, M);
TwoSatisfiability two(N);
for(int i = 0; i < M; i++) {
int a, b, c;
input.read(a, b, c);
if(a < 0) a = two.rev(-a - 1);
else --a;
if(b < 0) b = two.rev(-b - 1);
else --b;
two.add_or(a, b);
}
auto ret = two.solve();
if(ret.empty()) {
output.writeln("s UNSATISFIABLE");
} else {
output.writeln("s SATISFIABLE");
output.write("v ");
for(size_t i = 0; i < ret.size(); i++) {
if(ret[i]) ret[i] = i + 1;
else ret[i] = -i - 1;
}
output.write(ret);
output.writeln(" 0");
}
}