CP-Algorithms Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub cp-algorithms/cp-algorithms-aux

:heavy_check_mark: cp-algo/graph/concepts.hpp

Depends on

Required by

Verified with

Code

#ifndef CP_ALGO_GRAPH_CONCEPTS_HPP
#define CP_ALGO_GRAPH_CONCEPTS_HPP
#include "edge_types.hpp"
#include <type_traits>

namespace cp_algo::graph {
    // Shared graph mode enum for all graph headers
    enum graph_mode { directed, undirected };
    // Traits: true for types that expose `edge_t` and static `mode`
    template<typename T, typename = void>
    struct graph_traits : std::false_type {};

    template<typename T>
    struct graph_traits<T, std::void_t<typename T::edge_t, decltype(T::mode)>> : std::true_type {
        using edge_t = typename T::edge_t;
        static constexpr auto mode = T::mode;
        static constexpr bool is_directed = mode == directed;
        static constexpr bool is_undirected = mode == undirected;
        static constexpr bool is_weighted = weighted_edge_type<edge_t>;
    };

    // Concepts
    template<typename G>
    concept graph_type = graph_traits<G>::value;

    template<typename G>
    concept digraph_type = graph_type<G> && graph_traits<G>::is_directed;

    template<typename G>
    concept undirected_graph_type = graph_type<G> && graph_traits<G>::is_undirected;

    template<typename G>
    concept weighted_graph_type = graph_type<G> && graph_traits<G>::is_weighted;

    template<typename G>
    concept weighted_digraph_type = digraph_type<G> && graph_traits<G>::is_weighted;

    template<typename G>
    concept weighted_undirected_graph_type = undirected_graph_type<G> && graph_traits<G>::is_weighted;
}
#endif // CP_ALGO_GRAPH_CONCEPTS_HPP
#line 1 "cp-algo/graph/concepts.hpp"


#line 1 "cp-algo/graph/edge_types.hpp"


#include <iostream>
#include <cstdint>
namespace cp_algo::graph {
    using node_index = int;
    struct edge_base {
        int xor_nodes;

        edge_base() {}
        edge_base(node_index from, node_index to): xor_nodes(from ^ to) {}

        // Given one endpoint, return the other
        node_index traverse(node_index from) const {
            return xor_nodes ^ from;
        }

        static auto read(node_index v0 = 0) {
            node_index u, v;
            std::cin >> u >> v;
            u -= v0;
            v -= v0;
            return std::pair{u, edge_base(u, v)};
        }
    };

    struct weighted_edge: edge_base {
        int64_t w;

        weighted_edge() {}
        weighted_edge(node_index from, node_index to, int64_t w): edge_base(from, to), w(w) {}

        static auto read(node_index v0 = 0) {
            auto [u, e] = edge_base::read(v0);
            int64_t w;
            std::cin >> w;
            return std::pair{u, weighted_edge(u, e.traverse(u), w)};
        }
    };

    template<typename edge>
    concept edge_type = std::is_base_of_v<edge_base, edge>;
    template<typename edge>
    concept weighted_edge_type = std::is_base_of_v<weighted_edge, edge>;
}

#line 4 "cp-algo/graph/concepts.hpp"
#include <type_traits>

namespace cp_algo::graph {
    // Shared graph mode enum for all graph headers
    enum graph_mode { directed, undirected };
    // Traits: true for types that expose `edge_t` and static `mode`
    template<typename T, typename = void>
    struct graph_traits : std::false_type {};

    template<typename T>
    struct graph_traits<T, std::void_t<typename T::edge_t, decltype(T::mode)>> : std::true_type {
        using edge_t = typename T::edge_t;
        static constexpr auto mode = T::mode;
        static constexpr bool is_directed = mode == directed;
        static constexpr bool is_undirected = mode == undirected;
        static constexpr bool is_weighted = weighted_edge_type<edge_t>;
    };

    // Concepts
    template<typename G>
    concept graph_type = graph_traits<G>::value;

    template<typename G>
    concept digraph_type = graph_type<G> && graph_traits<G>::is_directed;

    template<typename G>
    concept undirected_graph_type = graph_type<G> && graph_traits<G>::is_undirected;

    template<typename G>
    concept weighted_graph_type = graph_type<G> && graph_traits<G>::is_weighted;

    template<typename G>
    concept weighted_digraph_type = digraph_type<G> && graph_traits<G>::is_weighted;

    template<typename G>
    concept weighted_undirected_graph_type = undirected_graph_type<G> && graph_traits<G>::is_weighted;
}

#ifndef CP_ALGO_GRAPH_CONCEPTS_HPP
#define CP_ALGO_GRAPH_CONCEPTS_HPP
#include "edge_types.hpp"
#include <type_traits>
namespace cp_algo::graph{enum graph_mode{directed,undirected};template<typename T,typename=void>struct graph_traits:std::false_type{};template<typename T>struct graph_traits<T,std::void_t<typename T::edge_t,decltype(T::mode)>>:std::true_type{using edge_t=typename T::edge_t;static constexpr auto mode=T::mode;static constexpr bool is_directed=mode==directed;static constexpr bool is_undirected=mode==undirected;static constexpr bool is_weighted=weighted_edge_type<edge_t>;};template<typename G>concept graph_type=graph_traits<G>::value;template<typename G>concept digraph_type=graph_type<G>&&graph_traits<G>::is_directed;template<typename G>concept undirected_graph_type=graph_type<G>&&graph_traits<G>::is_undirected;template<typename G>concept weighted_graph_type=graph_type<G>&&graph_traits<G>::is_weighted;template<typename G>concept weighted_digraph_type=digraph_type<G>&&graph_traits<G>::is_weighted;template<typename G>concept weighted_undirected_graph_type=undirected_graph_type<G>&&graph_traits<G>::is_weighted;}
#endif
#line 1 "cp-algo/graph/concepts.hpp"
#line 1 "cp-algo/graph/edge_types.hpp"
#include <iostream>
#include <cstdint>
namespace cp_algo::graph{using node_index=int;struct edge_base{int xor_nodes;edge_base(){}edge_base(node_index from,node_index to):xor_nodes(from^to){}node_index traverse(node_index from)const{return xor_nodes^from;}static auto read(node_index v0=0){node_index u,v;std::cin>>u>>v;u-=v0;v-=v0;return std::pair{u,edge_base(u,v)};}};struct weighted_edge:edge_base{int64_t w;weighted_edge(){}weighted_edge(node_index from,node_index to,int64_t w):edge_base(from,to),w(w){}static auto read(node_index v0=0){auto[u,e]=edge_base::read(v0);int64_t w;std::cin>>w;return std::pair{u,weighted_edge(u,e.traverse(u),w)};}};template<typename edge>concept edge_type=std::is_base_of_v<edge_base,edge>;template<typename edge>concept weighted_edge_type=std::is_base_of_v<weighted_edge,edge>;}
#line 4 "cp-algo/graph/concepts.hpp"
#include <type_traits>
namespace cp_algo::graph{enum graph_mode{directed,undirected};template<typename T,typename=void>struct graph_traits:std::false_type{};template<typename T>struct graph_traits<T,std::void_t<typename T::edge_t,decltype(T::mode)>>:std::true_type{using edge_t=typename T::edge_t;static constexpr auto mode=T::mode;static constexpr bool is_directed=mode==directed;static constexpr bool is_undirected=mode==undirected;static constexpr bool is_weighted=weighted_edge_type<edge_t>;};template<typename G>concept graph_type=graph_traits<G>::value;template<typename G>concept digraph_type=graph_type<G>&&graph_traits<G>::is_directed;template<typename G>concept undirected_graph_type=graph_type<G>&&graph_traits<G>::is_undirected;template<typename G>concept weighted_graph_type=graph_type<G>&&graph_traits<G>::is_weighted;template<typename G>concept weighted_digraph_type=digraph_type<G>&&graph_traits<G>::is_weighted;template<typename G>concept weighted_undirected_graph_type=undirected_graph_type<G>&&graph_traits<G>::is_weighted;}
Back to top page