Medium
,DFS
,BFS
,Graph
1466. Reorder Routes to Make All Paths Lead to the City Zero
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]
= [, ] represents a road from city to city .
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:
Example 2:
Example 3:
Constraints:
n
<= 5 * 104connections.length
== n
- 1connections[i].length
== 2n
- 1Ron ChenFri, Mar 24, 2023
看 Ron 神ㄉ改ㄌ一下資料結構從 Beat 18% 變成 76%ㄌ,好ㄟ 又學習ㄌgpwork4uFri, Mar 24, 2023
想半天最後建兩種graph去做比對…感覺特別傻
MarsgoatFri, Mar 24, 2023
分享一下除了樓上兩位大神的解法,看到的另一種做法,感覺也不錯,beat94%
MarsgoatMar 25, 2023