-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path924.cpp
More file actions
98 lines (95 loc) · 2.82 KB
/
924.cpp
File metadata and controls
98 lines (95 loc) · 2.82 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class DisjointSet {
private:
vector<int> parent;
vector<int> rank;
vector<int> size;
public:
DisjointSet(int n) {
parent.resize(n, 0);
rank.resize(n, 0);
size.resize(n, 1);
for (int i = 0; i < n; ++i) parent[i] = i;
}
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;
size[pX] += size[pY];
}
else if (rank[pX] < rank[pY]) {
parent[pX] = pY;
size[pY] += size[pX];
}
else {
rank[pX]++;
parent[pY] = pX;
size[pX] += size[pY];
}
}
int getSize(int x) {
return size[find(x)];
}
};
class Solution {
public:
int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
int n = graph.size();
DisjointSet* disjointSet = new DisjointSet(n);
vector<vector<int>> adjacency(n);
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (graph[i][j]) {
adjacency[i].push_back(j);
adjacency[j].push_back(i);
}
}
}
vector<bool> visited(n, false);
for (auto& initNode : initial) {
if (visited[initNode]) continue;
queue<int> q;
q.push(initNode);
visited[initNode] = true;
while (!q.empty()) {
int node = q.front();
q.pop();
for (auto& neighbor : adjacency[node]) {
if (visited[neighbor]) continue;
q.push(neighbor);
visited[neighbor] = true;
disjointSet->join(node, neighbor);
}
}
}
unordered_map<int, vector<int>> parent2nodes;
for (auto& initNode : initial) {
int parent = disjointSet->find(initNode);
parent2nodes[parent].push_back(parent);
}
// find # of node = 1 with max size
int res = INT_MAX;
int maxSize = -1;
for (auto& element : parent2nodes) {
if (element.second.size() == 1) {
int size = disjointSet->getSize(element.first);
if (size > maxSize) {
maxSize = size;
res = element.second[0];
}
else if (size == maxSize) {
res = min(res, element.second[0]);
}
}
}
if (res != INT_MAX) return res;
return *min_element(initial.begin(), initial.end());
}
};