-
Notifications
You must be signed in to change notification settings - Fork 16
/
DSU.cpp
executable file
·111 lines (94 loc) · 3.26 KB
/
DSU.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <bits/stdc++.h>
using namespace std;
#define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
#define cout_2d(vec, n, m) for(int i = 0; i < n; i++, cout << "\n") for(int j = 0; j < m && cout << vec[i][j] << " "; j++);
#define fixed(n) fixed << setprecision(n)
#define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
#define fill(vec, value) memset(vec, value, sizeof(vec));
#define mul_mod(a, b, m) (((a % m) * (b % m)) % m)
#define add_mod(a, b, m) (((a % m) + (b % m)) % m)
#define all(vec) vec.begin(), vec.end()
#define rall(vec) vec.rbegin(), vec.rend()
#define sz(x) int(x.size())
#define debug(x) cout << #x << ": " << (x) << "\n";
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define Mod 1'000'000'007
#define OO 2'000'000'000
#define EPS 1e-9
#define PI acos(-1)
template < typename T = int > using Pair = pair < T, T >;
vector < string > RET = {"NO", "YES"};
template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
for (auto &x : v) in >> x;
return in;
}
template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
for (const T &x : v) out << x << ' ';
return out;
}
template < typename T = int, int Base = 1 > struct DSU {
vector < T > parent, Gsize, nxt, tail, pos, roots;
DSU(int MaxNodes){
parent = Gsize = roots = tail = pos = nxt = vector < T > (MaxNodes + Base);
for(int i = Base; i < MaxNodes + Base; i++){
parent[i] = roots[i] = pos[i] = tail[i] = i;
nxt[i] = -1, Gsize[i] = 1;
}
}
T find_leader(int node){
return parent[node] = (parent[node] == node ? node : find_leader(parent[node]));
}
bool is_same_sets(int u, int v){
return find_leader(u) == find_leader(v);
}
void union_sets(int u, int v){
int leader_u = find_leader(u), leader_v = find_leader(v);
if(leader_u == leader_v) return;
// make leader_u is the leader with the larger component
if(Gsize[leader_u] < Gsize[leader_v])
swap(leader_u, leader_v);
int p = pos[leader_v];
Gsize[leader_u] += Gsize[leader_v];
parent[leader_v] = leader_u;
roots[p] = roots.back();
pos[roots[p]] = p;
roots.pop_back();
nxt[tail[leader_u]] = leader_v;
tail[leader_u] = tail[leader_v];
}
void print(){
for(int root = Base; root < sz(roots); root++){
for(int u = roots[root]; ~u ; u = nxt[u])
cout << u << " \n"[!~nxt[u]];
}
}
vector < vector < int > > get_components(){
vector < vector < int > > components;
for(int root = Base; root < sz(roots); root++){
vector < int > component;
for(int u = roots[root]; ~u ; u = nxt[u])
component.push_back(u);
components.push_back(component);
}
return components;
}
int get_size(int u){
return Gsize[find_leader(u)];
}
int get_components_number(){
return sz(roots) - Base;
}
};
void Solve(){
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int t = 1;
//cin >> t;
while(t--)
Solve();
return 0;
}