CompProgLibrary

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

View the Project on GitHub RTnF/CompProgLibrary

:heavy_check_mark: cpp/graph/topological_sort.hpp

Depends on

Verified with

Code

#pragma once
#include "graph/graph_list.hpp"
#include "template/small_template.hpp"

/**
 * トポロジカルソート(Kahn) O(E + V)
 * 返り値:DAGの場合根から順、非DAGの場合空
 * https://ja.wikipedia.org/wiki/トポロジカルソート
 */
template <class Cost, class E>
vector<int> ListGraph<Cost, E>::topological_sort() {
  vector<int> sorted_node, in_count(n_, 0);
  // vector<bool> visited(n_, false);
  sorted_node.reserve(n_);
  stack<int> st;
  for (auto &es : adj) {
    for (auto &e : es) {
      in_count[e.to]++;
    }
  }
  for (int i = 0; i < n_; ++i) {
    if (in_count[i] == 0) {
      st.emplace(i);
    }
  }
  while (!st.empty()) {
    int v = st.top();
    st.pop();
    // visited[v] = true;
    sorted_node.emplace_back(v);
    for (auto &e : adj[v]) {
      if (--in_count[e.to] == 0) {
        st.emplace(e.to);
      }
    }
  }
  if ((int)sorted_node.size() != n_) {
    return vector<int>();
  }
  return sorted_node;
}

/**
 * トポロジカルソート(Kahn)
 * 辞書順最小O(E + V log V)
 */
template <class Cost, class E>
vector<int> ListGraph<Cost, E>::topological_sort_minimum() {
  vector<int> sorted_node, in_count(n_, 0);
  // vector<bool> visited(n_, false);
  sorted_node.reserve(n_);
  priority_queue<int, vector<int>, greater<int>> pq;
  for (auto &es : adj) {
    for (auto &e : es) {
      in_count[e.to]++;
    }
  }
  for (int i = 0; i < n_; ++i) {
    if (in_count[i] == 0) {
      pq.emplace(i);
    }
  }
  while (!pq.empty()) {
    int v = pq.top();
    pq.pop();
    // visited[v] = true;
    sorted_node.emplace_back(v);
    for (auto &e : adj[v]) {
      if (--in_count[e.to] == 0) {
        pq.emplace(e.to);
      }
    }
  }
  if ((int)sorted_node.size() != n_) {
    return vector<int>();
  }
  return sorted_node;
}
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.12.0/x64/lib/python3.12/site-packages/onlinejudge_verify/documentation/build.py", line 71, in _render_source_code_stat
    bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.0/x64/lib/python3.12/site-packages/onlinejudge_verify/languages/cplusplus.py", line 187, in bundle
    bundler.update(path)
  File "/opt/hostedtoolcache/Python/3.12.0/x64/lib/python3.12/site-packages/onlinejudge_verify/languages/cplusplus_bundle.py", line 401, in update
    self.update(self._resolve(pathlib.Path(included), included_from=path))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.0/x64/lib/python3.12/site-packages/onlinejudge_verify/languages/cplusplus_bundle.py", line 260, in _resolve
    raise BundleErrorAt(path, -1, "no such header")
onlinejudge_verify.languages.cplusplus_bundle.BundleErrorAt: graph/graph_list.hpp: line -1: no such header
Back to top page