Problems
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.
Examples
Example 1:
graph LR A(0) B(1) C(2) D(3) E(4) F(5) A -->|❌| B B -->|❌| D C --> D A ~~~ E --> A E -->|❌| F %% Corrected edges (after reversal) B -.->|✓| A D -.->|✓| B F -.->|✓| E linkStyle 0 stroke:#ff0000,stroke-width:3px linkStyle 1 stroke:#ff0000,stroke-width:3px linkStyle 2 stroke:#00aa00,stroke-width:2px linkStyle 4 stroke:#00aa00,stroke-width:2px linkStyle 5 stroke:#ff0000,stroke-width:3px linkStyle 6 stroke:#00aa00,stroke-width:2px linkStyle 7 stroke:#00aa00,stroke-width:2px linkStyle 8 stroke:#00aa00,stroke-width:2px,stroke-dasharray: 5 5
|
|
Example 2:
graph LR A(0) B(1) C(2) D(3) E(4) A ~~~ B --> A B -->|❌| C D --> C D -->|❌| E %% Corrected edges (after reversal) C -.->|✓| B E -.->|✓| D linkStyle 2 stroke:#ff0000,stroke-width:3px linkStyle 4 stroke:#ff0000,stroke-width:3px linkStyle 1 stroke:#00aa00,stroke-width:2px linkStyle 3 stroke:#00aa00,stroke-width:2px linkStyle 5 stroke:#00aa00,stroke-width:2px,stroke-dasharray: 5 5 linkStyle 6 stroke:#00aa00,stroke-width:2px,stroke-dasharray: 5 5
|
|
Example 3:
|
|
Solution
Method 1 – BFS with Edge Direction Tracking
Intuition
First of all, the graph cannot have cycles, as guaranteed by the problem statement (the network forms a tree).
We start from node 0 and check if all its neighbors can reach 0. If not, we reorient the necessary edges.
For example, here we see that node 1
cannot reach 0, so we fix it.
graph TB subgraph S1[" "] A(0) B(1) C(2) D(3) E(4) F(5) A(0) A --> B:::red A ~~~ E --> A B --> D C --> D E --> F end subgraph S2[" "] A2(0) B2(1) C2(2) D2(3) E2(4) F2(5) A2(0) A2 -->|❌| B2:::green A2 ~~~ E2 --> A2 B2 --> D2 C2 --> D2 E2 --> F2 B2 -.->|✓| A2 end S1 --> S2 classDef red fill:#FF0000,stroke:#000,stroke-width:1px,color:#fff; classDef green fill:#3CB371,stroke:#000,stroke-width:1px,color:#fff;
But node 4
can already reach 0
.
graph LR A(0) B(1) C(2) D(3) E(4):::green F(5) A(0) A --> B A ~~~ E --> A B --> D C --> D E --> F classDef red fill:#FF0000,stroke:#000,stroke-width:1px,color:#fff; classDef green fill:#3CB371,stroke:#000,stroke-width:1px,color:#fff;
Now we check if 4
’s neighbor can reach it. If not, we fix it.
graph TB subgraph S1[" "] A(0) B(1) C(2) D(3) E(4) F(5):::red A(0) A -->|❌| B A ~~~ E --> A B --> D C --> D E --> F B -.->|✓| A end subgraph S2[" "] A2(0) B2(1) C2(2) D2(3) E2(4) F2(5):::green A2(0) A2 -->|❌| B2 A2 ~~~ E2 --> A2 B2 --> D2 C2 --> D2 E2 -->|❌| F2 B2 -.->|✓| A2 F2 -.->|✓| E2 end S1 --> S2 classDef red fill:#FF0000,stroke:#000,stroke-width:1px,color:#fff; classDef green fill:#3CB371,stroke:#000,stroke-width:1px,color:#fff;
Now, check if 1
’s neighbor can reach it. If not, we fix it.
graph TB subgraph S1[" "] A(0) B(1) C(2) D(3):::red E(4) F(5) A(0) A -->|❌| B A ~~~ E --> A B --> D C --> D E --> F B -.->|✓| A end subgraph S2[" "] A2(0) B2(1) C2(2) D2(3):::green E2(4) F2(5) A2(0) A2 -->|❌| B2 A2 ~~~ E2 --> A2 B2 -->|❌| D2 C2 --> D2 E2 -->|❌| F2 B2 -.->|✓| A2 F2 -.->|✓| E2 D2 -.->|✓| B2 end S1 --> S2 classDef red fill:#FF0000,stroke:#000,stroke-width:1px,color:#fff; classDef green fill:#3CB371,stroke:#000,stroke-width:1px,color:#fff;
Can 3’s neighbor reach it? Yes. Can 2’s neighbor reach it? No neighbors remaining.
Now, the graph is directed, but we use one trick: we mark the graph as undirected for traversal, but remember the original direction of each edge. This allows us to count how many edges need to be reversed as we perform a BFS from city 0.
To ensure all cities can reach city 0, we need to reorient the minimum number of roads. By treating the graph as undirected for traversal but remembering the original direction of each edge, we can count how many edges need to be reversed as we perform a BFS from city 0.
Approach
- Build an undirected graph, but for each edge, record whether it was originally directed away from the current node.
- Start BFS from city 0, marking nodes as visited.
- For each neighbor, if the edge is directed away from the current node (i.e., needs to be reversed), increment the answer.
- Continue until all nodes are visited.
Code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(N)
whereN
is the number of cities (since the graph is a tree withN-1
edges). - 🧺 Space complexity:
O(N)
for the graph and visited structures.