I'm posting my code for a LeetCode problem copied here. If you would like to review, please do so. Thank you for your time!

Problem

In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j] = 1.

Some nodes initial are initially infected by malware. Whenever two nodes are directly connected and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner.

Suppose M(initial) is the final number of nodes infected with malware in the entire network, after the spread of malware stops.

We will remove one node from the initial list. Return the node that if removed, would minimize M(initial). If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.

Note that if a node was removed from the initial list of infected nodes, it may still be infected later as a result of the malware spread.

Inputs

[[1,1,0],[1,1,0],[0,0,1]]
[0,1]

[[1,0,0,0],[0,1,0,0],[0,0,1,1],[0,0,1,1]]
[3,1]

[[1,0,0],[0,1,0],[0,0,1]]
[0,2]

[[1,1,1],[1,1,1],[1,1,1]]
[1,2]

0

3

0

1

Code

#include <vector>
#include <algorithm>

struct Solution {
std::vector<int> parents;
inline int minMalwareSpread(const std::vector<std::vector<int>> &graph, const std::vector<int> &initial) {
size_t length = graph.size();

for (size_t index = 0; index < length; index++) {
parents.push_back(index);
}

for (size_t row = 0; row < length; row++) {
for (size_t col = row + 1; col < length; col++) {
if (graph[row][col]) {
union_uf(row, col);
}
}
}

std::vector<int> areas(length, 0);
std::vector<int> malwares(length, 0);

for (size_t area = 0; area < length; area++) {
areas[find(area)]++;
}

for (size_t init : initial) {
malwares[find(init)]++;
}

for (int init : initial) {
}

}

private:
const inline size_t find(const size_t x) {
if (x != parents[x]) {
parents[x] = find(parents[x]);
}

return parents[x];
}

const inline void union_uf(const size_t x, const size_t y) {
parents[find(x)] = find(y);
}
};

Algorithm

• We'd first map the graph using Disjoint Set Union (DSU).
• We'd count all areas as well as all infected sets.
• If there'd be only one malware set, we would return the largest. Otherwise, we'd return the set with lowest index using the last loop.