-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_82_1319. Number of Operations to Make Network Connected.cpp
63 lines (56 loc) · 2 KB
/
Day_82_1319. Number of Operations to Make Network Connected.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
// ##########################################
// solution 1.1 : More optimized solution
// ##########################################
class Solution {
public:
int get( int u , vector<int>& parent ){
if( parent[u] == u ) return u ;
return parent[u] = get( parent[u] , parent );
}
void join( int u , int v , vector<int>& parent ){
u = get( u , parent ) , v = get( v , parent );
if( u != v ) parent[v] = u ;
}
int makeConnected(int n, vector<vector<int>>& connections) {
if ( connections.size() < n - 1 ) return -1;
vector<int>parent(n);
for( int i = 0 ; i < n ; i++ ) parent[i] = i ; // init
int links = n - 1 ;
for( auto & i : connections ) {
if ( get(i[0],parent) != get(i[1],parent) ) {
join(i[0],i[1],parent);
links--;
}
}
return links; // rest of links is the qunatity needed
}
};
// ####################################################
// solution 1 : using DSU and unique components check
// ####################################################
class Solution {
public:
int get( int u , vector<int>& parent ){
if( parent[u] == u ) return u ;
return parent[u] = get( parent[u] , parent );
}
void join( int u , int v , vector<int>& parent ){
u = get( u , parent ) , v = get( v , parent );
if( u != v ) parent[v] = u ;
}
int makeConnected(int n, vector<vector<int>>& connections) {
vector<int>parent(n);
for( int i = 0 ; i < n ; i++ ) parent[i] = i ; // init
int extra = 0 ;
for( auto & i : connections ){
if ( get(i[0],parent) != get(i[1],parent) ) join(i[0],i[1],parent);
else extra++;
}
set<int> unique;
for( int i = 0 ; i < n ; i++ ) {
unique.insert(get((i),parent)) ;
}
int components = unique.size() ;
return ( ( components-1 <= extra ) ? components - 1 : -1 ) ;
}
};