競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
有向グラフで、辺重みの総和が指定した余りになる歩道を調べます。歩道では同じ頂点や辺を何度でも通れます。
WalkWeightCongruence(n, edges) に WalkCongruenceEdge{from, to, weight} を渡します。頂点は 0 始まりで、重みは符号付きの int32_t です。
period(v) は v へ戻る歩道の重みの最大公約数です。重みが 0 の歩道しかなければ 0 を返します。periods() では強連結成分ごとの周期を一度に取得できます。relation(u, v) は同じ強連結成分で WalkWeightClass{offset, period} を返します。別成分なら nullopt です。matches(residue, modulus) は、総重みが residue と法 modulus で合同な歩道の有無を返します。法は正の整数です。周期が正なら、重みはすべて offset と法 period で合同になります。整数として offset そのものを実現できるとは限りません。周期が 0 の場合、同成分内の歩道には重み offset だけが現れます。
構築は O(n + m log(nW + 2)) 時間と O(n + m) 領域です。W には辺重みの絶対値の上限を使います。周期と合同類の取得は O(1)、余りの判定は O(log(modulus + 1)) 時間です。
WalkCongruenceEdge{from,to,weight}: 0始まりの有向辺と符号付き int32_t 重み。
WalkWeightCongruence(n,edges): 強連結成分ごとに閉歩道の重みの最大公約数を求める。
構築 O(n+m log(nW+2)) 時間・O(n+m) 追加領域。W は重みの絶対値の上限。
period(v)/periods(): vの成分の周期/全成分の周期の範囲。すべての閉歩道の重みが0なら周期0。O(1)。
relation(u,v): 同成分なら WalkWeightClass{offset,period}。別成分はnullopt。O(1)。
重みは offset modulo period。period=0ならすべて同じ offset。頂点と辺の再訪を許す。
WalkWeightClass::matches(residue,modulus): 指定余りの歩道が存在するか。modulus>0。O(log(modulus+1))。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| E - 無限グラフ | walk_weight_congruence_lift.cpp | 未提出 | — |
| G - Return to 1 | walk_weight_congruence_return.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/walk_weight_congruence.md
//
// API一覧
// WalkCongruenceEdge{from,to,weight}: 0始まりの有向辺と符号付き int32_t 重み。
// WalkWeightCongruence(n,edges): 強連結成分ごとに閉歩道の重みの最大公約数を求める。
// 構築 O(n+m log(nW+2)) 時間・O(n+m) 追加領域。W は重みの絶対値の上限。
// period(v)/periods(): vの成分の周期/全成分の周期の範囲。すべての閉歩道の重みが0なら周期0。O(1)。
// relation(u,v): 同成分なら WalkWeightClass{offset,period}。別成分はnullopt。O(1)。
// 重みは offset modulo period。period=0ならすべて同じ offset。頂点と辺の再訪を許す。
// WalkWeightClass::matches(residue,modulus): 指定余りの歩道が存在するか。modulus>0。O(log(modulus+1))。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <memory>
#include <numeric>
#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_WALK_WEIGHT_CONGRUENCE_DEFINED
#define SIRKIT_WALK_WEIGHT_CONGRUENCE_DEFINED
struct WalkCongruenceEdge
{
int from;
int to;
std::int32_t weight;
};
struct WalkWeightClass
{
std::int64_t offset;
std::uint64_t period;
[[nodiscard]] bool matches(std::int64_t residue, std::uint64_t modulus) const
{
#ifndef SKIP_RUNTIME_CHECKS
if (modulus == 0)
{
throw std::invalid_argument("WalkWeightClass: zero modulus");
}
#endif
const auto divisor = std::gcd(modulus, period);
auto normalize = [divisor](std::int64_t x)
{
const auto bits = static_cast<std::uint64_t>(x);
if (x >= 0)
{
return bits % divisor;
}
const auto magnitude = (0 - bits) % divisor;
return magnitude == 0 ? 0 : divisor - magnitude;
};
return normalize(residue) == normalize(offset);
}
};
class WalkWeightCongruence
{
public:
WalkWeightCongruence(int n, std::span<const WalkCongruenceEdge> edges)
{
scc_graph components(n);
components.reserve_edges(edges.size());
for (const auto &[u, v, weight] : edges)
{
components.add_edge(u, v);
}
periods_.resize(components.build());
component_ = components.ids();
struct Arc
{
int to;
std::int32_t weight;
};
std::vector<std::vector<Arc>> graph(n);
for (const auto &[u, v, weight] : edges)
{
if (component_[u] == component_[v])
{
graph[u].push_back({v, weight});
}
}
// 単純パスは高々 n-1 辺。int32_t 重みの総和と2本のパスの差は int64_t に収まる。
constexpr auto kUnvisited = std::numeric_limits<std::int64_t>::max();
potential_.assign(n, kUnvisited);
std::vector<int> stack;
for (int root = 0; root < n; ++root)
{
if (potential_[root] != kUnvisited)
{
continue;
}
potential_[root] = 0;
stack.push_back(root);
auto &period = periods_[component_[root]];
while (!stack.empty())
{
const int u = stack.back();
stack.pop_back();
for (const auto &[v, weight] : graph[u])
{
const auto candidate = potential_[u] + weight;
if (potential_[v] == kUnvisited)
{
potential_[v] = candidate;
stack.push_back(v);
}
else
{
const auto difference = candidate - potential_[v];
const auto magnitude = difference < 0 ? -difference : difference;
period = std::gcd(period, static_cast<std::uint64_t>(magnitude));
}
}
}
}
}
[[nodiscard]] std::uint64_t period(int vertex) const
{
check_vertex(vertex);
return periods_[component_[vertex]];
}
[[nodiscard]] std::span<const std::uint64_t> periods() const noexcept
{
return periods_;
}
[[nodiscard]] std::optional<WalkWeightClass> relation(int source, int target) const
{
check_vertex(source);
check_vertex(target);
if (component_[source] != component_[target])
{
return std::nullopt;
}
return WalkWeightClass{potential_[target] - potential_[source], periods_[component_[source]]};
}
private:
void check_vertex([[maybe_unused]] int vertex) const
{
#ifndef SKIP_RUNTIME_CHECKS
if (static_cast<std::size_t>(vertex) >= component_.size())
{
throw std::out_of_range("WalkWeightCongruence: invalid vertex");
}
#endif
}
std::vector<int> component_;
std::vector<std::int64_t> potential_;
std::vector<std::uint64_t> periods_;
};
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1