This documentation is automatically generated by online-judge-tools/verification-helper
#include "data_structure/weighted_unionfind.hpp"
parents
でまとめているdiff_weight
が持つ(頂点が根ならアーベル群の単位元を入れる)merge()
x
に対し、weight(x)
は leader(x)
からの差分となっているw
は weight(y) - weight(x)
となっており、w += weight(x)
によって w
は weight(y) - weight(leader(x))
となり、w -= weight(y)
によって w
は weight(leader(y)) - weight(leader(x))
となり、最後に、leader(x)
と leader(y)
を Union by Size しているleader(x)
の重みを 0 として、相対的に表現しているため、diff_weight[y] = w
でOKgroups()
#pragma once
template <class T> struct WeightedUnionFind {
int n;
std::vector<int> parents;
std::vector<T> diff_weight;
WeightedUnionFind() {}
WeightedUnionFind(int n) : n(n), parents(n, -1), diff_weight(n, T(0)) {}
int leader(int x) {
if (parents[x] < 0) {
return x;
} else {
int r = leader(parents[x]);
diff_weight[x] += diff_weight[parents[x]];
return parents[x] = r;
}
}
T weight(int x) {
leader(x);
return diff_weight[x];
}
// weight(y) = weight(x) + w
bool merge(int x, int y, T w) {
w += weight(x);
w -= weight(y);
x = leader(x), y = leader(y);
if (x == y) return false;
if (parents[x] > parents[y]) std::swap(x, y), w = -w;
parents[x] += parents[y];
parents[y] = x;
diff_weight[y] = w;
return true;
}
// return weight(y) - weight(x)
T diff(int x, int y) { return weight(y) - weight(x); }
bool same(int x, int y) { return leader(x) == leader(y); }
int size(int x) { return -parents[leader(x)]; }
// 連結成分ごとに (頂点番号 v, diff(leader(v), v)) の配列を返す
std::vector<std::vector<std::pair<int, T>>> groups() {
std::vector<int> leader_buf(n), group_size(n);
for (int i = 0; i < n; i++) {
leader_buf[i] = leader(i);
group_size[leader_buf[i]]++;
}
std::vector<std::vector<std::pair<int, T>>> result(n);
for (int i = 0; i < n; i++) {
result[i].reserve(group_size[i]);
}
for (int i = 0; i < n; i++) {
result[leader_buf[i]].emplace_back(i, weight(i));
}
result.erase(std::remove_if(result.begin(), result.end(), [&](const std::vector<std::pair<int, T>>& v) { return v.empty(); }), result.end());
return result;
}
void init(int n) { parents.assign(n, -1), diff_weight.assign(n, T(0)); } // reset
};
#line 2 "data_structure/weighted_unionfind.hpp"
template <class T> struct WeightedUnionFind {
int n;
std::vector<int> parents;
std::vector<T> diff_weight;
WeightedUnionFind() {}
WeightedUnionFind(int n) : n(n), parents(n, -1), diff_weight(n, T(0)) {}
int leader(int x) {
if (parents[x] < 0) {
return x;
} else {
int r = leader(parents[x]);
diff_weight[x] += diff_weight[parents[x]];
return parents[x] = r;
}
}
T weight(int x) {
leader(x);
return diff_weight[x];
}
// weight(y) = weight(x) + w
bool merge(int x, int y, T w) {
w += weight(x);
w -= weight(y);
x = leader(x), y = leader(y);
if (x == y) return false;
if (parents[x] > parents[y]) std::swap(x, y), w = -w;
parents[x] += parents[y];
parents[y] = x;
diff_weight[y] = w;
return true;
}
// return weight(y) - weight(x)
T diff(int x, int y) { return weight(y) - weight(x); }
bool same(int x, int y) { return leader(x) == leader(y); }
int size(int x) { return -parents[leader(x)]; }
// 連結成分ごとに (頂点番号 v, diff(leader(v), v)) の配列を返す
std::vector<std::vector<std::pair<int, T>>> groups() {
std::vector<int> leader_buf(n), group_size(n);
for (int i = 0; i < n; i++) {
leader_buf[i] = leader(i);
group_size[leader_buf[i]]++;
}
std::vector<std::vector<std::pair<int, T>>> result(n);
for (int i = 0; i < n; i++) {
result[i].reserve(group_size[i]);
}
for (int i = 0; i < n; i++) {
result[leader_buf[i]].emplace_back(i, weight(i));
}
result.erase(std::remove_if(result.begin(), result.end(), [&](const std::vector<std::pair<int, T>>& v) { return v.empty(); }), result.end());
return result;
}
void init(int n) { parents.assign(n, -1), diff_weight.assign(n, T(0)); } // reset
};