Open azl397985856 opened 8 months ago
class UnionFind {
public:
UnionFind(int len) {
parent.resize(len, -1);
}
int findRootParent(int p) {
if (parent[p] == -1 || p == parent[p]) return p;
return findRootParent(parent[p]);
}
void unionConn(int p1, int p2) {
int p1Root = findRootParent(p1);
int p2Root = findRootParent(p2);
if (p1Root != p2Root) {
parent[p1Root] = p2Root;
}
}
private:
vector<int> parent;
};
class Solution {
public:
static bool cmp(vector<int>& l, vector<int>& r) {
return l[1] > r[1] || (l[1] == r[1] && l[0] < r[0]);
}
int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
int rowLen = graph.size();
if (rowLen == 0) {
return 0;
}
int colLen = graph[0].size();
if (colLen == 0) {
return 0;
}
UnionFind uf(rowLen);
for (int row = 0; row < rowLen; ++row) {
for (int col = 0; col < colLen; ++col) {
if (graph[row][col] == 1) {
uf.unionConn(row, col);
}
}
}
unordered_map<int, int> rootNum;
for (int i = 0; i < rowLen; ++i) {
int rootP = uf.findRootParent(i);
++rootNum[rootP];
}
vector<int> color(rowLen);
vector<int> groupNum(rowLen, -1);
for (int i = 0; i < rowLen; ++i) {
int rootP = uf.findRootParent(i);
color[i] = rootP;
groupNum[i] = rootNum[rootP];
}
map<int, int> nodeNumPerColor;
for (int i = 0; i < initial.size(); ++i) {
++nodeNumPerColor[color[initial[i]]];
}
vector<vector<int>> ans;
for (int num: initial) {
if (nodeNumPerColor[color[num]] == 1) {
ans.push_back({num, groupNum[num]});
continue;
}
ans.push_back({num, 0});
}
sort(ans.begin(), ans.end(), cmp);
if (!ans.empty()) {
return ans.at(0).at(0);
}
return 0;
}
};
/**
@return {number} */ var minMalwareSpread = function (graph, initial) { let map = new Map(); let N = graph.length; for (let i = 0; i < graph.length; i++) { for (let j = i + 1; j < graph.length; j++) { if (graph[i][j] === 1) { let connects = map.get(i) || []; connects.push(j); map.set(i, connects);
connects = map.get(j) || [];
connects.push(i);
map.set(j, connects);
}
}
}
let result = Infinity; let ansNode; initial.sort((a, b) => a - b); for (let i = 0; i < initial.length; i++) { let val = initial.splice(i, 1)[0]; let ans = count(new Set(initial), initial.concat([]), val); if (ans < result) { result = ans; ansNode = val; } initial.splice(i, 0, val); } return ansNode;
function count(visited, stack, val) {
while (stack.length > 0) {
let node = stack.shift();
let next = map.get(node);
if (!next) continue;
for (let nn of next) {
//与减少恶意软件传播I唯一的区别就是这个条件nn!=val
若没这个条件就是I
的答案了
if (!visited.has(nn) && nn != val) {
visited.add(nn);
stack.push(nn);
}
}
}
return visited.size;
}
};
from collections import defaultdict
class UnionFind: def init(self, n): self.root = [i for i in range(n)] self.size = [1]*n self.part = n
def find(self, x):
if x != self.root[x]:
# 在查询的时候合并到顺带直接根节点
root_x = self.find(self.root[x])
self.root[x] = root_x
return root_x
return x
def union(self, x, y):
root_x = self.find(x)
root_y = self.find(y)
if root_x == root_y:
return
if self.size[root_x] >= self.size[root_y]:
root_x, root_y = root_y, root_x
self.root[root_x] = root_y
self.size[root_y] += self.size[root_x]
# 将非根节点的秩赋0
self.size[root_x] = 0
self.part -= 1
return
def is_connected(self, x, y):
return self.find(x) == self.find(y)
def get_root_part(self):
# 获取每个根节点对应的组
part = defaultdict(list)
n = len(self.root)
for i in range(n):
part[self.find(i)].append(i)
return part
def get_root_size(self):
# 获取每个根节点对应的组大小
size = defaultdict(int)
n = len(self.root)
for i in range(n):
size[self.find(i)] = self.size[self.find(i)]
return size
class Solution: def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int: n = len(graph) uf = UnionFind(n) for i in range(n): for j in range(i+1, n): if graph[i][j]: uf.union(i, j) add = -1 ans = -1 virus = set(initial) part = uf.get_root_part() for k in part: cur = 0 node = -1 for i in part[k]: if i in virus: cur += 1 node = i if cur == 1: if len(part[k]) > add: add = len(part[k]) ans = node elif len(part[k]) == add and node < ans: ans = node return ans if ans != -1 else min(initial)
924. 尽量减少恶意软件的传播
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/minimize-malware-spread
前置知识
暂无
题目描述