-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path924.java
149 lines (134 loc) · 4.34 KB
/
924.java
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public int minMalwareSpread(int[][] graph, int[] initial) {
int n = graph.length;
int colors[] = new int[n];
int c = 1;
for (int i : initial) {
colors[i] = c++;
}
int res = n, count = 0;
for (int i : initial) {
int cnt = dfs(graph, colors, i, colors[i]);
if (cnt > count) {
res = i;
count = cnt;
} else if (cnt == count) {
res = Math.min(res,i);
}
}
return res;
}
int dfs(int[][] graph, int colors[], int s, int c) {
int cnt = 1;
colors[s] = c;
for (int i = 0; i < graph.length; i++) {
if (graph[s][i] == 1) {
if (colors[i] == 0) {
int ret_cnt = dfs(graph, colors, i, c);
if (ret_cnt == 0) {
return 0;
} else {
cnt += ret_cnt;
}
} else if (colors[i] != c){
return 0;
}
}
}
return cnt;
}
public int minMalwareSpread1(int[][] graph, int[] initial) {
int n = graph.length;
int parents[] = new int[n];
int counts[] = new int[n];
int infects[] = new int[n];
for (int i = 0; i < n; i++) {
parents[i] = i;
}
for(int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (graph[i][j] == 1) {
union(parents, i, j);
}
}
}
for (int i = 0; i < n; i++) {
counts[find(parents, i)]++;
}
for (int i : initial) {
infects[find(parents, i)]++;
}
int res = initial[0];
boolean found = false;
for (int i : initial) {
if (infects[find(parents, i)] == 1) {
if (!found) {
res = i;
} else if (counts[find(parents, i)] >= counts[find(parents, res)]) {
res = Math.min(res, i);
}
found = true;
}
if (!found) {
res = Math.min(res, i);
}
}
return res;
}
int find(int parents[], int n) {
if (parents[n] != n) {
parents[n] = find(parents, parents[n]);
}
return parents[n];
}
void union(int parents[], int i, int j) {
int pi = find(parents, i);
int pj = find(parents, j);
parents[pi] = pj;
}
}
__________________________________________________________________________________________________
sample 60856 kb submission
class Solution {
public int minMalwareSpread(int[][] graph, int[] initial) {
Set<Integer> set = count(graph, initial, -1);
Arrays.sort(initial);
int minIdx = initial[0];
int min = set.size();
for(int i : initial){
set = count(graph, initial, i);
if(set.size() < min){
minIdx = i;
min = set.size();
}
}
return minIdx;
}
private Set<Integer> count(int[][] graph, int[] initial, int skip){
Set<Integer> visited = new HashSet<>();
Set<Integer> res = new HashSet<>();
for(int i : initial){
if(i != skip){
visited = new HashSet<>();
visited.add(i);
Queue<Integer> queue = new LinkedList<>();
queue.add(i);
while(!queue.isEmpty()){
int current = queue.poll();
int[] tmp = graph[current];
for(int j = 0; j<tmp.length; j++){
if(tmp[j] ==1 && !visited.contains(j)){
queue.add(j);
visited.add(j);
res.add(j);
}
}
}
}
}
return res;
}
}
__________________________________________________________________________________________________