incremental_scc.hpp¶
离线处理按顺序加入有向边后的强连通变化;适合询问两点最早何时进入同一 SCC。
Complexity: Time: O((V + E) log E). Space: O(V + E log E).
AC 记录:incremental_scc。
Implementation¶
当前头文件,省略 include guard;依赖见 #include。
/// @complexity Time: O((V + E) log E).
/// Space: O(V + E log E).
#include "atcoder/scc.hpp"
#include <algorithm>
#include <cassert>
#include <functional>
#include <tuple>
#include <utility>
#include <vector>
namespace noya {
/// @brief Return a certificate time for every edge in an incremental graph.
/// Divide the prefix-time interval at its midpoint and compute SCCs using the
/// edges already present there. Edges internal to one SCC recurse into the
/// earlier half; all other edges recurse into the later half after contracting
/// those SCCs. Thus every recursion level is linear in its active graph. An
/// edge with returned time t joins its endpoint components after the first t
/// insertions; applying all such joins reconstructs every SCC partition.
/// `m+1` denotes an edge that never joins two distinct components.
inline std::vector<int>
incremental_scc_merge_times(int n, const std::vector<std::pair<int, int>> &es) {
assert(n >= 0);
int m = int(es.size());
for (auto [v, to] : es) {
assert(0 <= v && v < n);
assert(0 <= to && to < n);
}
std::vector<int> mt(m, m + 1);
std::vector<std::tuple<int, int, int>> act;
act.reserve(m);
for (int id = 0; id < m; id++) {
act.emplace_back(id, es[id].first, es[id].second);
}
std::vector<int> idx(n, -1);
auto dfs = [&](auto &self, std::vector<std::tuple<int, int, int>> dat, int tl,
int tr) -> void {
if (dat.empty() || tr == tl + 1) {
return;
}
int tm = (tl + tr) / 2;
int sz = 0;
for (auto [id, v, to] : dat) {
if (idx[v] == -1) {
idx[v] = sz++;
}
if (idx[to] == -1) {
idx[to] = sz++;
}
}
atcoder::scc_graph g(sz);
for (auto [id, v, to] : dat) {
if (id < tm) {
g.add_edge(idx[v], idx[to]);
}
}
auto dsu = g.scc();
std::vector<int> bel(sz);
for (int com = 0; com < int(dsu.size()); com++) {
for (int u : dsu[com]) {
bel[u] = com;
}
}
std::vector<std::tuple<int, int, int>> pre;
std::vector<std::tuple<int, int, int>> nxt;
pre.reserve(dat.size());
nxt.reserve(dat.size());
for (auto [id, u0, v0] : dat) {
int v = idx[u0];
int to = idx[v0];
if (id < tm && bel[v] == bel[to]) {
mt[id] = std::min(mt[id], tm);
pre.emplace_back(id, v, to);
} else {
nxt.emplace_back(id, bel[v], bel[to]);
}
}
for (auto [id, v, to] : dat) {
idx[v] = -1;
idx[to] = -1;
}
self(self, std::move(pre), tl, tm);
self(self, std::move(nxt), tm, tr);
};
dfs(dfs, std::move(act), 0, m + 1);
return mt;
}
/// @brief After every insertion, return the sum of weight products over pairs
/// of vertices in the same strongly connected com.
template <class T>
std::vector<T>
incremental_scc_pair_product_sums(const std::vector<T> &wt,
const std::vector<std::pair<int, int>> &es) {
int n = int(wt.size());
int m = int(es.size());
auto mt = incremental_scc_merge_times(n, es);
std::vector<std::vector<int>> es1(m + 1);
for (int id = 0; id < m; id++) {
if (mt[id] <= m) {
es1[mt[id]].push_back(id);
}
}
std::vector<int> fa(n, -1);
std::vector<T> sum = wt;
auto rt = [&](auto &self, int u) -> int {
if (fa[u] < 0) {
return u;
}
return fa[u] = self(self, fa[u]);
};
T tot{};
std::vector<T> res(m);
for (int tm1 = 1; tm1 <= m; tm1++) {
for (int id : es1[tm1]) {
int a = rt(rt, es[id].first);
int b = rt(rt, es[id].second);
if (a == b) {
continue;
}
tot += sum[a] * sum[b];
if (fa[a] > fa[b]) {
std::swap(a, b);
}
fa[a] += fa[b];
fa[b] = a;
sum[a] += sum[b];
}
res[tm1 - 1] = tot;
}
return res;
}
} // namespace noya
#ifndef NOYA_INCREMENTAL_SCC_HPP
#define NOYA_INCREMENTAL_SCC_HPP 1
/// @complexity Time: O((V + E) log E).
/// Space: O(V + E log E).
#include "atcoder/scc.hpp"
#include <algorithm>
#include <cassert>
#include <functional>
#include <tuple>
#include <utility>
#include <vector>
namespace noya {
/// @brief Return a certificate time for every edge in an incremental graph.
/// Divide the prefix-time interval at its midpoint and compute SCCs using the
/// edges already present there. Edges internal to one SCC recurse into the
/// earlier half; all other edges recurse into the later half after contracting
/// those SCCs. Thus every recursion level is linear in its active graph. An
/// edge with returned time t joins its endpoint components after the first t
/// insertions; applying all such joins reconstructs every SCC partition.
/// `m+1` denotes an edge that never joins two distinct components.
inline std::vector<int>
incremental_scc_merge_times(int n, const std::vector<std::pair<int, int>> &es) {
assert(n >= 0);
int m = int(es.size());
for (auto [v, to] : es) {
assert(0 <= v && v < n);
assert(0 <= to && to < n);
}
std::vector<int> mt(m, m + 1);
std::vector<std::tuple<int, int, int>> act;
act.reserve(m);
for (int id = 0; id < m; id++) {
act.emplace_back(id, es[id].first, es[id].second);
}
std::vector<int> idx(n, -1);
auto dfs = [&](auto &self, std::vector<std::tuple<int, int, int>> dat, int tl,
int tr) -> void {
if (dat.empty() || tr == tl + 1) {
return;
}
int tm = (tl + tr) / 2;
int sz = 0;
for (auto [id, v, to] : dat) {
if (idx[v] == -1) {
idx[v] = sz++;
}
if (idx[to] == -1) {
idx[to] = sz++;
}
}
atcoder::scc_graph g(sz);
for (auto [id, v, to] : dat) {
if (id < tm) {
g.add_edge(idx[v], idx[to]);
}
}
auto dsu = g.scc();
std::vector<int> bel(sz);
for (int com = 0; com < int(dsu.size()); com++) {
for (int u : dsu[com]) {
bel[u] = com;
}
}
std::vector<std::tuple<int, int, int>> pre;
std::vector<std::tuple<int, int, int>> nxt;
pre.reserve(dat.size());
nxt.reserve(dat.size());
for (auto [id, u0, v0] : dat) {
int v = idx[u0];
int to = idx[v0];
if (id < tm && bel[v] == bel[to]) {
mt[id] = std::min(mt[id], tm);
pre.emplace_back(id, v, to);
} else {
nxt.emplace_back(id, bel[v], bel[to]);
}
}
for (auto [id, v, to] : dat) {
idx[v] = -1;
idx[to] = -1;
}
self(self, std::move(pre), tl, tm);
self(self, std::move(nxt), tm, tr);
};
dfs(dfs, std::move(act), 0, m + 1);
return mt;
}
/// @brief After every insertion, return the sum of weight products over pairs
/// of vertices in the same strongly connected com.
template <class T>
std::vector<T>
incremental_scc_pair_product_sums(const std::vector<T> &wt,
const std::vector<std::pair<int, int>> &es) {
int n = int(wt.size());
int m = int(es.size());
auto mt = incremental_scc_merge_times(n, es);
std::vector<std::vector<int>> es1(m + 1);
for (int id = 0; id < m; id++) {
if (mt[id] <= m) {
es1[mt[id]].push_back(id);
}
}
std::vector<int> fa(n, -1);
std::vector<T> sum = wt;
auto rt = [&](auto &self, int u) -> int {
if (fa[u] < 0) {
return u;
}
return fa[u] = self(self, fa[u]);
};
T tot{};
std::vector<T> res(m);
for (int tm1 = 1; tm1 <= m; tm1++) {
for (int id : es1[tm1]) {
int a = rt(rt, es[id].first);
int b = rt(rt, es[id].second);
if (a == b) {
continue;
}
tot += sum[a] * sum[b];
if (fa[a] > fa[b]) {
std::swap(a, b);
}
fa[a] += fa[b];
fa[b] = a;
sum[a] += sum[b];
}
res[tm1 - 1] = tot;
}
return res;
}
} // namespace noya
#endif // NOYA_INCREMENTAL_SCC_HPP
#include <algorithm>
#include <cassert>
#include <functional>
#include <tuple>
#include <utility>
#include <vector>
/// @complexity Time: O((V + E) log E).
/// Space: O(V + E log E).
namespace atcoder {
namespace internal {
template <class E> struct csr {
std::vector<int> start;
std::vector<E> elist;
explicit csr(int n, const std::vector<std::pair<int, E>>& edges)
: start(n + 1), elist(edges.size()) {
for (auto e : edges) {
start[e.first + 1]++;
}
for (int i = 1; i <= n; i++) {
start[i] += start[i - 1];
}
auto counter = start;
for (auto e : edges) {
elist[counter[e.first]++] = e.second;
}
}
};
} // namespace internal
} // namespace atcoder
namespace atcoder {
namespace internal {
// Reference:
// R. Tarjan,
// Depth-First Search and Linear Graph Algorithms
struct scc_graph {
public:
explicit scc_graph(int n) : _n(n) {}
int num_vertices() { return _n; }
void add_edge(int from, int to) { edges.push_back({from, {to}}); }
// @return pair of (# of scc, scc id)
std::pair<int, std::vector<int>> scc_ids() {
auto g = csr<edge>(_n, edges);
int now_ord = 0, group_num = 0;
std::vector<int> visited, low(_n), ord(_n, -1), ids(_n);
visited.reserve(_n);
auto dfs = [&](auto self, int v) -> void {
low[v] = ord[v] = now_ord++;
visited.push_back(v);
for (int i = g.start[v]; i < g.start[v + 1]; i++) {
auto to = g.elist[i].to;
if (ord[to] == -1) {
self(self, to);
low[v] = std::min(low[v], low[to]);
} else {
low[v] = std::min(low[v], ord[to]);
}
}
if (low[v] == ord[v]) {
while (true) {
int u = visited.back();
visited.pop_back();
ord[u] = _n;
ids[u] = group_num;
if (u == v) break;
}
group_num++;
}
};
for (int i = 0; i < _n; i++) {
if (ord[i] == -1) dfs(dfs, i);
}
for (auto& x : ids) {
x = group_num - 1 - x;
}
return {group_num, ids};
}
std::vector<std::vector<int>> scc() {
auto ids = scc_ids();
int group_num = ids.first;
std::vector<int> counts(group_num);
for (auto x : ids.second) counts[x]++;
std::vector<std::vector<int>> groups(ids.first);
for (int i = 0; i < group_num; i++) {
groups[i].reserve(counts[i]);
}
for (int i = 0; i < _n; i++) {
groups[ids.second[i]].push_back(i);
}
return groups;
}
private:
int _n;
struct edge {
int to;
};
std::vector<std::pair<int, edge>> edges;
};
} // namespace internal
} // namespace atcoder
namespace atcoder {
struct scc_graph {
public:
scc_graph() : internal(0) {}
explicit scc_graph(int n) : internal(n) {}
void add_edge(int from, int to) {
int n = internal.num_vertices();
assert(0 <= from && from < n);
assert(0 <= to && to < n);
internal.add_edge(from, to);
}
std::vector<std::vector<int>> scc() { return internal.scc(); }
private:
internal::scc_graph internal;
};
} // namespace atcoder
namespace noya {
/// @brief Return a certificate time for every edge in an incremental graph.
/// Divide the prefix-time interval at its midpoint and compute SCCs using the
/// edges already present there. Edges internal to one SCC recurse into the
/// earlier half; all other edges recurse into the later half after contracting
/// those SCCs. Thus every recursion level is linear in its active graph. An
/// edge with returned time t joins its endpoint components after the first t
/// insertions; applying all such joins reconstructs every SCC partition.
/// `m+1` denotes an edge that never joins two distinct components.
inline std::vector<int>
incremental_scc_merge_times(int n, const std::vector<std::pair<int, int>> &es) {
assert(n >= 0);
int m = int(es.size());
for (auto [v, to] : es) {
assert(0 <= v && v < n);
assert(0 <= to && to < n);
}
std::vector<int> mt(m, m + 1);
std::vector<std::tuple<int, int, int>> act;
act.reserve(m);
for (int id = 0; id < m; id++) {
act.emplace_back(id, es[id].first, es[id].second);
}
std::vector<int> idx(n, -1);
auto dfs = [&](auto &self, std::vector<std::tuple<int, int, int>> dat, int tl,
int tr) -> void {
if (dat.empty() || tr == tl + 1) {
return;
}
int tm = (tl + tr) / 2;
int sz = 0;
for (auto [id, v, to] : dat) {
if (idx[v] == -1) {
idx[v] = sz++;
}
if (idx[to] == -1) {
idx[to] = sz++;
}
}
atcoder::scc_graph g(sz);
for (auto [id, v, to] : dat) {
if (id < tm) {
g.add_edge(idx[v], idx[to]);
}
}
auto dsu = g.scc();
std::vector<int> bel(sz);
for (int com = 0; com < int(dsu.size()); com++) {
for (int u : dsu[com]) {
bel[u] = com;
}
}
std::vector<std::tuple<int, int, int>> pre;
std::vector<std::tuple<int, int, int>> nxt;
pre.reserve(dat.size());
nxt.reserve(dat.size());
for (auto [id, u0, v0] : dat) {
int v = idx[u0];
int to = idx[v0];
if (id < tm && bel[v] == bel[to]) {
mt[id] = std::min(mt[id], tm);
pre.emplace_back(id, v, to);
} else {
nxt.emplace_back(id, bel[v], bel[to]);
}
}
for (auto [id, v, to] : dat) {
idx[v] = -1;
idx[to] = -1;
}
self(self, std::move(pre), tl, tm);
self(self, std::move(nxt), tm, tr);
};
dfs(dfs, std::move(act), 0, m + 1);
return mt;
}
/// @brief After every insertion, return the sum of weight products over pairs
/// of vertices in the same strongly connected com.
template <class T>
std::vector<T>
incremental_scc_pair_product_sums(const std::vector<T> &wt,
const std::vector<std::pair<int, int>> &es) {
int n = int(wt.size());
int m = int(es.size());
auto mt = incremental_scc_merge_times(n, es);
std::vector<std::vector<int>> es1(m + 1);
for (int id = 0; id < m; id++) {
if (mt[id] <= m) {
es1[mt[id]].push_back(id);
}
}
std::vector<int> fa(n, -1);
std::vector<T> sum = wt;
auto rt = [&](auto &self, int u) -> int {
if (fa[u] < 0) {
return u;
}
return fa[u] = self(self, fa[u]);
};
T tot{};
std::vector<T> res(m);
for (int tm1 = 1; tm1 <= m; tm1++) {
for (int id : es1[tm1]) {
int a = rt(rt, es[id].first);
int b = rt(rt, es[id].second);
if (a == b) {
continue;
}
tot += sum[a] * sum[b];
if (fa[a] > fa[b]) {
std::swap(a, b);
}
fa[a] += fa[b];
fa[b] = a;
sum[a] += sum[b];
}
res[tm1 - 1] = tot;
}
return res;
}
} // namespace noya