This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "src/Graph/incidence_matrix_equation.hpp"
関数名 | 概要 | 計算量 |
---|---|---|
incidence_matrix_equation<T>(g, b) |
有向グラフ $g$ の接続行列 $A$ と頂点数 $n$ の次元を持つベクトル $\boldsymbol{b}$ に対して $\displaystyle A\boldsymbol{x}=\boldsymbol{b}$ を満たす辺数 $m$ の次元を持つベクトル $\boldsymbol{x}$ を一つ返す. 引数は Graph クラス と vector<T> . 戻り値は vector<T> . 解なしなら空集合を返す. また T=bool の場合は有限体 $\mathbb{F}_2$ だとみなす. |
$O(n+m)$ |
#pragma once
#include <cassert>
#include "src/Graph/Graph.hpp"
template <class T> std::vector<T> incidence_matrix_equation(const Graph &g, std::vector<T> b) {
const int n= g.vertex_size();
assert((int)b.size() == n);
std::vector<T> x(g.edge_size());
auto adje= g.adjacency_edge(0);
std::vector<int> pre(n, -2), ei(adje.p.begin(), adje.p.begin() + n);
for (int s= 0, p, e; s < n; ++s)
if (pre[s] == -2)
for (pre[p= s]= -1;;) {
if (ei[p] == adje.p[p + 1]) {
if (e= pre[p]; e < 0) {
if (b[p] != T()) return {}; // no solution
break;
}
T tmp= b[p];
p= g[e].to(p);
if constexpr (std::is_same_v<T, bool>) x[e]= tmp, b[p]= tmp ^ b[p];
else x[e]= g[e].second == p ? -tmp : tmp, b[p]+= tmp;
} else if (int q= g[e= adje.dat[ei[p]++]].to(p); pre[q] == -2) pre[p= q]= e;
}
return x;
}
#line 2 "src/Graph/incidence_matrix_equation.hpp"
#include <cassert>
#line 2 "src/Internal/ListRange.hpp"
#include <vector>
#include <iostream>
#include <iterator>
#include <type_traits>
#define _LR(name, IT, CT) \
template <class T> struct name { \
using Iterator= typename std::vector<T>::IT; \
Iterator bg, ed; \
Iterator begin() const { return bg; } \
Iterator end() const { return ed; } \
size_t size() const { return std::distance(bg, ed); } \
CT &operator[](int i) const { return bg[i]; } \
}
_LR(ListRange, iterator, T);
_LR(ConstListRange, const_iterator, const T);
#undef _LR
template <class T> struct CSRArray {
std::vector<T> dat;
std::vector<int> p;
size_t size() const { return p.size() - 1; }
ListRange<T> operator[](int i) { return {dat.begin() + p[i], dat.begin() + p[i + 1]}; }
ConstListRange<T> operator[](int i) const { return {dat.cbegin() + p[i], dat.cbegin() + p[i + 1]}; }
};
template <template <class> class F, class T> std::enable_if_t<std::disjunction_v<std::is_same<F<T>, ListRange<T>>, std::is_same<F<T>, ConstListRange<T>>, std::is_same<F<T>, CSRArray<T>>>, std::ostream &> operator<<(std::ostream &os, const F<T> &r) {
os << '[';
for (int _= 0, __= r.size(); _ < __; ++_) os << (_ ? ", " : "") << r[_];
return os << ']';
}
#line 3 "src/Graph/Graph.hpp"
struct Edge: std::pair<int, int> {
using std::pair<int, int>::pair;
Edge &operator--() { return --first, --second, *this; }
int to(int v) const { return first ^ second ^ v; }
friend std::istream &operator>>(std::istream &is, Edge &e) { return is >> e.first >> e.second, is; }
};
struct Graph: std::vector<Edge> {
size_t n;
Graph(size_t n= 0, size_t m= 0): vector(m), n(n) {}
size_t vertex_size() const { return n; }
size_t edge_size() const { return size(); }
size_t add_vertex() { return n++; }
size_t add_edge(int s, int d) { return emplace_back(s, d), size() - 1; }
size_t add_edge(Edge e) { return emplace_back(e), size() - 1; }
#define _ADJ_FOR(a, b) \
for (auto [u, v]: *this) a; \
for (size_t i= 0; i < n; ++i) p[i + 1]+= p[i]; \
for (int i= size(); i--;) { \
auto [u, v]= (*this)[i]; \
b; \
}
#define _ADJ(a, b) \
vector<int> p(n + 1), c(size() << !dir); \
if (!dir) { \
_ADJ_FOR((++p[u], ++p[v]), (c[--p[u]]= a, c[--p[v]]= b)) \
} else if (dir > 0) { \
_ADJ_FOR(++p[u], c[--p[u]]= a) \
} else { \
_ADJ_FOR(++p[v], c[--p[v]]= b) \
} \
return {c, p}
CSRArray<int> adjacency_vertex(int dir) const { _ADJ(v, u); }
CSRArray<int> adjacency_edge(int dir) const { _ADJ(i, i); }
#undef _ADJ
#undef _ADJ_FOR
};
#line 4 "src/Graph/incidence_matrix_equation.hpp"
template <class T> std::vector<T> incidence_matrix_equation(const Graph &g, std::vector<T> b) {
const int n= g.vertex_size();
assert((int)b.size() == n);
std::vector<T> x(g.edge_size());
auto adje= g.adjacency_edge(0);
std::vector<int> pre(n, -2), ei(adje.p.begin(), adje.p.begin() + n);
for (int s= 0, p, e; s < n; ++s)
if (pre[s] == -2)
for (pre[p= s]= -1;;) {
if (ei[p] == adje.p[p + 1]) {
if (e= pre[p]; e < 0) {
if (b[p] != T()) return {}; // no solution
break;
}
T tmp= b[p];
p= g[e].to(p);
if constexpr (std::is_same_v<T, bool>) x[e]= tmp, b[p]= tmp ^ b[p];
else x[e]= g[e].second == p ? -tmp : tmp, b[p]+= tmp;
} else if (int q= g[e= adje.dat[ei[p]++]].to(p); pre[q] == -2) pre[p= q]= e;
}
return x;
}