1
0
Fork 0

feat(number/basis): better version

This commit is contained in:
arielherself 2025-01-18 20:24:23 +08:00
parent d2ae7650a0
commit 2bc1fd0684
Signed by: arielherself
SSH Key Fingerprint: SHA256:AK3cyo9tFsp7Mox7K0sYphleC8hReXhnRKxwuDT5LBc
2 changed files with 36 additions and 2 deletions

34
number/basis-ng.cc Normal file
View File

@ -0,0 +1,34 @@
template <typename T>
struct basis {
array<T, sizeof(T) * 8> p;
int cnt = 0;
basis(const vector<T>& v) {
for (auto&& x : v) {
insert(x);
}
}
bool insert(T x) {
for (size_t i = sizeof(T) * 8 - 1; ~i; --i) {
if (not (x >> i)) continue;
if (not p[i]) {
p[i] = x;
cnt += 1;
return true;
}
x ^= p[i];
}
return false;
}
vector<T> value(void) const {
vector<T> res;
for (size_t i = sizeof(T) * 8 - 1; ~i; --i) {
if (not p[i]) continue;
res.emplace_back(p[i]);
}
return res;
}
};

View File

@ -6,12 +6,12 @@ public:
iota(c.begin(), c.end(), 0);
sz.assign(n, 1);
}
size_t query(size_t i) {
if (c[i] != i) c[i] = query(c[i]);
return c[i];
}
void merge(size_t i, size_t j) {
if (connected(i, j)) return;
sz[query(j)] += sz[query(i)];