competitive-programming-library

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

View the Project on GitHub kmyk/competitive-programming-library

:heavy_check_mark: graph/quotient_graph.hpp

Depends on

Verified with

Code

#pragma once
#include <vector>
#include "../graph/transpose_graph.hpp"
#include "../utils/macros.hpp"

/**
 * @param g is an adjacent list of a digraph
 * @param size is the size of equivalence classes
 * @param component_of is the map from original vertices to equivalence classes
 * @note $O(V + E)$
 * @see https://en.wikipedia.org/wiki/Quotient_graph
 */
std::vector<std::vector<int> > make_quotient_graph(const std::vector<std::vector<int> > & g, int size, const std::vector<int> & component_of) {
    int n = g.size();
    std::vector<std::vector<int> > h(size);
    REP (i, n) for (int j : g[i]) {
        if (component_of[i] != component_of[j]) {
            h[component_of[i]].push_back(component_of[j]);
        }
    }
    REP (k, size) {
        std::sort(ALL(h[k]));
        h[k].erase(std::unique(ALL(h[k])), h[k].end());
    }
    return h;
}
#line 2 "graph/quotient_graph.hpp"
#include <vector>
#line 2 "utils/macros.hpp"
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
#line 4 "graph/transpose_graph.hpp"

/**
 * @param g is an adjacent list of a digraph
 * @note $O(V + E)$
 * @see https://en.wikipedia.org/wiki/Transpose_graph
 */
std::vector<std::vector<int> > make_transpose_graph(std::vector<std::vector<int> > const & g) {
    int n = g.size();
    std::vector<std::vector<int> > h(n);
    REP (i, n) {
        for (int j : g[i]) {
            h[j].push_back(i);
        }
    }
    return h;
}
#line 5 "graph/quotient_graph.hpp"

/**
 * @param g is an adjacent list of a digraph
 * @param size is the size of equivalence classes
 * @param component_of is the map from original vertices to equivalence classes
 * @note $O(V + E)$
 * @see https://en.wikipedia.org/wiki/Quotient_graph
 */
std::vector<std::vector<int> > make_quotient_graph(const std::vector<std::vector<int> > & g, int size, const std::vector<int> & component_of) {
    int n = g.size();
    std::vector<std::vector<int> > h(size);
    REP (i, n) for (int j : g[i]) {
        if (component_of[i] != component_of[j]) {
            h[component_of[i]].push_back(component_of[j]);
        }
    }
    REP (k, size) {
        std::sort(ALL(h[k]));
        h[k].erase(std::unique(ALL(h[k])), h[k].end());
    }
    return h;
}
Back to top page