In a network of nodes, each nodeĀ iĀ is directly connected to another nodeĀ jĀ if and only ifĀ graph[i][j] = 1.
Some nodesĀ initialĀ are initially infected by malware.Ā Whenever two nodes are directly connected and at least one of those two nodes is infected by malware, both nodes will be infected by malware.Ā This spread of malware will continue until no more nodes can be infected in this manner.
SupposeĀ M(initial)Ā is the final number of nodes infected with malware in the entire network, after the spread of malware stops.
We willĀ remove one node from the initial list.Ā Return the node that if removed, would minimizeĀ M(initial).Ā If multiple nodes could be removed to minimizeĀ M(initial), return such a node with the smallest index.
Note that if a node was removed from theĀ initialĀ list of infected nodes, it may still be infected later as a result of the malware spread.
Starting with anĀ undirectedĀ graph (the “original graph”) with nodes fromĀ 0Ā toĀ N-1, subdivisions are made to some of the edges.
The graph is given as follows:Ā edges[k]Ā is a list of integer pairsĀ (i, j, n)Ā such thatĀ (i, j)Ā is an edge of the original graph,
andĀ nĀ is the total number ofĀ newĀ nodes on that edge.
Then, the edgeĀ (i, j)Ā is deleted from the original graph,Ā nĀ new nodesĀ (x_1, x_2, ..., x_n)Ā are added to the original graph,
andĀ n+1Ā newĀ edgesĀ (i, x_1), (x_1, x_2), (x_2, x_3), ..., (x_{n-1}, x_n), (x_n, j)Ā are added to the originalĀ graph.
Now, you start at nodeĀ 0Ā from the original graph, and in each move, you travel along oneĀ edge.
Return how many nodes you can reach in at mostĀ MĀ moves.
Example 1:
Input: edge = [[0,1,10],[0,2,1],[1,2,2]], M = 6, N = 3Output: 13Explanation: The nodes that are reachable in the final graph after M = 6 moves are indicated below.
Example 2:
Input: edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], M = 10, N = 4
Output: 23
Note:
0 <= edges.length <= 10000
0 <= edges[i][0] <Ā edges[i][1] < N
There does not exist anyĀ i != jĀ for whichĀ edges[i][0] == edges[j][0]Ā andĀ edges[i][1] == edges[j][1].
The original graphĀ has no parallel edges.
0 <= edges[i][2] <= 10000
0 <= M <= 10^9
1 <= N <= 3000
Solution: Dijkstra Shortest Path
Compute the shortest from 0 to rest of the nodes. Use HP to mark the maximum moves left to reach each node.
HP[u] = a, HP[v] = b, new_nodes[u][v] = c
nodes covered between a<->b = min(c, a + b)
Time complexity: O(ElogE)
Space complexity: O(E)
C++
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
// Author: Huahua
// Running time: 88 ms
classSolution{
public:
intreachableNodes(vector<vector<int>>& edges, int M, int N) {
unordered_map<int, unordered_map<int, int>> g;
for(constauto& e : edges)
g[e[0]][e[1]] = g[e[1]][e[0]] = e[2];
priority_queue<pair<int,int>>q;// {hp, node}, sort by HP desc
unordered_map<int,int>HP;// node -> max HP left
q.push({M,0});
while(!q.empty()){
inthp=q.top().first;
intcur=q.top().second;
q.pop();
if(HP.count(cur))continue;
HP[cur]=hp;
for(constauto& pair : g[cur]) {
int nxt = pair.first;
intnxt_hp=hp-pair.second-1;
if(HP.count(nxt)||nxt_hp<0)continue;
q.push({nxt_hp,nxt});
}
}
intans=HP.size();// Original nodes covered.
for(constauto& e : edges) {
int uv = HP.count(e[0]) ? HP[e[0]] : 0;
intvu=HP.count(e[1])?HP[e[1]]:0;
ans+=min(e[2],uv+vu);
}
returnans;
}
};
Optimized Dijkstra (replace hashmap with vector)
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
// Author: Huahua
// Running time: 56 ms (beats 88%)
classSolution{
public:
intreachableNodes(vector<vector<int>>& edges, int M, int N) {
Given an undirectedĀ graph, returnĀ trueĀ if and only if it is bipartite.
Recall that a graph isĀ bipartiteĀ if we can split it’s set of nodes into two independentĀ subsets A and B such that every edge in the graph has one node in A and another node in B.
The graph is given in the following form:Ā graph[i]Ā is a list of indexesĀ jĀ for which the edge between nodesĀ iĀ andĀ jĀ exists.Ā Each node is an integer betweenĀ 0Ā andĀ graph.length - 1.Ā There are no self edges or parallel edges:Ā graph[i]Ā does not containĀ i, and it doesn’t contain any element twice.
Example 1:Input: [[1,3], [0,2], [1,3], [0,2]]
Output: true
Explanation:
The graph looks like this:
0----1
| |
| |
3----2
We can divide the vertices into two groups: {0, 2} and {1, 3}.
Example 2:Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
Output: false
Explanation:
The graph looks like this:
0----1
| \ |
| \ |
3----2
We cannot find a way to divide the set of nodes into two independent subsets.
Note:
graphĀ will have length in rangeĀ [1, 100].
graph[i]Ā will contain integers in rangeĀ [0, graph.length - 1].
graph[i]Ā will not containĀ iĀ or duplicate values.
The graph is undirected: if any elementĀ jĀ is inĀ graph[i], thenĀ iĀ will be inĀ graph[j].
Solution: Graph Coloring
For each node
If has not been colored, color it to RED(1).
Color its neighbors with a different color RED(1) to BLUE(-1) or BLUE(-1) to RED(-1).
If we can finish the coloring then the graph is bipartite. All red nodes on the left no connections between them and all blues nodes on the right, again no connections between them. red and blue nodes are neighbors.
Time complexity: O(V+E)
Space complexity: O(V)
C++ / DFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Author: Huahua
// Running time: 12 ms
classSolution{
public:
boolisBipartite(vector<vector<int>>& graph) {
const int n = graph.size();
vector<int>colors(n);
for(inti=0;i<n;++i)
if(!colors[i]&& !coloring(graph, colors, 1, i))
return false;
returntrue;
}
private:
boolcoloring(constvector<vector<int>>& graph, vector<int>& colors, int color, int node) {