This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/2559"
#include "../../Src/Template/IOSetting.hpp"
#include "../../Src/DataStructure/SparseTable/DualSparseTable.hpp"
#include "../../Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp"
#include "../../Src/Graph/Tree/HeavyLightDecomposition.hpp"
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
#include <cassert>
using namespace zawa;
struct MIN {
using Element = int;
static Element identity() {
return (int)2e9;
}
static Element operation(Element L, Element R) {
return std::min(L, R);
}
};
int main() {
SetFastIO();
int N, M;
std::cin >> N >> M;
std::vector<std::tuple<int, int, int, int>> E(M);
for (int i{} ; auto& [w, a, b, id] : E) {
std::cin >> a >> b >> w;
a--; b--;
id = i++;
}
std::sort(E.begin(), E.end());
DisjointSetUnion dsu(N);
std::vector<int> id(M, -1);
int use_edge{};
long long base{};
for (auto [w, a, b, i] : E) {
if (dsu.same(a, b)) continue;
base += w;
dsu.merge(a, b);
id[i] = N + use_edge++;
}
assert(use_edge <= N - 1);
if (use_edge < N - 1) {
for (int i{} ; i < M ; i++) std::cout << -1 << '\n';
return 0;
}
std::vector<std::vector<int>> T(N + N - 1);
for (auto [_, a, b, i] : E) if (id[i] != -1) {
T[a].push_back(id[i]);
T[id[i]].push_back(a);
// AddEdge(T, a, id[i]);
T[b].push_back(id[i]);
T[id[i]].push_back(b);
// AddEdge(T, b, id[i]);
}
HeavyLightDecomposition hld{T};
const int INF{MIN::identity()};
DualSparseTable<MIN> spt{std::vector<int>(N + N - 1, INF)};
for (auto [w, a, b, i] : E) if (id[i] == -1) {
for (auto [l, r] : hld(a, b)) {
l = hld[l];
r = hld[r];
if (l > r) std::swap(l, r);
spt.operation(l, r + 1, w);
}
}
auto prod{spt.build()};
std::vector<long long> ans(M, -1LL);
for (auto [w, a, b, i] : E) {
if (id[i] == -1) ans[i] = base;
else {
int find{prod[hld[id[i]]]};
ans[i] = (find == INF ? -1LL : base - w + find);
}
}
for (int i{} ; i < M ; i++) {
std::cout << ans[i] << '\n';
}
}
#line 1 "Test/AOJ/2559.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/2559"
#line 2 "Src/Template/IOSetting.hpp"
#line 2 "Src/Template/TypeAlias.hpp"
#include <cstdint>
#include <cstddef>
namespace zawa {
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using usize = std::size_t;
} // namespace zawa
#line 4 "Src/Template/IOSetting.hpp"
#include <iostream>
#include <iomanip>
namespace zawa {
void SetFastIO() {
std::cin.tie(nullptr)->sync_with_stdio(false);
}
void SetPrecision(u32 dig) {
std::cout << std::fixed << std::setprecision(dig);
}
} // namespace zawa
#line 2 "Src/DataStructure/SparseTable/DualSparseTable.hpp"
#line 2 "Src/Algebra/Monoid/MonoidConcept.hpp"
#line 2 "Src/Algebra/Semigroup/SemigroupConcept.hpp"
#include <concepts>
namespace zawa {
namespace concepts {
template <class T>
concept Semigroup = requires {
typename T::Element;
{ T::operation(std::declval<typename T::Element>(), std::declval<typename T::Element>()) } -> std::same_as<typename T::Element>;
};
} // namespace concepts
} // namespace zawa
#line 4 "Src/Algebra/Monoid/MonoidConcept.hpp"
#line 6 "Src/Algebra/Monoid/MonoidConcept.hpp"
namespace zawa {
namespace concepts {
template <class T>
concept Identitiable = requires {
typename T::Element;
{ T::identity() } -> std::same_as<typename T::Element>;
};
template <class T>
concept Monoid = Semigroup<T> and Identitiable<T>;
} // namespace
} // namespace zawa
#line 5 "Src/DataStructure/SparseTable/DualSparseTable.hpp"
#include <vector>
#include <cassert>
namespace zawa {
template <concepts::Monoid S>
class DualSparseTable {
public:
using Value = typename S::Element;
DualSparseTable() = default;
DualSparseTable(const std::vector<Value>& A)
: n_{A.size()}, L_(A.size() + 1), dat_{} {
assert(A.size());
for (u32 i{1} ; i < L_.size() ; i++) {
L_[i] = L_[i - 1] + (i >> (L_[i - 1] + 1));
}
dat_ = std::vector(L_.back() + 1, std::vector(A.size(), S::identity()));
dat_[0] = A;
}
void operation(u32 L, u32 R, const Value& v) {
assert(L <= R and R <= size());
if (L == R) {
return;
}
else if (L + 1 == R) {
dat_[0][L] = S::operation(dat_[0][L], v);
}
else {
u32 now{L_[R - L]};
dat_[now][L] = S::operation(dat_[now][L], v);
dat_[now][R - (1 << now)] = S::operation(dat_[now][R - (1 << now)], v);
}
}
constexpr usize size() const {
return n_;
}
std::vector<Value> build() {
assert(dat_.size() >= 2u);
for (u32 i{static_cast<u32>(dat_.size()) - 2u} ; i + 1 < dat_.size() ; i--) {
for (u32 j{} ; j + (1 << i) < size() ; j++) {
dat_[i][j] = S::operation(dat_[i][j], dat_[i + 1][j]);
dat_[i][j + (1 << i)] = S::operation(dat_[i][j + (1 << i)], dat_[i + 1][j]);
}
}
return dat_[0];
}
private:
usize n_{};
std::vector<u32> L_;
std::vector<std::vector<Value>> dat_{};
};
} // namespace zawa
#line 2 "Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp"
#line 4 "Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp"
#include <algorithm>
#line 7 "Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp"
#include <numeric>
#line 10 "Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp"
namespace zawa {
class DisjointSetUnion {
public:
DisjointSetUnion() = default;
DisjointSetUnion(usize n) : n_{n}, comps_{n}, data_(n, -1) {
data_.shrink_to_fit();
}
u32 leader(u32 v) {
return data_[v] < 0 ? v : static_cast<u32>(data_[v] = leader(data_[v]));
}
bool same(u32 u, u32 v) {
return leader(u) == leader(v);
}
bool merge(u32 u, u32 v) {
assert(u < n_);
assert(v < n_);
u = leader(u);
v = leader(v);
if (u == v) return false;
comps_--;
if (data_[u] > data_[v]) std::swap(u, v);
data_[u] += data_[v];
data_[v] = u;
return true;
}
inline usize size() const noexcept {
return n_;
}
usize size(u32 v) {
assert(v < n_);
return static_cast<usize>(-data_[leader(v)]);
}
inline usize components() const noexcept {
return comps_;
}
template <class T = usize>
std::vector<std::vector<T>> enumerate() requires std::convertible_to<usize, T> {
std::vector<std::vector<T>> res(n_);
for (usize v{} ; v < n_ ; v++) {
res[leader(v)].push_back(static_cast<T>(v));
}
std::erase_if(res, [](const auto& arr) -> bool { return arr.empty(); });
return res;
}
private:
usize n_{}, comps_{};
std::vector<i32> data_;
};
} // namespace zawa
#line 2 "Src/Graph/Tree/HeavyLightDecomposition.hpp"
#line 4 "Src/Graph/Tree/HeavyLightDecomposition.hpp"
#line 7 "Src/Graph/Tree/HeavyLightDecomposition.hpp"
#include <cmath>
#include <limits>
#include <utility>
#line 11 "Src/Graph/Tree/HeavyLightDecomposition.hpp"
namespace zawa {
template <class V>
class HeavyLightDecomposition {
public:
static constexpr V Invalid() noexcept {
return INVALID;
}
HeavyLightDecomposition() = default;
HeavyLightDecomposition(std::vector<std::vector<V>> T, V root = 0u)
: n_{T.size()}, par_(n_), top_(n_), idx_(n_),
inv_(n_), size_(n_, usize{1}), dep_(n_) {
auto dfs1{[&](auto dfs, V v, V p, usize d) -> usize {
par_[v] = p;
dep_[v] = d;
if (p != INVALID) {
for (u32 i{} ; i + 1 < T[v].size() ; i++) if (T[v][i] == p) {
std::swap(T[v][i], T[v].back());
break;
}
assert(T[v].back() == p);
T[v].pop_back();
}
for (V x : T[v]) {
size_[v] += dfs(dfs, x, v, d + 1);
}
for (u32 i{1} ; i < T[v].size() ; i++) if (size_[T[v][0]] < size_[T[v][i]]) {
std::swap(T[v][0], T[v][i]);
}
return size_[v];
}};
auto dfs2{[&](auto dfs, V v, V idx, V top) -> V {
idx_[v] = idx++;
inv_[idx_[v]] = v;
top_[v] = top;
if (T[v].size()) {
idx = dfs(dfs, T[v][0], idx, top);
for (u32 i{1} ; i < T[v].size() ; i++) {
idx = dfs(dfs, T[v][i], idx, T[v][i]);
}
}
return idx;
}};
dfs1(dfs1, root, INVALID, 0u);
dfs2(dfs2, root, 0u, root);
}
inline usize size() const noexcept {
return n_;
}
usize size(V v) const noexcept {
assert(v < (V)size());
return size_[v];
}
usize depth(V v) const noexcept {
assert(v < (V)size());
return dep_[v];
}
V parent(V v) const noexcept {
assert(v < (V)size());
return par_[v];
}
V index(V v) const noexcept {
assert(v < (V)size());
return idx_[v];
}
V operator[](V v) const noexcept {
assert(v < (V)size());
return idx_[v];
}
std::vector<std::pair<V, V>> decomp(V s, V t) const {
assert(s < (V)size());
assert(t < (V)size());
std::vector<std::pair<V, V>> res, ser;
while (top_[s] != top_[t]) {
if (dep_[top_[s]] >= dep_[top_[t]]) {
res.emplace_back(s, top_[s]);
s = top_[s];
if (par_[s] != INVALID) s = par_[s];
}
else {
ser.emplace_back(top_[t], t);
t = top_[t];
if (par_[t] != INVALID) t = par_[t];
}
}
res.emplace_back(s, t);
std::reverse(ser.begin(), ser.end());
res.insert(res.end(), ser.begin(), ser.end());
return res;
}
std::vector<std::pair<V, V>> operator()(V s, V t) const {
return decomp(s, t);
}
V lca(V u, V v) const {
assert(u < (V)size());
assert(v < (V)size());
while (top_[u] != top_[v]) {
if (dep_[top_[u]] >= dep_[top_[v]]) {
u = top_[u];
if (par_[u] != INVALID) u = par_[u];
}
else {
v = top_[v];
if (par_[v] != INVALID) v = par_[v];
}
}
return (dep_[u] <= dep_[v] ? u : v);
}
// pはvの祖先か?
bool isAncestor(V v, V p) {
assert(v < size());
assert(p < size());
if (dep_[v] < dep_[p]) return false;
while (v != INVALID and top_[v] != top_[p]) {
v = par_[top_[v]];
}
return v != INVALID;
}
V levelAncestor(V v, usize step) const {
assert(v < (V)size());
if (step > dep_[v]) return INVALID;
while (true) {
usize dist{dep_[v] - dep_[top_[v]]};
if (dist >= step) break;
step -= dist + 1;
v = par_[top_[v]];
}
step = (dep_[v] - dep_[top_[v]]) - step;
return inv_[idx_[top_[v]] + step];
}
V jump(V s, V t, usize step) const {
assert(s < (V)size());
assert(t < (V)size());
V uu{INVALID}, vv{INVALID};
usize d{};
for (auto [u, v] : decomp(s, t)) {
usize dist{std::max(dep_[u], dep_[v]) - std::min(dep_[u], dep_[v])};
if (dist >= step) {
uu = u;
vv = v;
d = dist;
break;
}
step -= dist + 1;
}
if (uu == INVALID) return INVALID;
if (dep_[uu] <= dep_[vv]) {
return inv_[idx_[uu] + step];
}
else {
return inv_[idx_[vv] + (d - step)];
}
}
usize distance(V s, V t) const {
assert(s < (V)size());
assert(t < (V)size());
usize res{};
for (auto [u, v] : decomp(s, t)) {
if (dep_[u] > dep_[v]) std::swap(u, v);
res += dep_[v] - dep_[u];
}
return res;
}
private:
static constexpr V INVALID{static_cast<V>(-1)};
usize n_{};
std::vector<V> par_{}, top_{}, idx_{}, inv_{};
std::vector<usize> size_{}, dep_{};
};
} // namespace zawa
#line 7 "Test/AOJ/2559.test.cpp"
#line 10 "Test/AOJ/2559.test.cpp"
#include <tuple>
#line 13 "Test/AOJ/2559.test.cpp"
using namespace zawa;
struct MIN {
using Element = int;
static Element identity() {
return (int)2e9;
}
static Element operation(Element L, Element R) {
return std::min(L, R);
}
};
int main() {
SetFastIO();
int N, M;
std::cin >> N >> M;
std::vector<std::tuple<int, int, int, int>> E(M);
for (int i{} ; auto& [w, a, b, id] : E) {
std::cin >> a >> b >> w;
a--; b--;
id = i++;
}
std::sort(E.begin(), E.end());
DisjointSetUnion dsu(N);
std::vector<int> id(M, -1);
int use_edge{};
long long base{};
for (auto [w, a, b, i] : E) {
if (dsu.same(a, b)) continue;
base += w;
dsu.merge(a, b);
id[i] = N + use_edge++;
}
assert(use_edge <= N - 1);
if (use_edge < N - 1) {
for (int i{} ; i < M ; i++) std::cout << -1 << '\n';
return 0;
}
std::vector<std::vector<int>> T(N + N - 1);
for (auto [_, a, b, i] : E) if (id[i] != -1) {
T[a].push_back(id[i]);
T[id[i]].push_back(a);
// AddEdge(T, a, id[i]);
T[b].push_back(id[i]);
T[id[i]].push_back(b);
// AddEdge(T, b, id[i]);
}
HeavyLightDecomposition hld{T};
const int INF{MIN::identity()};
DualSparseTable<MIN> spt{std::vector<int>(N + N - 1, INF)};
for (auto [w, a, b, i] : E) if (id[i] == -1) {
for (auto [l, r] : hld(a, b)) {
l = hld[l];
r = hld[r];
if (l > r) std::swap(l, r);
spt.operation(l, r + 1, w);
}
}
auto prod{spt.build()};
std::vector<long long> ans(M, -1LL);
for (auto [w, a, b, i] : E) {
if (id[i] == -1) ans[i] = base;
else {
int find{prod[hld[id[i]]]};
ans[i] = (find == INF ? -1LL : base - w + find);
}
}
for (int i{} ; i < M ; i++) {
std::cout << ans[i] << '\n';
}
}