This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/two_sat"
#include <bits/stdc++.h>
using namespace std;
#include "../2sat.h"
#define REP(i, a) for (int i = 0, _##i = (a); i < _##i; ++i)
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
string wtf; cin >> wtf >> wtf;
int n, m; cin >> n >> m;
TwoSatSolver solver(n);
while (m--) {
int x, y; cin >> x >> y >> wtf;
solver.x_or_y_constraint(x > 0, std::abs(x) - 1, y > 0, std::abs(y) - 1);
}
auto [has_solution, sol] = solver.solve();
if (has_solution) {
cout << "s SATISFIABLE" << endl;
cout << "v ";
REP(i,n) {
if (sol[i]) cout << i+1;
else cout << "-" << (i+1);
cout << ' ';
}
cout << 0 << endl;
} else {
cout << "s UNSATISFIABLE" << endl;
}
return 0;
}
#line 1 "Graph/tests/two_sat.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/two_sat"
#include <bits/stdc++.h>
using namespace std;
#line 1 "Graph/2sat.h"
// For lexicographical min result:
// - For each variable: check if it can be set to False
// (by adding constraint i -> !i)
// - If solver.solve() -> keep constraint i -> !i
// - Otherwise, remove constraint i -> !i, and add !i -> i to force it to True
// See https://oj.vnoi.info/problem/icpc21_mt_i
// Variables: 0 -> n-1
// Tested:
// - https://judge.yosupo.jp/problem/two_sat
// - https://oj.vnoi.info/problem/twosat
// - https://oj.vnoi.info/problem/elect
// - https://open.kattis.com/problems/cleaningpipes
// - https://oj.vnoi.info/problem/icpc21_mt_i
// - https://cses.fi/problemset/task/1684/
#line 1 "Graph/DfsTree/StronglyConnected.h"
// Index from 0
// Usage:
// DirectedDfs tree;
// Now you can use tree.scc
//
// Note: reverse(tree.scc) is topo sorted
//
// Tested:
// - (requires scc to be topo sorted) https://judge.yosupo.jp/problem/scc
// - https://cses.fi/problemset/task/1686/
// - (edges have costs) https://oj.vnoi.info/problem/bedao_g16_b
struct DirectedDfs {
vector<vector<int>> g;
int n;
vector<int> num, low, current, S;
int counter;
vector<int> comp_ids;
vector< vector<int> > scc;
DirectedDfs(const vector<vector<int>>& _g) : g(_g), n(g.size()),
num(n, -1), low(n, 0), current(n, 0), counter(0), comp_ids(n, -1) {
for (int i = 0; i < n; i++) {
if (num[i] == -1) dfs(i);
}
}
void dfs(int u) {
low[u] = num[u] = counter++;
S.push_back(u);
current[u] = 1;
for (auto v : g[u]) {
if (num[v] == -1) dfs(v);
if (current[v]) low[u] = min(low[u], low[v]);
}
if (low[u] == num[u]) {
scc.push_back(vector<int>());
while (1) {
int v = S.back(); S.pop_back(); current[v] = 0;
scc.back().push_back(v);
comp_ids[v] = ((int) scc.size()) - 1;
if (u == v) break;
}
}
}
// build DAG of strongly connected components
// Returns: adjacency list of DAG
std::vector<std::vector<int>> build_scc_dag() {
std::vector<std::vector<int>> dag(scc.size());
for (int u = 0; u < n; u++) {
int x = comp_ids[u];
for (int v : g[u]) {
int y = comp_ids[v];
if (x != y) {
dag[x].push_back(y);
}
}
}
return dag;
}
};
#line 17 "Graph/2sat.h"
struct TwoSatSolver {
TwoSatSolver(int _n_vars) : n_vars(_n_vars), g(2*n_vars) {}
void x_or_y_constraint(bool is_x_true, int x, bool is_y_true, int y) {
assert(x >= 0 && x < n_vars);
assert(y >= 0 && y < n_vars);
if (!is_x_true) x += n_vars;
if (!is_y_true) y += n_vars;
// x || y
// !x -> y
// !y -> x
g[(x + n_vars) % (2*n_vars)].push_back(y);
g[(y + n_vars) % (2*n_vars)].push_back(x);
}
// Returns:
// If no solution -> returns {false, {}}
// If has solution -> returns {true, solution}
// where |solution| = n_vars, solution = true / false
pair<bool, vector<bool>> solve() {
DirectedDfs tree(g);
vector<bool> solution(n_vars);
for (int i = 0; i < n_vars; i++) {
if (tree.comp_ids[i] == tree.comp_ids[i + n_vars]) {
return {false, {}};
}
// Note that reverse(tree.scc) is topo sorted
solution[i] = tree.comp_ids[i] < tree.comp_ids[i + n_vars];
}
return {true, solution};
}
// number of variables
int n_vars;
// vertex 0 -> n_vars - 1: Ai is true
// vertex n_vars -> 2*n_vars - 1: Ai is false
vector<vector<int>> g;
};
#line 7 "Graph/tests/two_sat.test.cpp"
#define REP(i, a) for (int i = 0, _##i = (a); i < _##i; ++i)
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
string wtf; cin >> wtf >> wtf;
int n, m; cin >> n >> m;
TwoSatSolver solver(n);
while (m--) {
int x, y; cin >> x >> y >> wtf;
solver.x_or_y_constraint(x > 0, std::abs(x) - 1, y > 0, std::abs(y) - 1);
}
auto [has_solution, sol] = solver.solve();
if (has_solution) {
cout << "s SATISFIABLE" << endl;
cout << "v ";
REP(i,n) {
if (sol[i]) cout << i+1;
else cout << "-" << (i+1);
cout << ' ';
}
cout << 0 << endl;
} else {
cout << "s UNSATISFIABLE" << endl;
}
return 0;
}