| 5 | // path compression + rank by union |
| 6 | |
| 7 | class DSU { |
| 8 | int* parent; |
| 9 | int* rank; |
| 10 | |
| 11 | public: |
| 12 | DSU(int n) |
| 13 | { |
| 14 | parent = new int[n]; |
| 15 | rank = new int[n]; |
| 16 | |
| 17 | for (int i = 0; i < n; i++) { |
| 18 | parent[i] = -1; |
| 19 | rank[i] = 1; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | // Find function |
| 24 | int find(int i) |
| 25 | { |
| 26 | if (parent[i] == -1) |
| 27 | return i; |
| 28 | |
| 29 | return parent[i] = find(parent[i]); |
| 30 | } |
| 31 | |
| 32 | // Union function |
| 33 | void unite(int x, int y) |
| 34 | { |
| 35 | int s1 = find(x); |
| 36 | int s2 = find(y); |
| 37 | |
| 38 | if (s1 != s2) { |
| 39 | if (rank[s1] < rank[s2]) { |
| 40 | parent[s1] = s2; |
| 41 | rank[s2] += rank[s1]; |
| 42 | } |
| 43 | else { |
| 44 | parent[s2] = s1; |
| 45 | rank[s1] += rank[s2]; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | class Graph { |
| 52 | vector<vector<int> > edgelist; |
nothing calls this directly
no outgoing calls
no test coverage detected