// For each node u in initial, dfs to find // 'seen': all nodes not in initial that it can reach. ArrayList<Integer>[] infectedBy = newArrayList[N]; for (inti=0; i < N; ++i) infectedBy[i] = newArrayList();
for (int u: initial) { Set<Integer> seen = newHashSet(); dfs(graph, clean, u, seen); for (int v: seen) infectedBy[v].add(u); }
// For each node u in initial, for every v not in initial // that is uniquely infected by u, add 1 to the contribution for u. int[] contribution = newint[N]; for (intv=0; v < N; ++v) if (infectedBy[v].size() == 1) contribution[infectedBy[v].get(0)]++;
// Take the best answer. Arrays.sort(initial); intans= initial[0], ansSize = -1; for (int u: initial) { intscore= contribution[u]; if (score > ansSize || score == ansSize && u < ans) { ans = u; ansSize = score; } } return ans; }
publicvoiddfs(int[][] graph, int[] clean, int u, Set<Integer> seen) { for (intv=0; v < graph.length; ++v) if (graph[u][v] == 1 && clean[v] == 1 && !seen.contains(v)) { seen.add(v); dfs(graph, clean, v, seen); } } }
classSolution(object): defminMalwareSpread(self, graph, initial): N = len(graph) clean = set(range(N)) - set(initial) defdfs(u, seen): for v, adj inenumerate(graph[u]): if adj and v in clean and v notin seen: seen.add(v) dfs(v, seen)
# For each node u in initial, dfs to find # 'seen': all nodes not in initial that it can reach. infected_by = {v: [] for v in clean} for u in initial: seen = set() dfs(u, seen)
# For each node v that was seen, u infects v. for v in seen: infected_by[v].append(u)
# For each node u in initial, for every v not in initial # that is uniquely infected by u, add 1 to the contribution for u. contribution = collections.Counter() for v, neighbors in infected_by.iteritems(): iflen(neighbors) == 1: contribution[neighbors[0]] += 1
# Take the best answer. best = (-1, min(initial)) for u, score in contribution.iteritems(): if score > best[0] or score == best[0] and u < best[1]: best = score, u return best[1]
// clean[u] == 1 if its a node in the graph not in initial. int[] clean = newint[N]; Arrays.fill(clean, 1); for (int x: initial) clean[x] = 0;
for (intu=0; u < N; ++u) if (clean[u] == 1) for (intv=0; v < N; ++v) if (clean[v] == 1) if (graph[u][v] == 1) dsu.union(u, v);
// dsu now represents the components of the graph without // any nodes from initial. Let's call this graph G. int[] count = newint[N]; Map<Integer, Set<Integer>> nodeToCompo = newHashMap(); for (int u: initial) { Set<Integer> components = newHashSet(); for (intv=0; v < N; ++v) if (clean[v] == 1) { if (graph[u][v] == 1) components.add(dsu.find(v)); }
nodeToCompo.put(u, components); for (int c: components) count[c]++; }
// For each node u in initial, nodeToCompo.get(u) // now has every component from G that u neighbors.
intans= -1, ansSize = -1; for (int u: nodeToCompo.keySet()) { Set<Integer> components = nodeToCompo.get(u); intscore=0; for (int c: components) if (count[c] == 1) // uniquely infected score += dsu.size(c);
if (score > ansSize || score == ansSize && u < ans) { ansSize = score; ans = u; } }
return ans; } }
classDSU { int[] p, sz;
DSU(int N) { p = newint[N]; for (intx=0; x < N; ++x) p[x] = x;
defunion(self, x, y): xr = self.find(x) yr = self.find(y) self.p[xr] = yr self.sz[yr] += self.sz[xr]
defsize(self, x): return self.sz[self.find(x)]
classSolution(object): defminMalwareSpread(self, graph, initial): N = len(graph) initial_set = set(initial) clean = [x for x inrange(N) if x notin initial_set]
# clean[u] == 1 if its a node in the graph not in initial. dsu = DSU(N) for u in clean: for v in clean: if graph[u][v]: dsu.union(u, v)
# dsu now represents the components of the graph without # any nodes from initial. Let's call this graph G. count = collections.Counter() node_to_compo = {} for u in initial: components = set() for v in clean: if graph[u][v]: components.add(dsu.find(v)) node_to_compo[u] = components
for c in components: count[c] += 1
# For each node u in initial, nodeToCompo.get(u) # now has every component from G that u neighbors.
best = (-1, None) # score, node for u, components in node_to_compo.iteritems(): score = 0 for c in components: if count[c] == 1: #uniquely infected score += dsu.size(c) if score > best[0] or score == best[0] and u < best[1]: best = (score, u)