compro_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub siro53/compro_library

:heavy_check_mark: test/mytest/graph/graph-scc-two-sat.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include "../../../template/template.cpp"
#include "../../../graph/graph_template.hpp"
#include "../../../graph/scc.hpp"
#include "../../../graph/shortest-path/dijkstra.hpp"
#include "../../../math/two_sat.hpp"


int main() {
    Edge<int> edge; edge = Edge<int>(2, 3);
    assert(edge.from == 2 && int(edge) == 3 && edge.cost == 1 && edge.id == -1);
    edge = Edge<int>(1, 2, 7, 4); assert(edge.to == 2 && edge.cost == 7 && edge.id == 4);
    Graph<int> undirected; undirected = Graph<int>(3); undirected.add_undirected_edge(0, 1);
    undirected.add_undirected_edge(1, 2, 7);
    const auto& const_graph = undirected;
    assert(const_graph.size() == 3 && const_graph[0][0].id == const_graph[1][0].id);
    assert(const_graph[1][1].cost == 7 && const_graph[2][0].from == 2);
    mt19937 rng(65);
    for(int n = 0; n <= 10; ++n) for(int trial = 0; trial < 20; ++trial) {
        Graph<ll> g(n); SCC<ll> built(n);
        const ll inf = 1000000000000LL;
        vector<vector<ll>> d(n, vector<ll>(n, inf));
        for(int i = 0; i < n; ++i) d[i][i] = 0;
        for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) if(rng() % 4 == 0) {
            ll w = rng() % 10; g.add_directed_edge(i, j, w); built.add_directed_edge(i, j); d[i][j] = min(d[i][j], w);
        }
        for(int k = 0; k < n; ++k) for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        SCC<ll> from_graph(g); built.build();
        set<int> ids;
        for(int i = 0; i < n; ++i) {
            ids.insert(built[i]);
            for(int j = 0; j < n; ++j) {
                bool same = d[i][j] < inf && d[j][i] < inf;
                assert((built[i] == built[j]) == same && (from_graph[i] == from_graph[j]) == same);
            }
        }
        assert(built.size() == ids.size() && from_graph.size() == ids.size());
        auto contracted = from_graph.get_contracted_graph();
        multiset<tuple<int, int, ll>> expected, actual;
        for(int u = 0; u < n; ++u) for(auto e : g[u]) if(from_graph[u] != from_graph[e.to]) {
            expected.emplace(from_graph[u], from_graph[e.to], e.cost);
        }
        for(int u = 0; u < int(contracted.size()); ++u) for(auto e : contracted[u]) {
            assert(u < e.to); actual.emplace(u, e.to, e.cost);
        }
        assert(actual == expected);
        for(int s = 0; s < n; ++s) {
            auto [distance, previous] = dijkstra(g, s, 17LL, inf);
            auto defaults = dijkstra(g, s); auto initial_only = dijkstra(g, s, 17LL);
            for(int v = 0; v < n; ++v) {
                if(d[s][v] == inf) {
                    assert(distance[v] == inf && previous[v] == -1);
                    assert(defaults.first[v] == LLONG_MAX && initial_only.first[v] == LLONG_MAX);
                } else {
                    assert(distance[v] == 17 + d[s][v] && defaults.first[v] == d[s][v] && initial_only.first[v] == distance[v]);
                    int u = v, length = 0;
                    while(u != s) {
                        int p = previous[u]; assert(p >= 0 && p < n);
                        bool found = false; for(auto e : g[p]) if(e.to == u && distance[p] + e.cost == distance[u]) found = true;
                        assert(found); u = p; assert(++length <= n);
                    }
                    assert(previous[s] == -1);
                }
            }
        }
    }
    for(int n = 0; n <= 5; ++n) for(int trial = 0; trial < 100; ++trial) {
        TwoSat sat(n); vector<tuple<int, bool, int, bool>> clauses;
        if(n) for(int k = 0; k < 10; ++k) {
            int i = rng() % n, j = rng() % n; bool a = rng() % 2, b = rng() % 2;
            clauses.emplace_back(i, a, j, b); sat.add_clause(i, a, j, b);
        }
        auto valid = [&](const vector<bool>& value) {
            for(auto [i, a, j, b] : clauses) if(value[i] != a && value[j] != b) return false;
            return true;
        };
        bool exists = false;
        for(int mask = 0; mask < (1 << n); ++mask) {
            vector<bool> values(n); for(int i = 0; i < n; ++i) values[i] = mask >> i & 1;
            exists |= valid(values);
        }
        assert(sat.satisfiable() == exists);
        if(exists) { auto answer = sat.get_answer(); assert(int(answer.size()) == n && valid(answer)); }
    }
    INT(a, b); print(a + b);
}
#line 1 "test/mytest/graph/graph-scc-two-sat.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#line 1 "template/template.cpp"
// #region Macros
#include <bits/stdc++.h>
using namespace std;
// input output utils
namespace siro53_io {
    // https://maspypy.github.io/library/other/io_old.hpp
    struct has_val_impl {
        template <class T>
        static auto check(T &&x) -> decltype(x.val(), std::true_type{});

        template <class T> static auto check(...) -> std::false_type;
    };

    template <class T>
    class has_val : public decltype(has_val_impl::check<T>(std::declval<T>())) {
    };

    // debug
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void dump(const T t) {
        cerr << t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void dump(const T t) {
        cerr << t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void dump(const T &t) {
        cerr << t.val();
    }
    void dump(__int128_t n) {
        if(n == 0) {
            cerr << '0';
            return;
        } else if(n < 0) {
            cerr << '-';
            n = -n;
        }
        string s;
        while(n > 0) {
            s += (char)('0' + n % 10);
            n /= 10;
        }
        reverse(s.begin(), s.end());
        cerr << s;
    }
    void dump(const string &s) { cerr << s; }
    void dump(const char *s) {
        int n = (int)strlen(s);
        for(int i = 0; i < n; i++) cerr << s[i];
    }
    template <class T1, class T2> void dump(const pair<T1, T2> &p) {
        cerr << '(';
        dump(p.first);
        cerr << ',';
        dump(p.second);
        cerr << ')';
    }
    template <class T> void dump(const vector<T> &v) {
        cerr << '{';
        for(int i = 0; i < (int)v.size(); i++) {
            dump(v[i]);
            if(i < (int)v.size() - 1) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(const set<T> &s) {
        cerr << '{';
        for(auto it = s.begin(); it != s.end(); it++) {
            dump(*it);
            if(next(it) != s.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class Key, class Value> void dump(const map<Key, Value> &mp) {
        cerr << '{';
        for(auto it = mp.begin(); it != mp.end(); it++) {
            dump(*it);
            if(next(it) != mp.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class Key, class Value>
    void dump(const unordered_map<Key, Value> &mp) {
        cerr << '{';
        for(auto it = mp.begin(); it != mp.end(); it++) {
            dump(*it);
            if(next(it) != mp.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(const deque<T> &v) {
        cerr << '{';
        for(int i = 0; i < (int)v.size(); i++) {
            dump(v[i]);
            if(i < (int)v.size() - 1) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(queue<T> q) {
        cerr << '{';
        while(!q.empty()) {
            dump(q.front());
            if((int)q.size() > 1) cerr << ',';
            q.pop();
        }
        cerr << '}';
    }

    void debug_print() { cerr << endl; }
    template <class Head, class... Tail>
    void debug_print(const Head &h, const Tail &...t) {
        dump(h);
        if(sizeof...(Tail)) dump(' ');
        debug_print(t...);
    }
    // print
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void print_single(const T t) {
        cout << t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void print_single(const T t) {
        cout << t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void print_single(const T t) {
        cout << t.val();
    }
    void print_single(__int128_t n) {
        if(n == 0) {
            cout << '0';
            return;
        } else if(n < 0) {
            cout << '-';
            n = -n;
        }
        string s;
        while(n > 0) {
            s += (char)('0' + n % 10);
            n /= 10;
        }
        reverse(s.begin(), s.end());
        cout << s;
    }
    void print_single(const string &s) { cout << s; }
    void print_single(const char *s) {
        int n = (int)strlen(s);
        for(int i = 0; i < n; i++) cout << s[i];
    }
    template <class T1, class T2> void print_single(const pair<T1, T2> &p) {
        print_single(p.first);
        cout << ' ';
        print_single(p.second);
    }
    template <class T> void print_single(const vector<T> &v) {
        for(int i = 0; i < (int)v.size(); i++) {
            print_single(v[i]);
            if(i < (int)v.size() - 1) cout << ' ';
        }
    }
    template <class T> void print_single(const set<T> &s) {
        for(auto it = s.begin(); it != s.end(); it++) {
            print_single(*it);
            if(next(it) != s.end()) cout << ' ';
        }
    }
    template <class T> void print_single(const deque<T> &v) {
        for(int i = 0; i < (int)v.size(); i++) {
            print_single(v[i]);
            if(i < (int)v.size() - 1) cout << ' ';
        }
    }
    template <class T> void print_single(queue<T> q) {
        while(!q.empty()) {
            print_single(q.front());
            if((int)q.size() > 1) cout << ' ';
            q.pop();
        }
    }

    void print() { cout << '\n'; }
    template <class Head, class... Tail>
    void print(const Head &h, const Tail &...t) {
        print_single(h);
        if(sizeof...(Tail)) print_single(' ');
        print(t...);
    }

    // input
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void input_single(T &t) {
        cin >> t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void input_single(T &t) {
        cin >> t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void input_single(T &t) {
        cin >> t;
    }
    void input_single(__int128_t &n) {
        string s;
        cin >> s;
        if(s == "0") {
            n = 0;
            return;
        }
        bool is_minus = false;
        if(s[0] == '-') {
            s = s.substr(1);
            is_minus = true;
        }
        n = 0;
        for(int i = 0; i < (int)s.size(); i++) n = n * 10 + (int)(s[i] - '0');
        if(is_minus) n = -n;
    }
    void input_single(string &s) { cin >> s; }
    template <class T1, class T2> void input_single(pair<T1, T2> &p) {
        input_single(p.first);
        input_single(p.second);
    }
    template <class T> void input_single(vector<T> &v) {
        for(auto &e : v) input_single(e);
    }
    void input() {}
    template <class Head, class... Tail> void input(Head &h, Tail &...t) {
        input_single(h);
        input(t...);
    }
}; // namespace siro53_io
#ifdef DEBUG
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debug_print(__VA_ARGS__)
#else
#define debug(...) (void(0))
#endif
// io setup
struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;
using namespace siro53_io;
// types
using ll = long long;
using i128 = __int128_t;
// input macros
#define INT(...)                                                               \
    int __VA_ARGS__;                                                           \
    input(__VA_ARGS__)
#define LL(...)                                                                \
    ll __VA_ARGS__;                                                            \
    input(__VA_ARGS__)
#define STRING(...)                                                            \
    string __VA_ARGS__;                                                        \
    input(__VA_ARGS__)
#define CHAR(...)                                                              \
    char __VA_ARGS__;                                                          \
    input(__VA_ARGS__)
#define DBL(...)                                                               \
    double __VA_ARGS__;                                                        \
    input(__VA_ARGS__)
#define LD(...)                                                                \
    long double __VA_ARGS__;                                                   \
    input(__VA_ARGS__)
#define UINT(...)                                                              \
    unsigned int __VA_ARGS__;                                                  \
    input(__VA_ARGS__)
#define ULL(...)                                                               \
    unsigned long long __VA_ARGS__;                                            \
    input(__VA_ARGS__)
#define VEC(name, type, len)                                                   \
    vector<type> name(len);                                                    \
    input(name);
#define VEC2(name, type, len1, len2)                                           \
    vector name(len1, vector<type>(len2));                                     \
    input(name);
// other macros
// https://trap.jp/post/1224/
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define REP1(i, n) for(int i = 0; i < int(n); i++)
#define REP2(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(RALL(v))
#define UNIQUE(v)                                                              \
    sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()), v.shrink_to_fit()
#define REV(v) reverse(ALL(v))
#define SZ(v) ((int)(v).size())
#define MIN(v) (*min_element(ALL(v)))
#define MAX(v) (*max_element(ALL(v)))
// util const
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MOD2 = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// util functions
void Case(int i) { cout << "Case #" << i << ": "; }
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
template <class T> inline bool chmax(T &a, T b) {
    return (a < b ? a = b, true : false);
}
template <class T> inline bool chmin(T &a, T b) {
    return (a > b ? a = b, true : false);
}
template <class T, int dim>
auto make_vector_impl(vector<int>& sizes, const T &e) {
    if constexpr(dim == 1) {
        return vector(sizes[0], e);
    } else {
        int n = sizes[dim - 1];
        sizes.pop_back();
        return vector(n, make_vector_impl<T, dim - 1>(sizes, e));
    }
}
template <class T, int dim>
auto make_vector(const int (&sizes)[dim], const T &e = T()) {
    vector<int> s(dim);
    for(int i = 0; i < dim; i++) s[i] = sizes[dim - i - 1];
    return make_vector_impl<T, dim>(s, e);
}
vector<int> iota_gen(int n, int start = 0) {
    vector<int> ord(n);
    iota(ord.begin(), ord.end(), start);
    return ord;
}
template<typename T>
vector<int> ord_sort(const vector<T>& v, bool greater = false) {
    auto ord = iota_gen((int)v.size());
    sort(ALL(ord), [&](int i, int j) {
        if(greater) return v[i] > v[j];
        return v[i] < v[j];
    });
    return ord;
}
// #endregion Macros
#line 2 "graph/graph_template.hpp"

#line 6 "graph/graph_template.hpp"

template <typename Cost = int> struct Edge {
    int from, to;
    Cost cost;
    int id;
    Edge() = default;
    explicit Edge(int from, int to, Cost cost = 1, int id = -1)
        : from(from), to(to), cost(cost), id(id) {}
    operator int() const { return to; }
};

template <typename Cost = int> class Graph {
  public:
    Graph() = default;
    explicit Graph(int N) : N(N), M(0), G(N) {}

    inline void add_directed_edge(int from, int to, Cost cost = 1) {
        assert(0 <= from && from < N);
        assert(0 <= to && to < N);
        G[from].emplace_back(from, to, cost, M++);
    }

    inline void add_undirected_edge(int from, int to, Cost cost = 1) {
        assert(0 <= from && from < N);
        assert(0 <= to && to < N);
        G[from].emplace_back(from, to, cost, M);
        G[to].emplace_back(to, from, cost, M++);
    }

    inline std::size_t size() const { return G.size(); }
    inline std::vector<Edge<Cost>> &operator[](const int &i) { return G[i]; }
    inline const std::vector<Edge<Cost>> &operator[](const int &i) const {
        return G[i];
    }

  protected:
    int N, M;
    std::vector<std::vector<Edge<Cost>>> G;
};

template <class Cost = int> using Edges = std::vector<Edge<Cost>>;
#line 2 "graph/scc.hpp"

#line 6 "graph/scc.hpp"

#line 8 "graph/scc.hpp"

template <typename Cost = int> class SCC {
  public:
    explicit SCC(int N)
        : G(N), G_rev(N), comp(N, -1), seen(N, false), isBuilt(false) {}
    explicit SCC(const Graph<Cost> &g)
        : G(g), G_rev(g.size()), comp(g.size(), -1), seen(g.size(), false), isBuilt(false) {
        for(int i = 0; i < (int)g.size(); i++) {
            for(const auto &e : g[i]) G_rev.add_directed_edge(e.to, i);
        }
        build();
    }
    inline void add_directed_edge(int from, int to) {
        assert(!isBuilt &&
               "This scc_graph is already built, but you tried to add edge.");
        G.add_directed_edge(from, to);
        G_rev.add_directed_edge(to, from);
    }
    void build() {
        assert(!isBuilt && "This scc_graph is already built.");
        for(int i = 0; i < (int)G.size(); i++) {
            if(!seen[i]) dfs(i);
        }
        std::reverse(vs.begin(), vs.end());
        count = 0;
        for(const int v : vs) {
            if(comp[v] == -1) {
                dfs_rev(v, count);
                count++;
            }
        }
        isBuilt = true;
    }
    Graph<Cost> get_contracted_graph() {
        assert(isBuilt && "This scc_graph is not yet built.");
        Graph<Cost> new_g(count);
        for(int i = 0; i < (int)G.size(); i++) {
            for(const auto &e : G[i]) {
                int from = comp[i];
                int to = comp[e.to];
                if(from != to) new_g.add_directed_edge(from, to, e.cost);
            }
        }
        return new_g;
    }
    int operator[](const int &k) const {
        assert(isBuilt && "This scc_graph is not yet built.");
        return comp[k];
    }
    size_t size() { return (size_t)count; }

  private:
    Graph<Cost> G, G_rev;
    std::vector<int> vs, comp;
    std::vector<bool> seen;
    int count;
    bool isBuilt;

    void dfs(int u) {
        seen[u] = true;
        for(const auto &e : G[u]) {
            if(!seen[e.to]) dfs(e.to);
        }
        vs.push_back(u);
    }
    void dfs_rev(int u, int idx) {
        comp[u] = idx;
        for(const auto &e : G_rev[u]) {
            if(comp[e.to] == -1) dfs_rev(e.to, idx);
        }
    }
};
#line 2 "graph/shortest-path/dijkstra.hpp"

#line 7 "graph/shortest-path/dijkstra.hpp"

#line 9 "graph/shortest-path/dijkstra.hpp"

template <typename Cost = int>
std::pair<std::vector<Cost>, std::vector<int>>
dijkstra(const Graph<Cost> &G, int start, Cost iv = 0,
         Cost inf = std::numeric_limits<Cost>::max()) {
    using Data = std::pair<Cost, int>;
    int N = (int)G.size();
    std::vector<Cost> dist(N, inf);
    std::vector<int> prev(N, -1);

    dist[start] = iv;
    std::priority_queue<Data, std::vector<Data>, std::greater<Data>> que;
    que.emplace(iv, start);
    while(!que.empty()) {
        auto [d, u] = que.top();
        que.pop();
        if(d > dist[u]) continue;
        for(const auto &e : G[u]) {
            if(dist[e.to] > d + e.cost) {
                dist[e.to] = d + e.cost;
                prev[e.to] = u;
                que.emplace(dist[e.to], e.to);
            }
        }
    }

    return std::make_pair(dist, prev);
}
#line 2 "math/two_sat.hpp"

#line 4 "math/two_sat.hpp"

template<class Cost = int>
class TwoSat {
public:
    TwoSat(): TwoSat(0) {}
    explicit TwoSat(int N): N(N), scc(N * 2), ans(N), called_satisfiable(false) {}

    void add_clause(int i, bool f_i, int j, bool f_j) {
        if(!f_i) i = NOT(i);
        if(!f_j) j = NOT(j);
        scc.add_directed_edge(i, NOT(j));
        scc.add_directed_edge(j, NOT(i));
    }
    bool satisfiable() {
        scc.build();
        for(int i = 0; i < N; i++) {
            if(scc[i] == scc[NOT(i)]) return false;
            ans[i] = (scc[i] < scc[NOT(i)]);
        }
        called_satisfiable = true;
        return true;
    }
    std::vector<bool> get_answer() const {
        assert(called_satisfiable);
        return ans;
    }

private:
    int N;
    SCC<Cost> scc;
    std::vector<bool> ans;
    bool called_satisfiable;

    inline int NOT(int i) { return i + (i < N ? N : -N); }
};
#line 7 "test/mytest/graph/graph-scc-two-sat.test.cpp"


int main() {
    Edge<int> edge; edge = Edge<int>(2, 3);
    assert(edge.from == 2 && int(edge) == 3 && edge.cost == 1 && edge.id == -1);
    edge = Edge<int>(1, 2, 7, 4); assert(edge.to == 2 && edge.cost == 7 && edge.id == 4);
    Graph<int> undirected; undirected = Graph<int>(3); undirected.add_undirected_edge(0, 1);
    undirected.add_undirected_edge(1, 2, 7);
    const auto& const_graph = undirected;
    assert(const_graph.size() == 3 && const_graph[0][0].id == const_graph[1][0].id);
    assert(const_graph[1][1].cost == 7 && const_graph[2][0].from == 2);
    mt19937 rng(65);
    for(int n = 0; n <= 10; ++n) for(int trial = 0; trial < 20; ++trial) {
        Graph<ll> g(n); SCC<ll> built(n);
        const ll inf = 1000000000000LL;
        vector<vector<ll>> d(n, vector<ll>(n, inf));
        for(int i = 0; i < n; ++i) d[i][i] = 0;
        for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) if(rng() % 4 == 0) {
            ll w = rng() % 10; g.add_directed_edge(i, j, w); built.add_directed_edge(i, j); d[i][j] = min(d[i][j], w);
        }
        for(int k = 0; k < n; ++k) for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        SCC<ll> from_graph(g); built.build();
        set<int> ids;
        for(int i = 0; i < n; ++i) {
            ids.insert(built[i]);
            for(int j = 0; j < n; ++j) {
                bool same = d[i][j] < inf && d[j][i] < inf;
                assert((built[i] == built[j]) == same && (from_graph[i] == from_graph[j]) == same);
            }
        }
        assert(built.size() == ids.size() && from_graph.size() == ids.size());
        auto contracted = from_graph.get_contracted_graph();
        multiset<tuple<int, int, ll>> expected, actual;
        for(int u = 0; u < n; ++u) for(auto e : g[u]) if(from_graph[u] != from_graph[e.to]) {
            expected.emplace(from_graph[u], from_graph[e.to], e.cost);
        }
        for(int u = 0; u < int(contracted.size()); ++u) for(auto e : contracted[u]) {
            assert(u < e.to); actual.emplace(u, e.to, e.cost);
        }
        assert(actual == expected);
        for(int s = 0; s < n; ++s) {
            auto [distance, previous] = dijkstra(g, s, 17LL, inf);
            auto defaults = dijkstra(g, s); auto initial_only = dijkstra(g, s, 17LL);
            for(int v = 0; v < n; ++v) {
                if(d[s][v] == inf) {
                    assert(distance[v] == inf && previous[v] == -1);
                    assert(defaults.first[v] == LLONG_MAX && initial_only.first[v] == LLONG_MAX);
                } else {
                    assert(distance[v] == 17 + d[s][v] && defaults.first[v] == d[s][v] && initial_only.first[v] == distance[v]);
                    int u = v, length = 0;
                    while(u != s) {
                        int p = previous[u]; assert(p >= 0 && p < n);
                        bool found = false; for(auto e : g[p]) if(e.to == u && distance[p] + e.cost == distance[u]) found = true;
                        assert(found); u = p; assert(++length <= n);
                    }
                    assert(previous[s] == -1);
                }
            }
        }
    }
    for(int n = 0; n <= 5; ++n) for(int trial = 0; trial < 100; ++trial) {
        TwoSat sat(n); vector<tuple<int, bool, int, bool>> clauses;
        if(n) for(int k = 0; k < 10; ++k) {
            int i = rng() % n, j = rng() % n; bool a = rng() % 2, b = rng() % 2;
            clauses.emplace_back(i, a, j, b); sat.add_clause(i, a, j, b);
        }
        auto valid = [&](const vector<bool>& value) {
            for(auto [i, a, j, b] : clauses) if(value[i] != a && value[j] != b) return false;
            return true;
        };
        bool exists = false;
        for(int mask = 0; mask < (1 << n); ++mask) {
            vector<bool> values(n); for(int i = 0; i < n; ++i) values[i] = mask >> i & 1;
            exists |= valid(values);
        }
        assert(sat.satisfiable() == exists);
        if(exists) { auto answer = sat.get_answer(); assert(int(answer.size()) == n && valid(answer)); }
    }
    INT(a, b); print(a + b);
}
Back to top page