-
Notifications
You must be signed in to change notification settings - Fork 3
/
2685.cpp
64 lines (64 loc) · 1.69 KB
/
2685.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
class DisjointSet {
private:
vector<int> parent;
vector<int> rank;
vector<int> nodes;
public:
DisjointSet(int size) {
parent.resize(size, 0);
for (int i = 0; i < size; ++i) parent[i] = i;
rank.resize(size, 0);
nodes.resize(size, 1);
}
int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
void join(int x, int y) {
int pX = find(x);
int pY = find(y);
if (pX == pY) return;
if (rank[pX] > rank[pY]) {
parent[pY] = pX;
nodes[pX] += nodes[pY];
}
else if (rank[pX] < rank[pY]) {
parent[pX] = pY;
nodes[pY] += nodes[pX];
}
else {
parent[pY] = pX;
nodes[pX] += nodes[pY];
rank[pX]++;
}
}
int getNodes(int x) {
int parent = find(x);
return nodes[parent];
}
};
class Solution {
public:
int countCompleteComponents(int n, vector<vector<int>>& edges) {
DisjointSet* disjointSet = new DisjointSet(n);
for (auto& edge : edges) {
disjointSet->join(edge[0], edge[1]);
}
unordered_map<int, int> parent2edgeCnt;
for (auto& edge : edges) {
int parent = disjointSet->find(edge[0]);
parent2edgeCnt[parent]++;
}
int res = 0;
for (auto& [parent, cnt] : parent2edgeCnt) {
int nodes = disjointSet->getNodes(parent);
if ((nodes * (nodes - 1)) / 2 == cnt) res++;
}
for (int i = 0; i < n; ++i) {
if (disjointSet->getNodes(i) == 1) res++;
}
return res;
}
};