Fu_L's Library

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

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: verify/unit_test/graph/bipartite.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include "../../../src/template/template.hpp"
#include "../../../src/random/random_number_generator.hpp"
#include "../../../src/graph/graph_template.hpp"
#include "../../../src/graph/bipartite.hpp"
#include "../../../src/tree/lowest_common_ancestor.hpp"
void random_yes() {
    int n = rng(1, 100000);
    vector<vector<int>> color(2);
    rep(i, 0, n) {
        color[rng(0, 1)].push_back(i);
    }
    Graph<int> g(n);
    if(color[0].empty() or color[1].empty()) {
        vector<int> col = bipartite(g);
        assert(!col.empty());
        rep(i, 0, n) assert(col[i] == 0 or col[i] == 1);
        return;
    }
    int m = rng(0, 200000);
    rep(i, 0, m) {
        int a = rng(0, (int)color[0].size() - 1);
        int b = rng(0, (int)color[1].size() - 1);
        g.add_edge(color[0][a], color[1][b]);
    }
    vector<int> col = bipartite(g);
    assert(!col.empty());
    rep(i, 0, n) {
        assert(col[i] == 0 or col[i] == 1);
        for(const int to : g[i]) {
            assert(col[to] == 1 - col[i]);
        }
    }
}
void namori() {
    int n = rng(1, 100000);
    auto [u, v] = rng.tree(n, false);
    Graph<int> g(n);
    rep(i, 0, n - 1) {
        g.add_edge(u[i], v[i]);
    }
    LowestCommonAncestor<int> tree(g);
    int a = rng(0, n - 1);
    int b = a;
    while(b == a) b = rng(0, n - 1);
    g.add_edge(a, b);
    vector<int> color = bipartite(g);
    if(tree.dist(a, b) % 2 == 0) {
        assert(color.empty());
    } else {
        assert(!color.empty());
        rep(i, 0, n) {
            assert(color[i] == 0 or color[i] == 1);
            for(const int to : g[i]) {
                assert(color[to] == 1 - color[i]);
            }
        }
    }
}
void tree() {
    int n = rng(1, 100000);
    auto [u, v] = rng.tree(n, false);
    Graph<int> g(n);
    rep(i, 0, n - 1) {
        g.add_edge(u[i], v[i]);
    }
    vector<int> color = bipartite(g);
    assert(!color.empty());
    rep(i, 0, n) {
        assert(color[i] == 0 or color[i] == 1);
        for(const int to : g[i]) {
            assert(color[to] == 1 - color[i]);
        }
    }
}
int main(void) {
    int test_num = 33;
    rep(i, 0, test_num) {
        random_yes();
    }
    rep(i, 0, test_num) {
        namori();
    }
    rep(i, 0, test_num) {
        tree();
    }
    int a, b;
    cin >> a >> b;
    cout << a + b << '\n';
}
#line 1 "verify/unit_test/graph/bipartite.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/random/random_number_generator.hpp"
struct RandomNumberGenerator {
    RandomNumberGenerator()
        : mt(chrono::steady_clock::now().time_since_epoch().count()) {}
    template <typename T>
    inline T operator()(const T lower, const T upper) {
        static_assert(is_integral_v<T> or is_floating_point_v<T>);
        assert(lower <= upper);
        if constexpr(is_integral_v<T>) {
            uniform_int_distribution<T> dist(lower, upper);
            return dist(mt);
        } else if constexpr(is_floating_point_v<T>) {
            uniform_real_distribution<T> dist(lower, upper);
            return dist(mt);
        }
    }
    template <typename T>
    inline vector<T> vec(const int n, const T lower, const T upper, const bool dup = true) {
        static_assert(is_integral_v<T> or is_floating_point_v<T>);
        assert(0 <= n and n <= (int)1e8);
        assert(lower <= upper);
        if(n == 0) return {};
        vector<T> res(n);
        if(dup or is_floating_point_v<T>) {
            for(int i = 0; i < n; ++i) res[i] = this->operator()(lower, upper);
        } else {
            assert(upper - lower + 1 >= n);
            if(upper - lower + 1 >= 2 * n) {
                set<T> used;
                while((int)used.size() < n) {
                    const T a = this->operator()(lower, upper);
                    used.insert(a);
                }
                int i = 0;
                for(const T a : used) {
                    res[i] = a;
                    ++i;
                }
            } else {
                const vector<int> p = perm(upper - lower + 1, false);
                for(int i = 0; i < n; ++i) {
                    res[i] = p[i] + lower;
                }
            }
        }
        return res;
    }
    inline vector<int> perm(const int n, const bool one = true) {
        assert(0 <= n and n <= (int)1e8);
        vector<int> res(n);
        for(int i = 0; i < n; ++i) res[i] = i + one;
        for(int i = n - 1; i > 0; --i) {
            swap(res[i], res[this->operator()(0, i)]);
        }
        return res;
    }
    inline pair<vector<int>, vector<int>> tree(const int n, const bool one = true) {
        assert(0 <= n and n <= (int)1e8);
        if(n <= 1) return {{}, {}};
        if(n == 2) return {{0 + one}, {1 + one}};
        vector<int> u(n - 1), v(n - 1);
        const vector<int> pruefer = vec(n - 2, 0, n - 1);
        set<int> st;
        vector<int> cnt(n);
        for(int i = 0; i < n; ++i) st.insert(i);
        auto add = [&](const int x) -> void {
            if(x > n) return;
            if(cnt[x] == 0) st.erase(x);
            ++cnt[x];
        };
        auto del = [&](const int x) -> void {
            if(x > n) return;
            --cnt[x];
            if(cnt[x] == 0) st.insert(x);
        };
        for(int i = 0; i < n - 2; ++i) add(pruefer[i]);
        for(int i = 0; i < n - 2; ++i) {
            const int a = *st.begin();
            const int b = pruefer[i];
            u[i] = a + one;
            v[i] = b + one;
            del(b);
            add(a);
        }
        const int a = *st.begin();
        add(a);
        const int b = *st.begin();
        u[n - 2] = a + one;
        v[n - 2] = b + one;
        return {u, v};
    }
    template <typename T>
    inline tuple<vector<int>, vector<int>, vector<T>> weighted_tree(const int n, const T lower, const T upper, const bool one = true) {
        static_assert(is_integral_v<T> or is_floating_point_v<T>);
        assert(0 <= n and n <= (int)1e8);
        assert(lower <= upper);
        if(n == 0) return {{}, {}, {}};
        const auto [u, v] = tree(n, one);
        const vector<T> w = vec(n - 1, lower, upper, true);
        return {u, v, w};
    }
    inline pair<vector<int>, vector<int>> graph(const int n, const int m, const bool one = true) {
        assert(0 <= n and n <= (int)1e8);
        assert(0 <= m and m <= (int)min((ll)1e8, 1ll * n * (n - 1) / 2));
        vector<int> u, v;
        u.reserve(m);
        v.reserve(m);
        if(1ll * n * (n - 1) / 2 >= 2e6) {
            set<pair<int, int>> edge;
            while((int)edge.size() < m) {
                const int a = this->operator()(0, n - 1);
                const int b = this->operator()(0, n - 1);
                if(a >= b) continue;
                edge.insert({a, b});
            }
            for(const auto& [a, b] : edge) {
                u.push_back(a + one);
                v.push_back(b + one);
            }
        } else {
            vector<pair<int, int>> edge;
            edge.reserve(n * (n - 1) / 2);
            for(int i = 0; i < n; ++i) {
                for(int j = i + 1; j < n; ++j) {
                    edge.push_back({i, j});
                }
            }
            const vector<int> p = perm(n * (n - 1) / 2, false);
            for(int i = 0; i < m; ++i) {
                u.push_back(edge[p[i]].first + one);
                v.push_back(edge[p[i]].second + one);
            }
        }
        return {u, v};
    }
    template <typename T>
    inline tuple<vector<int>, vector<int>, vector<T>> weighted_graph(const int n, const int m, const T lower, const T upper, const bool one = true) {
        static_assert(is_integral_v<T> or is_floating_point_v<T>);
        assert(0 <= n and n <= (int)1e8);
        assert(0 <= m and m <= (int)min((ll)1e8, 1ll * n * (n - 1) / 2));
        assert(lower <= upper);
        const auto [u, v] = graph(n, m, one);
        const vector<T> w = vec(m, lower, upper, true);
        return {u, v, w};
    }
    inline pair<vector<int>, vector<int>> connected_graph(const int n, const int m, const bool one = true) {
        assert(0 <= n and n <= (int)1e8);
        assert(max(n - 1, 0) <= m and m <= (int)min((ll)1e8, 1ll * n * (n - 1) / 2));
        if(n <= 1) return {{}, {}};
        vector<int> u, v;
        u.reserve(m);
        v.reserve(m);
        auto [ut, vt] = tree(n, false);
        if(1ll * n * (n - 1) / 2 >= 2e6) {
            set<pair<int, int>> edge;
            for(int i = 0; i < n - 1; ++i) {
                edge.insert({min(ut[i], vt[i]), max(ut[i], vt[i])});
            }
            while((int)edge.size() < m) {
                const int a = this->operator()(0, n - 1);
                const int b = this->operator()(0, n - 1);
                if(a >= b) continue;
                edge.insert({a, b});
            }
            for(const auto& [a, b] : edge) {
                u.push_back(a + one);
                v.push_back(b + one);
            }
        } else {
            set<pair<int, int>> used;
            for(int i = 0; i < n - 1; ++i) {
                u.push_back(ut[i] + one);
                v.push_back(vt[i] + one);
                used.insert({min(ut[i], vt[i]), max(ut[i], vt[i])});
            }
            vector<pair<int, int>> edge;
            edge.reserve(n * (n - 1) / 2 - (n - 1));
            for(int i = 0; i < n; ++i) {
                for(int j = i + 1; j < n; ++j) {
                    if(used.find({i, j}) == used.end()) edge.push_back({i, j});
                }
            }
            const vector<int> p = perm(n * (n - 1) / 2 - (n - 1), false);
            for(int i = 0; i < m - (n - 1); ++i) {
                u.push_back(edge[p[i]].first + one);
                v.push_back(edge[p[i]].second + one);
            }
        }
        return {u, v};
    }
    template <typename T>
    inline tuple<vector<int>, vector<int>, vector<T>> weighted_connected_graph(const int n, const int m, const T lower, const T upper, const bool one = true) {
        static_assert(is_integral_v<T> or is_floating_point_v<T>);
        assert(0 <= n and n <= (int)1e8);
        assert(max(n - 1, 0) <= m and m <= (int)min((ll)1e8, 1ll * n * (n - 1) / 2));
        assert(lower <= upper);
        const auto [u, v] = connected_graph(n, m, one);
        const vector<T> w = vec(m, lower, upper, true);
        return {u, v, w};
    }
    inline string parenthesis(const int n) {
        assert(0 <= n and n <= 1e8);
        string res = "";
        int N = n, M = n;
        for(int i = 0; i < 2 * n; ++i) {
            if(this->operator()(0.0l, 1.0l) > 1.0l * (N - M) * (N + 1) / ((N - M + 1) * (N + M))) {
                res += "(";
                --M;
            } else {
                res += ")";
                --N;
            }
        }
        return res;
    }

   private:
    mt19937_64 mt;
} rng;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T>
struct Graph {
    Graph(const int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 4 "src/graph/bipartite.hpp"
template <typename T>
vector<int> bipartite(const Graph<T>& g) {
    const int n = g.size();
    vector<int> color(n, -1);
    auto dfs = [&](const auto& dfs, const int cur, const int col) -> bool {
        color[cur] = col;
        for(const Edge<T>& e : g[cur]) {
            if(color[e.to] == col) return false;
            if(color[e.to] == -1 and !dfs(dfs, e.to, 1 - col)) return false;
        }
        return true;
    };
    for(int i = 0; i < n; ++i) {
        if(color[i] != -1) continue;
        if(!dfs(dfs, i, 0)) return {};
    }
    return color;
}
#line 4 "src/tree/lowest_common_ancestor.hpp"
template <typename T>
struct LowestCommonAncestor {
    LowestCommonAncestor(const Graph<T>& g, const int root = 0) {
        assert(0 <= root and root < g.size());
        init(g, root);
    }
    int depth(const int v) const {
        assert(0 <= v and v < n);
        return dep[v];
    }
    T cost(const int v) const {
        assert(0 <= v and v < n);
        return co[v];
    }
    int parent(const int v) const {
        assert(0 <= v and v < n);
        return par[0][v];
    }
    int la(int v, int x) const {
        assert(0 <= v and v < n);
        assert(x >= 0);
        if(x > dep[v]) return -1;
        for(int i = 0; x > 0; ++i) {
            if(x & 1) v = par[i][v];
            x >>= 1;
        }
        return v;
    }
    int lca(int u, int v) const {
        assert(0 <= u and u < n and 0 <= v and v < n);
        if(dep[u] > dep[v]) swap(u, v);
        v = la(v, dep[v] - dep[u]);
        if(u == v) return u;
        for(int i = (int)par.size() - 1; i >= 0; --i) {
            if(par[i][u] != par[i][v]) {
                u = par[i][u];
                v = par[i][v];
            }
        }
        return par[0][u];
    }
    int dist(const int u, const int v) const {
        assert(0 <= u and u < n and 0 <= v and v < n);
        return dep[u] + dep[v] - 2 * dep[lca(u, v)];
    }
    T length(const int u, const int v) const {
        assert(0 <= u and u < n and 0 <= v and v < n);
        return co[u] + co[v] - 2 * co[lca(u, v)];
    }

   private:
    int n;
    vector<vector<int>> par;
    vector<int> dep;
    vector<T> co;
    void init(const Graph<T>& g, const int root = 0) {
        n = g.size();
        int h = 1;
        while((1 << h) < n) ++h;
        par.assign(h, vector<int>(n, -1));
        dep.assign(n, -1);
        co.assign(n, -1);
        dfs(g, root, -1, 0, 0);
        for(int i = 0; i + 1 < (int)par.size(); ++i) {
            for(int v = 0; v < n; ++v) {
                if(par[i][v] != -1) {
                    par[i + 1][v] = par[i][par[i][v]];
                }
            }
        }
    }
    void dfs(const Graph<T>& g, const int v, const int p, const int d, const T& c) {
        par[0][v] = p;
        dep[v] = d;
        co[v] = c;
        for(const Edge<T>& e : g[v]) {
            if(e.to == p) continue;
            dfs(g, e.to, v, d + 1, c + e.cost);
        }
    }
};
#line 7 "verify/unit_test/graph/bipartite.test.cpp"
void random_yes() {
    int n = rng(1, 100000);
    vector<vector<int>> color(2);
    rep(i, 0, n) {
        color[rng(0, 1)].push_back(i);
    }
    Graph<int> g(n);
    if(color[0].empty() or color[1].empty()) {
        vector<int> col = bipartite(g);
        assert(!col.empty());
        rep(i, 0, n) assert(col[i] == 0 or col[i] == 1);
        return;
    }
    int m = rng(0, 200000);
    rep(i, 0, m) {
        int a = rng(0, (int)color[0].size() - 1);
        int b = rng(0, (int)color[1].size() - 1);
        g.add_edge(color[0][a], color[1][b]);
    }
    vector<int> col = bipartite(g);
    assert(!col.empty());
    rep(i, 0, n) {
        assert(col[i] == 0 or col[i] == 1);
        for(const int to : g[i]) {
            assert(col[to] == 1 - col[i]);
        }
    }
}
void namori() {
    int n = rng(1, 100000);
    auto [u, v] = rng.tree(n, false);
    Graph<int> g(n);
    rep(i, 0, n - 1) {
        g.add_edge(u[i], v[i]);
    }
    LowestCommonAncestor<int> tree(g);
    int a = rng(0, n - 1);
    int b = a;
    while(b == a) b = rng(0, n - 1);
    g.add_edge(a, b);
    vector<int> color = bipartite(g);
    if(tree.dist(a, b) % 2 == 0) {
        assert(color.empty());
    } else {
        assert(!color.empty());
        rep(i, 0, n) {
            assert(color[i] == 0 or color[i] == 1);
            for(const int to : g[i]) {
                assert(color[to] == 1 - color[i]);
            }
        }
    }
}
void tree() {
    int n = rng(1, 100000);
    auto [u, v] = rng.tree(n, false);
    Graph<int> g(n);
    rep(i, 0, n - 1) {
        g.add_edge(u[i], v[i]);
    }
    vector<int> color = bipartite(g);
    assert(!color.empty());
    rep(i, 0, n) {
        assert(color[i] == 0 or color[i] == 1);
        for(const int to : g[i]) {
            assert(color[to] == 1 - color[i]);
        }
    }
}
int main(void) {
    int test_num = 33;
    rep(i, 0, test_num) {
        random_yes();
    }
    rep(i, 0, test_num) {
        namori();
    }
    rep(i, 0, test_num) {
        tree();
    }
    int a, b;
    cin >> a >> b;
    cout << a + b << '\n';
}
Back to top page