-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.cpp
62 lines (60 loc) · 1.62 KB
/
Solution.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
class Solution {
public:
vector<int> p;
int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
int n = graph.size();
vector<int> size(n, 1);
for (int i = 0; i < n; ++i) p.push_back(i);
vector<bool> clean(n, true);
for (int i : initial) clean[i] = false;
for (int i = 0; i < n; ++i)
{
if (!clean[i]) continue;
for (int j = i + 1; j < n; ++j)
{
if (!clean[j]) continue;
if (graph[i][j])
{
int pa = find(i), pb = find(j);
if (pa == pb) continue;
p[pa] = pb;
size[pb] += size[pa];
}
}
}
vector<int> cnt(n, 0);
unordered_map<int, unordered_set<int>> mp;
for (int i : initial)
{
unordered_set<int> s;
for (int j = 0; j < n; ++j)
{
if (!clean[j]) continue;
if (graph[i][j]) s.insert(find(j));
}
for (int e : s) ++cnt[e];
mp[i] = s;
}
int mx = -1;
int res = 0;
for (auto item : mp)
{
int i = item.first;
int t = 0;
for (int e : item.second)
{
if (cnt[e] == 1) t += size[e];
}
if (mx < t || (mx == t && i < res))
{
mx = t;
res = i;
}
}
return res;
}
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
};