Open songyy5517 opened 5 months ago
Approach: DFS, convert the graph to a tree
isVisited
: the visit array;ArrayList<Integer>[] adj
: store all adjacent nodes of each node. For each element in connections
, we add the corresponding node value to adj
according to the direction of edge;num
: the number of paths from parent to child (negative edge).adj[idx]
:
num
and perform DFS on this adjacent node.num
.Complexity Analysis
ArrayList<Integer>[]
$O(N)$ ?)class Solution {
int num = 0;
public int minReorder(int n, int[][] connections) {
// 思路:图转换成树,DFS
// 1. 异常处理
if (connections == null)
return 0;
// 2. 有向图转换成无向图
ArrayList<Integer>[] adj = new ArrayList[n];
for (int i = 0; i < n; i++)
adj[i] = new ArrayList();
for (int i = 0; i < connections.length; i++){
adj[connections[i][0]].add(connections[i][1]);
adj[connections[i][1]].add(-connections[i][0]);
}
// 3. DFS
boolean[] isVisited = new boolean[n];
DFS(adj, isVisited, 0);
return num;
}
void DFS(ArrayList<Integer>[] adj, boolean[] isVisited, int index){
// 递归出口
if (isVisited[index])
return ;
// 处理当前节点
isVisited[index] = true;
for (int neighbor : adj[index]){
int neighbor_abs = Math.abs(neighbor);
if (neighbor > 0 && !isVisited[neighbor])
num ++;
DFS(adj, isVisited, neighbor_abs);
}
}
}
2024/6/19 2024/6/21
There are n cities numbered from 0 to n - 1 and n - 1 roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow. Roads are represented by connections where connections[i] = [ai, bi] represents a road from city ai to city bi. This year, there will be a big event in the capital (city 0), and many people want to travel to this city. Your task consists of reorienting some roads such that each city can visit the city 0. Return the minimum number of edges changed. It's guaranteed that each city can reach city 0 after reorder.
Example 1:
Intuition The problem can be converted into finding the number of edges from parent to child with node 0 as the root. Therefore, we can travarse the whole graph from 0 using DFS, and calculate the number of edges from parent to child. To determine the direction of an edge, we need to define a nested arraylist to store all edges of each node, where reverse edges are set to negative (its opposite number). Therefore, the solution include three steps in general: 1. Transform the graph into a tree, that is, convert all the directed edges into undirected edges in the graph; 2. Travarse the graph from node 0, and identify negative paths by comparing the direction of each edge and its original direction; 3. Compute the total number of nagative edges.