競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.yosupo.jp/problem/incremental_minimum_spanning_forest"
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#include "../../../src/data_structures/link-cut-tree.hpp"
// (重み, 辺の番号) の最大。辺のない頂点は (-1, -1)
struct MaxEdge {
using S = std::pair<long long, int>;
static S op(const S& a, const S& b) { return std::max(a, b); }
static S e() { return {-1, -1}; }
};
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
int n, m;
std::cin >> n >> m;
// 頂点 0..n-1 と、辺 i を表す頂点 n + i を持ち、辺 i は u - (n + i) - v とつなぐ
LinkCutTree<MaxEdge> lct(n + m);
std::vector<int> eu(m), ev(m);
for (int i = 0; i < m; i++) {
int u, v;
long long w;
std::cin >> u >> v >> w;
eu[i] = u, ev[i] = v;
lct.set(n + i, {w, i});
int removed = -1;
if (u == v) {
removed = i;
} else if (lct.connected(u, v)) {
// u と v を結ぶパス上の最も重い辺より軽ければ入れ替える
auto [mw, j] = lct.prod(u, v);
if (mw < w) {
removed = i;
} else {
lct.cut(eu[j], n + j);
lct.cut(n + j, ev[j]);
removed = j;
}
}
if (removed != i) {
lct.link(u, n + i);
lct.link(n + i, v);
}
std::cout << removed << (i + 1 < m ? ' ' : '\n');
}
}#line 1 "test/yosupo/graph/incremental-minimum-spanning-forest.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/incremental_minimum_spanning_forest"
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#line 2 "src/data_structures/link-cut-tree.hpp"
/**
* @file link-cut-tree.hpp
* @brief Link-Cut Tree
*/
#include <cassert>
#include <initializer_list>
#include <type_traits>
#line 13 "src/data_structures/link-cut-tree.hpp"
#line 2 "src/utility/traits.hpp"
/**
* @file traits.hpp
* @brief 型特性
*/
#include <cstddef>
#include <functional>
#include <iterator>
#include <set>
#line 14 "src/utility/traits.hpp"
#if __cplusplus >= 202002L
#include <concepts>
#include <ranges>
#endif
namespace internal_traits {
using std::begin, std::end;
#if __cplusplus >= 202002L
template <class T>
struct is_range_impl : std::bool_constant < requires(T& t) {
begin(t);
end(t);
}>{};
#else
template <class T, class = void>
struct is_range_impl : std::false_type {};
template <class T>
struct is_range_impl<T, std::void_t<decltype(begin(std::declval<T&>()), end(std::declval<T&>()))>> : std::true_type {};
#endif
// サイズ付き範囲から要素を直接構築するコンストラクタで共有する。
#if __cplusplus >= 202002L
template <class R, class S>
concept sized_constructible_range = std::ranges::input_range<R> && std::ranges::sized_range<R> &&
std::is_constructible_v<S, std::ranges::range_reference_t<R>>;
template <class R>
auto range_size(R& r) {
return std::ranges::size(r);
}
template <class R>
auto range_begin(R& r) {
return std::ranges::begin(r);
}
template <class R>
using range_value_t = std::ranges::range_value_t<R>;
#else
using std::size;
template <class R>
auto range_size(R& r) -> decltype(size(r)) {
return size(r);
}
template <class R>
auto range_begin(R& r) -> decltype(begin(r)) {
return begin(r);
}
template <class R>
using range_value_t = typename std::iterator_traits<decltype(range_begin(std::declval<R&>()))>::value_type;
template <class R, class S, class = void>
struct is_sized_constructible_range : std::false_type {};
template <class R, class S>
struct is_sized_constructible_range<R, S, std::void_t<decltype(range_size(std::declval<R&>())), decltype(++std::declval<decltype(begin(std::declval<R&>()))&>()), decltype(begin(std::declval<R&>()) != end(std::declval<R&>())), decltype(*begin(std::declval<R&>()))>>
: std::is_constructible<S, decltype(*begin(std::declval<R&>()))> {};
#endif
} // namespace internal_traits
template <class T>
struct is_range : internal_traits::is_range_impl<T>::type {};
template <class T>
inline constexpr bool is_range_v = is_range<T>::value;
#if __cplusplus >= 202002L
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(); }; \
} \
template <class T> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T>> {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, class U> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(std::declval<U>()); }; \
} \
template <class T, class U> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T, U>> {}; \
template <class T, class U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#else
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T> \
struct has_##func_name##_impl<T, std::void_t<decltype(std::declval<T>().func_name())>> \
: std::true_type {}; \
} \
template <class T> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T>::type {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, typename U, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T, typename U> \
struct has_##func_name##_impl<T, U, std::void_t<decltype(std::declval<T>().func_name(std::declval<U>()))>> \
: std::true_type {}; \
} \
template <class T, typename U> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T, U>::type {}; \
template <class T, typename U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#endif
HAS_METHOD(repr)
HAS_METHOD(type_str)
HAS_METHOD(initializer_str)
HAS_METHOD(max)
HAS_METHOD(min)
HAS_METHOD(reversed)
HAS_METHOD(sorted)
HAS_METHOD(sum)
HAS_METHOD(product)
HAS_METHOD(product_xor)
HAS_METHOD_ARG(count)
HAS_METHOD_ARG(find)
HAS_METHOD_ARG(lower_bound)
HAS_METHOD_ARG(upper_bound)
#define ENABLE_IF_T_IMPL(expr) std::enable_if_t<expr, std::nullptr_t> = nullptr
#define ENABLE_IF_T(...) ENABLE_IF_T_IMPL((__VA_ARGS__))
namespace internal_traits {
// 射影 (proj) の既定値
#if __cplusplus >= 202002L
using identity = std::identity;
#else
struct identity {
using is_transparent = void;
template <class T>
constexpr T&& operator()(T&& x) const noexcept {
return std::forward<T>(x);
}
};
#endif
} // namespace internal_traits
template <class C>
using mem_value_type = typename C::value_type;
template <class C>
using mem_difference_type = typename C::difference_type;
namespace internal_traits {
template <class T, class = void>
struct is_sorted_container_impl : std::false_type {};
template <class T>
struct is_sorted_container_impl<std::set<T>> : std::true_type {};
template <class T>
struct is_sorted_container_impl<std::multiset<T>> : std::true_type {};
} // namespace internal_traits
template <class T>
struct is_sorted_container : internal_traits::is_sorted_container_impl<T>::type {};
template <class T>
inline constexpr bool is_sorted_container_v = is_sorted_container<T>::value;
#line 15 "src/data_structures/link-cut-tree.hpp"
/**
* @brief Link-Cut Tree (森で、辺の追加・削除とパス上の値の集約ができる)
*
* 各操作はならし O(log n)。内部の配列は int で持つ
* 例:
* LinkCutTree<monoid::Sum<long long>> lct(a);
* lct.link(0, 1);
* long long s = lct.prod(0, 1);
*
* @tparam M モノイド (algebra/monoid.hpp)。可換でなくてもよい (prod は u から v の向きにたたむ)
*/
template <typename M>
class LinkCutTree {
using S = typename M::S;
struct Node {
int l = -1, r = -1, p = -1;
bool rev = false;
S val, sum, rsum; // sum は左から右、rsum は右から左にたたんだもの
};
M m;
S op(const S& a, const S& b) const { return m.op(a, b); }
S e() const { return m.e(); }
std::vector<Node> t;
std::vector<int> stk;
bool is_root(int x) const {
int p = t[x].p;
return p == -1 || (t[p].l != x && t[p].r != x);
}
S sum(int x) const { return x == -1 ? e() : t[x].sum; }
S rsum(int x) const { return x == -1 ? e() : t[x].rsum; }
void update(int x) {
t[x].sum = op(op(sum(t[x].l), t[x].val), sum(t[x].r));
t[x].rsum = op(op(rsum(t[x].r), t[x].val), rsum(t[x].l));
}
void toggle(int x) {
if (x == -1) return;
std::swap(t[x].l, t[x].r);
std::swap(t[x].sum, t[x].rsum);
t[x].rev = !t[x].rev;
}
void push(int x) {
if (!t[x].rev) return;
toggle(t[x].l);
toggle(t[x].r);
t[x].rev = false;
}
void rotate(int x) {
int p = t[x].p, g = t[p].p;
if (t[p].l == x) {
t[p].l = t[x].r;
if (t[x].r != -1) t[t[x].r].p = p;
t[x].r = p;
} else {
t[p].r = t[x].l;
if (t[x].l != -1) t[t[x].l].p = p;
t[x].l = p;
}
t[p].p = x, t[x].p = g;
if (g != -1) {
if (t[g].l == p) t[g].l = x;
if (t[g].r == p) t[g].r = x;
}
update(p), update(x);
}
void splay(int x) {
for (int y = x;; y = t[y].p) {
stk.push_back(y);
if (is_root(y)) break;
}
while (!stk.empty()) push(stk.back()), stk.pop_back();
while (!is_root(x)) {
int p = t[x].p;
if (!is_root(p)) rotate((t[t[p].p].l == p) == (t[p].l == x) ? p : x);
rotate(x);
}
}
// x から根までのパスを 1 本の splay 木にし、x をその根にする。最後に splay した頂点を返す
int access(int x) {
int last = -1;
for (int y = x; y != -1; y = t[y].p) {
splay(y);
t[y].r = last;
update(y);
last = y;
}
splay(x);
return last;
}
public:
/**
* @brief 頂点数 n、辺なし、値はすべて単位元
*
* @param m モノイド
*/
explicit LinkCutTree(long long n = 0, M m = M()) : m(std::move(m)) {
Node x;
x.val = x.sum = x.rsum = e();
t.assign(n, x);
}
/**
* @brief 頂点 i の値が a[i]、辺なし
*/
#if __cplusplus >= 202002L
template <class R = std::initializer_list<S>>
requires internal_traits::sized_constructible_range<R, S>
#else
template <class R = std::initializer_list<S>, ENABLE_IF_T(internal_traits::is_sized_constructible_range<R, S>::value)>
#endif
explicit LinkCutTree(R&& a, M m = M()) : LinkCutTree(internal_traits::range_size(a), std::move(m)) {
long long i = 0;
for (auto&& x : a) {
t[i].val = t[i].sum = t[i].rsum = S(std::forward<decltype(x)>(x));
++i;
}
}
/**
* @brief 頂点数
*/
long long size() const { return static_cast<long long>(t.size()); }
/**
* @brief v を根にする
*/
void evert(long long v) {
access(v);
toggle(v);
push(v);
}
/**
* @brief 辺 (u, v) を追加する。u と v は別の木にあること
*/
void link(long long u, long long v) {
assert(!connected(u, v));
evert(u);
t[u].p = static_cast<int>(v);
}
/**
* @brief 辺 (u, v) を削除する。辺 (u, v) があること
*/
void cut(long long u, long long v) {
evert(u);
access(v);
assert(t[v].l == u && t[u].r == -1);
t[v].l = t[u].p = -1;
update(v);
}
/**
* @brief v を含む木の根
*/
long long root(long long v) {
access(v);
int x = static_cast<int>(v);
while (push(x), t[x].l != -1) x = t[x].l;
splay(x);
return x;
}
/**
* @brief u と v が同じ木にあるか
*/
bool connected(long long u, long long v) { return u == v || root(u) == root(v); }
/**
* @brief u と v の最小共通祖先 (根は最後に evert した頂点など、今の根)。u と v は同じ木にあること
*/
long long lca(long long u, long long v) {
assert(connected(u, v));
access(u);
return access(v);
}
/**
* @brief 頂点 v の値を x にする
*/
void set(long long v, const S& x) {
access(v);
t[v].val = x;
update(v);
}
/**
* @brief 頂点 v の値
*/
S get(long long v) const { return t[v].val; }
/**
* @brief 頂点 v の値を op(get(v), x) にする
*/
void apply(long long v, const S& x) { set(v, op(t[v].val, x)); }
/**
* @brief u から v へのパス上の頂点 (端点を含む) の値を、u の側から順にたたんだもの。u と v は同じ木にあること
*
* u が根になる
*/
S prod(long long u, long long v) {
assert(connected(u, v));
evert(u);
access(v);
return t[v].sum;
}
};
/**
* @brief 部分木の値の集約もできる Link-Cut Tree
*
* パスに含まれない子の部分木の値 (virtual) を持つ。各操作はならし O(log n)
* 例:
* SubtreeLinkCutTree<group::Sum<long long>> lct(a);
* lct.link(0, 1);
* long long s = lct.subtree_prod(1, 0); // 0 を親としたときの 1 の部分木の和
*
* @tparam G 可換な群 (algebra/group.hpp)
*/
template <typename M>
LinkCutTree(long long, M) -> LinkCutTree<M>;
template <class R, typename M>
LinkCutTree(R&&, M) -> LinkCutTree<M>;
template <typename M>
LinkCutTree(std::initializer_list<typename M::S>, M) -> LinkCutTree<M>;
template <typename G>
class SubtreeLinkCutTree {
using S = typename G::S;
struct Node {
int l = -1, r = -1, p = -1;
bool rev = false;
S val, path, virt, sum; // path はパス上、sum は virtual も含めた和
};
G g;
S op(const S& a, const S& b) const { return g.op(a, b); }
S e() const { return g.e(); }
S inv(const S& a) const { return g.inv(a); }
std::vector<Node> t;
std::vector<int> stk;
bool is_root(int x) const {
int p = t[x].p;
return p == -1 || (t[p].l != x && t[p].r != x);
}
S path(int x) const { return x == -1 ? e() : t[x].path; }
S sum(int x) const { return x == -1 ? e() : t[x].sum; }
void update(int x) {
t[x].path = op(op(path(t[x].l), t[x].val), path(t[x].r));
t[x].sum = op(op(sum(t[x].l), op(t[x].val, t[x].virt)), sum(t[x].r));
}
void toggle(int x) {
if (x == -1) return;
std::swap(t[x].l, t[x].r);
t[x].rev = !t[x].rev;
}
void push(int x) {
if (!t[x].rev) return;
toggle(t[x].l);
toggle(t[x].r);
t[x].rev = false;
}
void rotate(int x) {
int p = t[x].p, g = t[p].p;
if (t[p].l == x) {
t[p].l = t[x].r;
if (t[x].r != -1) t[t[x].r].p = p;
t[x].r = p;
} else {
t[p].r = t[x].l;
if (t[x].l != -1) t[t[x].l].p = p;
t[x].l = p;
}
t[p].p = x, t[x].p = g;
if (g != -1) {
if (t[g].l == p) t[g].l = x;
if (t[g].r == p) t[g].r = x;
}
update(p), update(x);
}
void splay(int x) {
for (int y = x;; y = t[y].p) {
stk.push_back(y);
if (is_root(y)) break;
}
while (!stk.empty()) push(stk.back()), stk.pop_back();
while (!is_root(x)) {
int p = t[x].p;
if (!is_root(p)) rotate((t[t[p].p].l == p) == (t[p].l == x) ? p : x);
rotate(x);
}
}
int access(int x) {
int last = -1;
for (int y = x; y != -1; y = t[y].p) {
splay(y);
// 今の右の子は virtual になり、last は virtual でなくなる
t[y].virt = op(op(t[y].virt, sum(t[y].r)), inv(sum(last)));
t[y].r = last;
update(y);
last = y;
}
splay(x);
return last;
}
public:
explicit SubtreeLinkCutTree(long long n = 0, G g = G()) : g(std::move(g)) {
Node x;
x.val = x.path = x.virt = x.sum = e();
t.assign(n, x);
}
#if __cplusplus >= 202002L
template <class R = std::initializer_list<S>>
requires internal_traits::sized_constructible_range<R, S>
#else
template <class R = std::initializer_list<S>, ENABLE_IF_T(internal_traits::is_sized_constructible_range<R, S>::value)>
#endif
explicit SubtreeLinkCutTree(R&& a, G g = G()) : SubtreeLinkCutTree(internal_traits::range_size(a), std::move(g)) {
long long i = 0;
for (auto&& x : a) {
t[i].val = t[i].path = t[i].sum = S(std::forward<decltype(x)>(x));
++i;
}
}
long long size() const { return static_cast<long long>(t.size()); }
void evert(long long v) {
access(v);
toggle(v);
push(v);
}
void link(long long u, long long v) {
assert(!connected(u, v));
evert(u);
access(v);
t[u].p = static_cast<int>(v);
t[v].virt = op(t[v].virt, t[u].sum);
update(v);
}
void cut(long long u, long long v) {
evert(u);
access(v);
assert(t[v].l == u && t[u].r == -1);
t[v].l = t[u].p = -1;
update(v);
}
long long root(long long v) {
access(v);
int x = static_cast<int>(v);
while (push(x), t[x].l != -1) x = t[x].l;
splay(x);
return x;
}
bool connected(long long u, long long v) { return u == v || root(u) == root(v); }
long long lca(long long u, long long v) {
assert(connected(u, v));
access(u);
return access(v);
}
void set(long long v, const S& x) {
access(v);
t[v].val = x;
update(v);
}
S get(long long v) const { return t[v].val; }
void apply(long long v, const S& x) { set(v, op(t[v].val, x)); }
/**
* @brief u から v へのパス上の頂点 (端点を含む) の値の和。u と v は同じ木にあること
*/
S prod(long long u, long long v) {
assert(connected(u, v));
evert(u);
access(v);
return t[v].path;
}
/**
* @brief p を根としたときの v の部分木の値の和 (p == v なら木全体)。v と p は同じ木にあること
*
* p が根になる
*/
S subtree_prod(long long v, long long p) {
assert(connected(v, p));
evert(p);
access(v);
return op(t[v].val, t[v].virt);
}
};
template <typename G>
SubtreeLinkCutTree(long long, G) -> SubtreeLinkCutTree<G>;
template <class R, typename G>
SubtreeLinkCutTree(R&&, G) -> SubtreeLinkCutTree<G>;
template <typename G>
SubtreeLinkCutTree(std::initializer_list<typename G::S>, G) -> SubtreeLinkCutTree<G>;
#line 9 "test/yosupo/graph/incremental-minimum-spanning-forest.test.cpp"
// (重み, 辺の番号) の最大。辺のない頂点は (-1, -1)
struct MaxEdge {
using S = std::pair<long long, int>;
static S op(const S& a, const S& b) { return std::max(a, b); }
static S e() { return {-1, -1}; }
};
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
int n, m;
std::cin >> n >> m;
// 頂点 0..n-1 と、辺 i を表す頂点 n + i を持ち、辺 i は u - (n + i) - v とつなぐ
LinkCutTree<MaxEdge> lct(n + m);
std::vector<int> eu(m), ev(m);
for (int i = 0; i < m; i++) {
int u, v;
long long w;
std::cin >> u >> v >> w;
eu[i] = u, ev[i] = v;
lct.set(n + i, {w, i});
int removed = -1;
if (u == v) {
removed = i;
} else if (lct.connected(u, v)) {
// u と v を結ぶパス上の最も重い辺より軽ければ入れ替える
auto [mw, j] = lct.prod(u, v);
if (mw < w) {
removed = i;
} else {
lct.cut(eu[j], n + j);
lct.cut(n + j, ev[j]);
removed = j;
}
}
if (removed != i) {
lct.link(u, n + i);
lct.link(n + i, v);
}
std::cout << removed << (i + 1 < m ? ' ' : '\n');
}
}