/
sweep-restore.hpp
51 lines (45 loc) · 1.09 KB
/
sweep-restore.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once
template <typename T>
struct Sweep {
using P = pair<T, unordered_set<int>>;
vector<P> basis;
Sweep() {}
Sweep(const vector<T>& v) {
for (int i = 0; i < (int)v.size(); i++) add(v[i], i);
}
// x を id と共に追加
void add(T x, int id) {
P v{x, {id}};
for (P& b : basis) {
if (v.first > (v.first ^ b.first)) apply(v, b);
}
if (v.first != T{}) basis.push_back(v);
}
// pair(x を復元できるか?, {復元した時の ID の集合})
pair<bool, vector<int>> restore(T x) {
P v{x, {}};
for (P& b : basis) {
if (v.first > (v.first ^ b.first)) apply(v, b);
}
if (v.first != T{}) return {false, {}};
vector<int> res;
for (auto& n : v.second) res.push_back(n);
sort(begin(res), end(res));
return {true, res};
}
private:
void apply(P& p, const P& o) {
p.first ^= o.first;
for (auto& x : o.second) apply(p.second, x);
}
void apply(unordered_set<int>& s, int x) {
if (s.count(x)) {
s.erase(x);
} else {
s.insert(x);
}
}
};
/**
* @brief 掃き出し法(復元付き)
*/