競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
#define SKIP_RUNTIME_CHECKS
// Verify-SHA256: e787920ebc7b0cb0935d303730f59278c1f1c00695fcb80a97d7e19fd54ba22e
// Verify: https://atcoder.jp/contests/abc191/tasks/abc191_e
// Library: Graph/Directed_Return_Distances.cpp
// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/directed_return_distances.md
//
// API一覧
// directed_return_distances(n,edges): 各頂点から1辺以上通って同じ頂点に戻る最短距離を返す。
// ReturnDistanceArc{from,to,weight}。頂点は0始まり、重みはuint64_t。自己辺・多重辺・重み0も可。
// 戻れないか、最短距離がuint64_tの上限を超える頂点はnullopt。
// 強連結成分が単一の有向閉路なら一周の和で済む。残りの各成分で始点別にDijkstraを行う。
// 成分cの頂点数・内部辺数をn_c,m_cとし、最悪O(n+m+Σ n_c*m_c*log(m_c+1))時間、O(n+m)領域。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <optional>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_UNWEIGHTED_GRAPH_DEFINED
#define SIRKIT_UNWEIGHTED_GRAPH_DEFINED
using Graph = std::vector<std::vector<int>>;
#endif
#ifndef SIRKIT_SCC_DETAIL_DEFINED
#define SIRKIT_SCC_DETAIL_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
// 既存CSR用の次数配列を調べる。通常のm!=nでは呼ばれない。
template<class Index>
#if defined(__GNUC__) || defined(__clang__)
[[gnu::noinline]]
#endif
inline bool outdegrees_all_one(const std::vector<Index>& degree) {
for(auto it=degree.begin()+1;it!=degree.end();++it)if(*it!=1)return false;
return true;
}
// 出次数が全て1なら、閉路と流入木を反復的に分解する。
#if defined(__GNUC__) || defined(__clang__)
[[gnu::noinline]]
#endif
inline std::pair<int,std::vector<int>> functional_component_ids(
int n,const std::vector<std::pair<int,int>>& edges) {
constexpr int on_path=std::numeric_limits<int>::min();
// ~vは[-n,-1]なので、n<=INT_MAXではon_pathと衝突しない。
std::vector<int> ids(n);
for(auto [u,v]:edges)ids[u]=~v;
auto path=std::make_unique_for_overwrite<int[]>(n);
int top=0,count=0;
for(int root=0;root<n;++root) {
if(ids[root]>=0)continue;
int v=root,state;
while((state=ids[v])<0&&state!=on_path) {
ids[v]=on_path;path[top++]=v;v=~state;
}
if(state==on_path) {
int member;
do{member=path[--top];ids[member]=count;}while(member!=v);
++count;
}
while(top!=0)ids[path[--top]]=count++;
}
for(auto& id:ids)id=count-1-id;
return {count,std::move(ids)};
}
// Indexは総辺数を表現できる符号なし型。
template<class Index>
inline std::pair<int, std::vector<int>> directed_component_ids_index(
int n, const std::vector<std::pair<int, int>>& edges) {
std::vector<Index> begin(static_cast<std::size_t>(n) + 1);
for (auto [u, v] : edges) { (void)v; ++begin[static_cast<std::size_t>(u) + 1]; }
if(edges.size()==static_cast<std::size_t>(n) && outdegrees_all_one(begin))
return functional_component_ids(n,edges);
for (int v = 0; v < n; ++v) begin[v + 1] += begin[v];
std::vector<Index> next(begin.begin() + 1, begin.end());
// CSRの全要素へ書き込んでから参照するため、先行初期化は不要。
auto destination = std::make_unique_for_overwrite<int[]>(edges.size());
// 辺順を保って逆向きに埋め、走査位置の再初期化を省く。
for (auto it = edges.rbegin(); it != edges.rend(); ++it)
destination[--next[it->first]] = it->second;
// idsは中断中のlow-linkの退避先を兼ね、確定時に成分番号を書く。
std::vector<int> entered(n, -1), ids(n), dfs, pending;
dfs.reserve(n); pending.reserve(n);
int clock = 0, count = 0;
for (int root = 0; root < n; ++root) {
if (entered[root] != -1) continue;
int v = root;
Index cursor = next[v], end = begin[v + 1];
// 出辺0の頂点は即確定し、DFS番号の割当ても省く。
if (cursor == end) {
entered[v] = n; ids[v] = count++;
continue;
}
int low = entered[v] = clock++;
pending.push_back(v);
for (;;) {
if (cursor != end) {
const int u = destination[cursor++];
if (entered[u] == -1) {
const Index child_cursor = next[u], child_end = begin[u + 1];
if (child_cursor == child_end) {
entered[u] = n; ids[u] = count++;
continue;
}
// 子へ降りるときだけ、親の走査位置とlow-linkを退避する。
next[v] = cursor;
ids[v] = low;
dfs.push_back(v);
v = u;
low = entered[v] = clock++;
cursor = child_cursor; end = child_end;
pending.push_back(v);
} else low = std::min(low, entered[u]);
continue;
}
// 成分番号への上書き後も親へ渡せるよう、low-linkを退避する。
const int child_low = low;
if (child_low == entered[v]) {
for (;;) {
const int u = pending.back(); pending.pop_back();
entered[u] = n; ids[u] = count;
if (u == v) break;
}
++count;
}
if (dfs.empty()) break;
v = dfs.back(); dfs.pop_back();
low = std::min(ids[v], child_low);
cursor = next[v]; end = begin[v + 1];
}
}
for (auto& id : ids) id = count - 1 - id;
return {count, std::move(ids)};
}
template<std::uint32_t kCompactEdgeLimit = std::numeric_limits<std::uint32_t>::max()>
inline std::pair<int, std::vector<int>> directed_component_ids(
int n, const std::vector<std::pair<int, int>>& edges) {
// 無辺では各頂点が1成分。従来と同じ逆番号で返す。
if (edges.empty()) {
std::vector<int> ids(n);
for (int v = 0; v < n; ++v) ids[v] = n - 1 - v;
return {n, std::move(ids)};
}
// 辺数が収まる場合だけCSRの添字を32bitにする。
if (edges.size() <= kCompactEdgeLimit)
return directed_component_ids_index<std::uint32_t>(n, edges);
return directed_component_ids_index<std::size_t>(n, edges);
}
inline int SccVertexCount(std::size_t vertices) {
if (vertices > static_cast<std::size_t>(std::numeric_limits<int>::max()))
throw std::length_error("SCC: too many vertices");
return static_cast<int>(vertices);
}
}
#endif
#ifndef SIRKIT_SCC_GRAPH_DEFINED
#define SIRKIT_SCC_GRAPH_DEFINED
class scc_graph {
int n_ = 0, count_ = 0;
std::vector<std::pair<int, int>> edges_;
std::vector<int> ids_;
#ifndef SKIP_RUNTIME_CHECKS
bool built_ = false;
void vertex(int v) const {
if (v < 0 || v >= n_) throw std::out_of_range("SCC: vertex out of range");
}
void require_build() const {
if (!built_) throw std::logic_error("SCC: call scc or build first");
}
#endif
public:
scc_graph() = default;
explicit scc_graph(int n) : n_(n) {
if (n < 0) throw std::length_error("SCC: negative size");
}
explicit scc_graph(const Graph& g)
: n_(/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::SccVertexCount(g.size())) {
for (int u = 0; u < n_; ++u) for (int v : g[u]) add_edge(u, v);
build();
}
scc_graph(const scc_graph&) = default;
scc_graph(scc_graph&& other) noexcept { swap(other); }
scc_graph& operator=(scc_graph other) noexcept { swap(other); return *this; }
void swap(scc_graph& other) noexcept {
std::swap(n_, other.n_); std::swap(count_, other.count_);
edges_.swap(other.edges_); ids_.swap(other.ids_);
#ifndef SKIP_RUNTIME_CHECKS
std::swap(built_, other.built_);
#endif
}
void reset(int n = 0) { scc_graph replacement(n); swap(replacement); }
int size() const noexcept { return n_; }
std::size_t edge_count() const noexcept { return edges_.size(); }
void reserve_edges(std::size_t count) { edges_.reserve(count); }
void add_edge(int u, int v) {
#ifndef SKIP_RUNTIME_CHECKS
vertex(u); vertex(v);
#endif
edges_.emplace_back(u, v);
#ifndef SKIP_RUNTIME_CHECKS
built_ = false;
#endif
}
int build() {
auto result = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::directed_component_ids(n_, edges_);
ids_.swap(result.second); count_ = result.first;
#ifndef SKIP_RUNTIME_CHECKS
built_ = true;
#endif
return count_;
}
int component_count() const {
#ifndef SKIP_RUNTIME_CHECKS
require_build();
#endif
return count_;
}
int id(int v) const {
#ifndef SKIP_RUNTIME_CHECKS
require_build(); vertex(v);
#endif
return ids_[v];
}
const std::vector<int>& ids() const {
#ifndef SKIP_RUNTIME_CHECKS
require_build();
#endif
return ids_;
}
std::vector<std::vector<int>> scc() {
build();
std::vector<std::vector<int>> groups(count_);
// 1成分・全頂点が別成分ならサイズの集計は不要。
if (count_ == 1) {
auto& group = groups.front();
group.resize(n_);
for (int v = 0; v < n_; ++v) group[v] = v;
return groups;
}
if (count_ == n_) {
// 確保も成分順に行い、返す頂点群の配置を揃える。
for (auto& group : groups) group.reserve(1);
for (int v = 0; v < n_; ++v) groups[ids_[v]].push_back(v);
return groups;
}
std::vector<int> sizes(count_);
for (int id : ids_) ++sizes[id];
for (int id = 0; id < count_; ++id) groups[id].reserve(sizes[id]);
for (int v = 0; v < n_; ++v) groups[ids_[v]].push_back(v);
return groups;
}
Graph condensation(bool unique_edges = true) const {
#ifndef SKIP_RUNTIME_CHECKS
require_build();
#endif
Graph result(count_);
if (!unique_edges) {
for (auto [u, v] : edges_) if (ids_[u] != ids_[v]) result[ids_[u]].push_back(ids_[v]);
return result;
}
for (auto [u, v] : edges_) if (ids_[u] != ids_[v]) result[ids_[u]].push_back(ids_[v]);
std::vector<int> last(count_, -1);
// 同じ行で重複を詰め、頂点群の二重確保を避ける。
for (int from = 0; from < count_; ++from) {
auto& row = result[from];
std::size_t used = 0;
for (int to : row) if (last[to] != from) {
last[to] = from; row[used++] = to;
}
row.resize(used);
// 多重辺を大量に除いた行の余分な容量を解放する。
if (used < row.capacity() / 2) {
std::vector<int> compact(row);
row.swap(compact);
}
}
return result;
}
};
#endif
#ifndef SIRKIT_DIRECTED_RETURN_DISTANCES_DEFINED
#define SIRKIT_DIRECTED_RETURN_DISTANCES_DEFINED
struct ReturnDistanceArc
{
int from;
int to;
std::uint64_t weight;
};
[[nodiscard]] inline std::vector<std::optional<std::uint64_t>>
directed_return_distances(int n, std::span<const ReturnDistanceArc> edges)
{
scc_graph components(n);
components.reserve_edges(edges.size());
for (const auto &[u, v, weight] : edges)
{
components.add_edge(u, v);
}
const int count = components.build();
const auto &id = components.ids();
using Arc = std::pair<int, std::uint64_t>;
std::vector<std::vector<Arc>> adjacent(n);
for (const auto &[u, v, weight] : edges)
{
if (id[u] == id[v])
{
adjacent[u].emplace_back(v, weight);
}
}
constexpr auto kMax = std::numeric_limits<std::uint64_t>::max();
std::vector<bool> simple_cycle(count, true);
std::vector<std::optional<std::uint64_t>> total(count, 0);
for (int u = 0; u < n; ++u)
{
if (adjacent[u].size() != 1)
{
simple_cycle[id[u]] = false;
}
else if (auto ¤t = total[id[u]]; current)
{
const auto weight = adjacent[u].front().second;
if (weight > kMax - *current)
{
current.reset();
}
else
{
*current += weight;
}
}
}
using State = std::pair<std::uint64_t, int>;
constexpr std::greater<State> kGreater;
std::vector<State> pending;
std::vector<int> stamp(n, -1);
std::vector<std::uint64_t> distance(n);
std::vector<std::optional<std::uint64_t>> result(n);
for (int source = 0; source < n; ++source)
{
if (adjacent[source].empty())
{
continue;
}
if (simple_cycle[id[source]])
{
result[source] = total[id[source]];
continue;
}
auto push = [&](int v, std::uint64_t value)
{
if (stamp[v] != source || value < distance[v])
{
stamp[v] = source;
distance[v] = value;
pending.emplace_back(value, v);
std::ranges::push_heap(pending, kGreater);
}
};
pending.clear();
for (const auto &[v, weight] : adjacent[source])
{
push(v, weight);
}
while (!pending.empty())
{
std::ranges::pop_heap(pending, kGreater);
const auto [current, u] = pending.back();
pending.pop_back();
if (current != distance[u])
{
continue;
}
if (u == source)
{
result[source] = current;
break;
}
for (const auto &[v, weight] : adjacent[u])
{
if (weight <= kMax - current)
{
push(v, current + weight);
}
}
}
}
return result;
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
namespace sirkit {}
using namespace sirkit;
// SPDX-License-Identifier: CC0-1.0
// Verify I/O: Verify/current/common/fast_io.hpp
// SPDX-License-Identifier: CC0-1.0
#include <algorithm>
#include <array>
#include <charconv>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
#ifndef SIRKIT_VERIFY_FAST_IO_DEFINED
#define SIRKIT_VERIFY_FAST_IO_DEFINED
// VerifyInput(file=stdin): 非所有のFILEから読む。別の入力方式と混用しない。
// >> 整数: 64bit以下の十進整数を読む。検査省略時は値域内の正しい入力を前提とする。
// >> char/string: 空白を飛ばし、1文字/空白までの文字列を読む。
// >> 浮動小数点: 十進文字列を変換する。通常のCロケールで使う。
// bool / !: 直前までの読み取りが成功しているか/失敗したかを返す。
class VerifyInput {
static constexpr std::size_t buffer_size = 1 << 17;
std::FILE* file_;
std::array<unsigned char, buffer_size + 1> buffer_;
std::size_t position_ = 0, size_ = 0;
bool good_ = true;
int peek() {
if (position_ == size_) {
size_ = std::fread(buffer_.data(), 1, buffer_size, file_);
buffer_[size_] = 0; // 補充後の末尾は常に数字でない番兵。
position_ = 0;
if (size_ == 0) return EOF;
}
return buffer_[position_];
}
int skip_space() {
int c;
while ((c = peek()) != EOF && c <= ' ') ++position_;
if (c == EOF) good_ = false;
return c;
}
public:
explicit VerifyInput(std::FILE* file = stdin) : file_(file) {}
VerifyInput(const VerifyInput&) = delete;
VerifyInput& operator=(const VerifyInput&) = delete;
explicit operator bool() const noexcept { return good_; }
bool operator!() const noexcept { return !good_; }
template<class T>
requires (std::is_integral_v<T> && sizeof(T) <= sizeof(std::uint64_t)
&& !std::is_same_v<T, char>)
VerifyInput& operator>>(T& result) {
if (!good_) return *this;
int c = skip_space();
if (c == EOF) return *this;
bool negative = false;
if (c == '-' || c == '+') {
negative = c == '-';
++position_;
c = peek();
}
if (c == EOF) {
good_ = false;
return *this;
}
#ifndef SKIP_RUNTIME_CHECKS
if (c < '0' || c > '9') {
good_ = false;
return *this;
}
// 先頭の0を除けば、20桁以下の末尾直前の値はuint64_tに収まる。
while (c == '0') {
++position_;
c = peek();
}
std::uint64_t previous = 0;
unsigned last_digit = 0;
std::size_t digits = 0;
#endif
std::uint64_t value = 0;
#ifdef SKIP_RUNTIME_CHECKS
// バッファ末尾の番兵で内側の範囲判定を省き、数字が境界をまたぐときだけ補充する。
for (;;) {
const unsigned char* first = buffer_.data() + position_;
const unsigned char* cursor = first;
while (static_cast<unsigned>(*cursor - '0') < 10) {
value = value * 10 + static_cast<unsigned>(*cursor - '0');
++cursor;
}
position_ += static_cast<std::size_t>(cursor - first);
if (position_ != size_) break;
c = peek();
if (c == EOF) break;
}
#else
while (c >= '0' && c <= '9') {
previous = value;
last_digit = static_cast<unsigned>(c - '0');
if (digits <= 20) ++digits;
value = value * 10 + static_cast<unsigned>(c - '0');
++position_;
c = peek();
}
#endif
#ifndef SKIP_RUNTIME_CHECKS
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
if ((c != EOF && c > ' ') || digits > 20
|| (digits == 20 && (previous > maximum / 10
|| (previous == maximum / 10 && last_digit > maximum % 10)))) {
good_ = false;
return *this;
}
std::uint64_t limit = static_cast<std::uint64_t>(std::numeric_limits<T>::max());
if constexpr (std::is_signed_v<T>) limit += negative;
else if (negative && value != 0) good_ = false;
if (value > limit) good_ = false;
if (!good_) return *this;
#endif
if constexpr (std::is_signed_v<T>) {
constexpr std::uint64_t minimum_magnitude =
static_cast<std::uint64_t>(std::numeric_limits<T>::max()) + 1;
// -(最小値)は評価せず、絶対値が最大値+1のときだけ最小値を使う。
result = negative
? (value == minimum_magnitude ? std::numeric_limits<T>::min()
: static_cast<T>(-static_cast<T>(value)))
: static_cast<T>(value);
} else result = static_cast<T>(value);
return *this;
}
VerifyInput& operator>>(char& result) {
if (!good_) return *this;
const int c = skip_space();
if (c != EOF) {
result = static_cast<char>(c);
++position_;
}
return *this;
}
VerifyInput& operator>>(std::string& result) {
if (!good_) return *this;
int c = skip_space();
if (c == EOF) return *this;
result.clear();
for (;;) {
const std::size_t first = position_;
while (position_ < size_ && buffer_[position_] > ' ') ++position_;
result.append(reinterpret_cast<const char*>(buffer_.data() + first), position_ - first);
if (position_ != size_ || peek() == EOF) break;
}
return *this;
}
template<class T> requires std::is_floating_point_v<T>
VerifyInput& operator>>(T& result) {
std::string token;
*this >> token;
if (!good_) return *this;
#ifndef SKIP_RUNTIME_CHECKS
char* end = nullptr;
const long double value = std::strtold(token.c_str(), &end);
if (end != token.c_str() + token.size()) good_ = false;
else result = static_cast<T>(value);
#else
result = static_cast<T>(std::strtold(token.c_str(), nullptr));
#endif
return *this;
}
};
// VerifyOutput(file=stdout): 非所有のFILEへ書く。破棄時にflushし、FILEは閉じない。
// << 整数/char/文字列/浮動小数点: 値を出力する。整数は64bit以下に対応する。
// fixed(digits): 浮動小数点を小数点以下digits桁で出力する(digits>=0)。
// flush(): バッファの内容をFILEへ渡す。bool / !で書き込み失敗を確認できる。
class VerifyOutput {
static constexpr std::size_t buffer_size = 1 << 17;
std::FILE* file_;
std::array<char, buffer_size> buffer_;
std::size_t size_ = 0;
int precision_ = 6;
bool fixed_ = false, good_ = true;
void write(std::string_view text) {
while (!text.empty() && good_) {
if (size_ == buffer_.size()) flush();
const auto count = std::min(text.size(), buffer_.size() - size_);
std::memcpy(buffer_.data() + size_, text.data(), count);
size_ += count;
text.remove_prefix(count);
}
}
public:
explicit VerifyOutput(std::FILE* file = stdout) : file_(file) {}
VerifyOutput(const VerifyOutput&) = delete;
VerifyOutput& operator=(const VerifyOutput&) = delete;
~VerifyOutput() { flush(); }
explicit operator bool() const noexcept { return good_; }
bool operator!() const noexcept { return !good_; }
VerifyOutput& fixed(int digits) {
if (digits < 0) throw std::invalid_argument("negative output precision");
fixed_ = true;
precision_ = digits;
return *this;
}
void flush() noexcept {
std::size_t written = 0;
while (written < size_ && good_) {
const auto count = std::fwrite(buffer_.data() + written, 1, size_ - written, file_);
if (count == 0) good_ = false;
written += count;
}
size_ = 0;
}
template<class T>
requires (std::is_integral_v<T> && sizeof(T) <= sizeof(std::uint64_t)
&& !std::is_same_v<T, char>)
VerifyOutput& operator<<(T value) {
if constexpr (std::is_same_v<T, bool>) return *this << static_cast<char>('0' + value);
else {
constexpr std::size_t capacity = std::numeric_limits<std::uint64_t>::digits10 + 3;
if (buffer_.size() - size_ < capacity) flush();
if (!good_) return *this;
const auto end = std::to_chars(buffer_.data() + size_, buffer_.data() + size_ + capacity, value).ptr;
size_ = static_cast<std::size_t>(end - buffer_.data());
return *this;
}
}
VerifyOutput& operator<<(char value) {
if (size_ == buffer_.size()) flush();
if (good_) buffer_[size_++] = value;
return *this;
}
VerifyOutput& operator<<(std::string_view value) { write(value); return *this; }
VerifyOutput& operator<<(const std::string& value) { write(value); return *this; }
VerifyOutput& operator<<(const char* value) { write(value); return *this; }
template<class T> requires std::is_floating_point_v<T>
VerifyOutput& operator<<(T value) {
char local[128];
const char* format = fixed_ ? "%.*Lf" : "%.*Lg";
const int count = std::snprintf(local, sizeof(local), format, precision_,
static_cast<long double>(value));
if (count < 0) good_ = false;
else if (static_cast<std::size_t>(count) < sizeof(local))
write(std::string_view(local, static_cast<std::size_t>(count)));
else {
std::vector<char> text(static_cast<std::size_t>(count) + 1);
std::snprintf(text.data(), text.size(), format, precision_, static_cast<long double>(value));
write(std::string_view(text.data(), static_cast<std::size_t>(count)));
}
return *this;
}
};
inline VerifyInput verify_in;
inline VerifyOutput verify_out;
#endif
#include <vector>
int main()
{
int n{}, m{};
verify_in >> n >> m;
std::vector<sirkit::ReturnDistanceArc> edges(m);
for (auto &[u, v, weight] : edges)
{
verify_in >> u >> v >> weight;
--u;
--v;
}
for (const auto distance : sirkit::directed_return_distances(n, edges))
{
if (distance)
{
verify_out << *distance << '\n';
}
else
{
verify_out << -1 << '\n';
}
}
}