-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3493.properties-graph.cpp
More file actions
57 lines (56 loc) · 1.75 KB
/
3493.properties-graph.cpp
File metadata and controls
57 lines (56 loc) · 1.75 KB
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
#
# @lc app=leetcode id=3493 lang=cpp
#
# [3493] Properties Graph
#
# @lc code=start
class Solution {
public:
int numberOfComponents(vector<vector<int>>& properties, int k) {
int n = properties.size();
// convert each row to a bitset of presence of numbers 1..100
vector<bitset<101>> sets(n);
for (int i = 0; i < n; ++i) {
// note: values are between 1 and 100 inclusive
for (int val : properties[i]) {
sets[i].set(val);
}
}
// DSU initialization
vector<int> parent(n);
vector<int> rank(n,0);
for(int i=0;i<n;++i) parent[i]=i;
function<int(int)> find = [&](int x)->int{
if(parent[x]!=x) parent[x]=find(parent[x]);
return parent[x];
};
auto unite = [&](int x,int y){
int rx=find(x), ry=find(y);
if(rx==ry) return;
if(rank[rx]<rank[ry]) parent[rx]=ry;
else if(rank[rx]>rank[ry]) parent[ry]=rx;
else{ parent[ry]=rx; rank[rx]++; }
};
// iterate over all pairs
for(int i=0;i<n;++i){
for(int j=i+1;j<n;++j){
// compute intersection size
int cnt=0;
// iterate over possible numbers from 1 to 100
// we can use bitwise AND on bitsets and count bits
bitset<101> common = sets[i] & sets[j];
cnt = common.count(); // number of set bits
if(cnt>=k){
unite(i,j);
}
}
}
// count distinct roots
unordered_set<int> roots;
for(int i=0;i<n;++i){
roots.insert(find(i));
}
return roots.size();
}};# @lc code=end