Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WeightedUnionFind -> PotentializedUnionFind #347

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
#include <utility>
#include <vector>

// CUT begin
// Weighted UnionFind
template <class S> struct WeightedUnionFind {
// Potentialized UnionFind (Weighted UnionFind)
template <class S> struct PotentializedUnionFind {
std::vector<int> par, sz;
std::vector<S> pot;
WeightedUnionFind(int N = 0) : par(N), sz(N, 1), pot(N) {
PotentializedUnionFind(int N = 0) : par(N), sz(N, 1), pot(N) {
std::iota(par.begin(), par.end(), 0);
}
int find(int x) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Weighted UnionFind (重み付き UnionFind)
documentation_of: ./weighted_unionfind.hpp
title: Potentialized UnionFind (重み付き UnionFind)
documentation_of: ./potentialized_unionfind.hpp
---

2個の要素間の重みづけが可能な UnionFind.
Expand All @@ -10,7 +10,7 @@ documentation_of: ./weighted_unionfind.hpp
ポテンシャルが(ふつうの)整数の場合.

```cpp
WeightedUnionFind<int> uf(N);
PotentializedUnionFind<int> uf(N);
uf.unite(s, t, diff); // f[t] = f[s] + diff を要請.これまでの要請と矛盾すれば false を返す.

auto x = uf.diff(s, t); // f[t] - f[s] (として考えられる値の一つ)を出力.
Expand All @@ -19,9 +19,10 @@ auto x = uf.diff(s, t); // f[t] - f[s] (として考えられる値の一つ
ポテンシャルが $\mathbb{F}_{2}$ 上のベクトルの場合.

```cpp
WeightedUnionFind<Nimber> uf(N);
PotentializedUnionFind<Nimber> uf(N);
```

## 問題例

- [No.1420 国勢調査 (Easy) - yukicoder](https://yukicoder.me/problems/no/1420) $\mathbb{F}_2$ 上のベクトル.
- [AtCoder Beginner Contest 373 D - Hidden Weights](https://atcoder.jp/contests/abc373/tasks/abc373_d)
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "../weighted_unionfind.hpp"
#include "../potentialized_unionfind.hpp"
#include <iostream>
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_B&lang=jp"
using namespace std;

int main() {
int N, Q, x, y, z;
cin >> N >> Q;
WeightedUnionFind<int> uf(N);
PotentializedUnionFind<int> uf(N);
for (int i = 0; i < Q; i++) {
int c;
cin >> c;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#define PROBLEM "https://yukicoder.me/problems/no/1420"
#define ERROR 1 // Check only whether the answer is -1 or not
#include "../../number/nimber.hpp"
#include "../weighted_unionfind.hpp"
#include "../potentialized_unionfind.hpp"
#include <iostream>
using namespace std;

Expand All @@ -10,7 +10,7 @@ int main() {
int N, M;
cin >> N >> M;

WeightedUnionFind<Nimber> uf(N);
PotentializedUnionFind<Nimber> uf(N);
while (M--) {
int a, b, y;
cin >> a >> b >> y;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=3142"
#include "../weighted_unionfind.hpp"
#include "../potentialized_unionfind.hpp"
#include <algorithm>
#include <iostream>
#include <vector>
Expand All @@ -8,7 +8,7 @@ using namespace std;
int N;
vector<vector<int>> to;
vector<int> A, B;
WeightedUnionFind<long long> uf;
PotentializedUnionFind<long long> uf;

long long dfs(int now, int prv) {
long long acc = B[now] - A[now];
Expand All @@ -26,7 +26,7 @@ int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);

cin >> N;
uf = WeightedUnionFind<long long>(N);
uf = PotentializedUnionFind<long long>(N);
to.resize(N);

for (int e = 0; e < N - 1; e++) {
Expand Down
Loading