@@ -5,17 +5,17 @@ class Solution {
55public:
66 int minMalwareSpread (vector<vector<int >>& graph, vector<int >& initial) {
77 unordered_set<int > initial_set (initial.cbegin (), initial.cend ());
8- UnionFind union_find (graph. size ()) ;
8+ vector< int > clean ;
99 for (int i = 0 ; i < graph.size (); ++i) {
10- if (initial_set.count (i)) {
11- continue ;
10+ if (! initial_set.count (i)) {
11+ clean. emplace_back (i) ;
1212 }
13- for ( int j = i + 1 ; j < graph. size (); ++j) {
14- if (initial_set. count (j)) {
15- continue ;
16- }
17- if (graph[i][j ] == 1 ) {
18- union_find.union_set (i, j );
13+ }
14+ UnionFind union_find (graph. size ());
15+ for ( int i = 0 ; i < clean. size (); ++i) {
16+ for ( int j = i + 1 ; j < clean. size (); ++j) {
17+ if (graph[clean[i]][clean[j] ] == 1 ) {
18+ union_find.union_set (clean[i], clean[j] );
1919 }
2020 }
2121 }
@@ -26,26 +26,19 @@ class Solution {
2626
2727 unordered_map<int , unordered_set<int >> shared_union;
2828 for (const auto & i: initial) {
29- for (int j = 0 ; j < graph.size (); ++j) {
30- if (initial_set.count (j)) {
31- continue ;
32- }
29+ for (const auto & j : clean) {
3330 if (graph[i][j] == 1 ) {
3431 auto x = union_find.find_set (j);
3532 shared_union[x].emplace (i);
3633 }
3734 }
3835 }
39-
4036 int result = numeric_limits<int >::max ();
4137 int total = numeric_limits<int >::min ();
4238 for (const auto & i: initial) {
4339 unordered_set<int > lookup;
4440 int curr = 0 ;
45- for (int j = 0 ; j < graph.size (); ++j) {
46- if (initial_set.count (j)) {
47- continue ;
48- }
41+ for (const auto & j : clean) {
4942 if (graph[i][j] == 1 ) {
5043 auto x = union_find.find_set (j);
5144 if (shared_union[x].size () == 1 &&
0 commit comments