diff --git a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/README.md b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/README.md index 91dd3a000b1b9..565880a6a3fc4 100644 --- a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/README.md +++ b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/README.md @@ -58,7 +58,13 @@ **方法一:DFS** -将图视为无向图。从编号 0 开始 dfs,如果遇到正向边,则需要累加一次变更。 +题目给定的路线图中有 $n$ 个节点和 $n-1$ 条边,如果我们忽略边的方向,那么这 $n$ 个节点构成了一棵树。而题目需要我们改变某些边的方向,使得每个节点都能到达节点 0ドル$。 + +我们不妨考虑从节点 0ドル$ 出发,到达其他所有节点。方向与题目描述相反,意味着我们在构建图的时候,对于有向边 $[a, b],ドル我们应该视为有向边 $[b, a]$。也即是说,如果要从 $a$ 到 $b,ドル我们需要变更一次方向;如果要从 $b$ 到 $a,ドル则不需要变更方向。 + +接下来,我们只需要从节点 0ドル$ 出发,搜索其他所有节点,过程中,如果遇到需要变更方向的边,则累加一次变更方向的次数。 + +时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是题目中节点的数量。 @@ -69,24 +75,14 @@ ```python class Solution: def minReorder(self, n: int, connections: List[List[int]]) -> int: - def dfs(u): - vis[u] = True - ans = 0 - for v in g[u]: - if not vis[v]: - if (u, v) in s: - ans += 1 - ans += dfs(v) - return ans - - g = defaultdict(list) - s = set() + def dfs(a: int, fa: int) -> int: + return sum(c + dfs(b, a) for b, c in g[a] if b != fa) + + g = [[] for _ in range(n)] for a, b in connections: - g[a].append(b) - g[b].append(a) - s.add((a, b)) - vis = [False] * n - return dfs(0) + g[a].append((b, 1)) + g[b].append((a, 0)) + return dfs(0, -1) ``` ### **Java** @@ -95,28 +91,25 @@ class Solution: ```java class Solution { + private List[] g; + public int minReorder(int n, int[][] connections) { - Map>> g = new HashMap(); - for (int[] e : connections) { - int u = e[0], v = e[1]; - g.computeIfAbsent(u, k -> new ArrayList()).add(new Pair(v, true)); - g.computeIfAbsent(v, k -> new ArrayList()).add(new Pair(u, false)); + g = new List[n]; + Arrays.setAll(g, k -> new ArrayList()); + for (var e : connections) { + int a = e[0], b = e[1]; + g[a].add(new int[] {b, 1}); + g[b].add(new int[] {a, 0}); } - boolean[] vis = new boolean[n]; - return dfs(0, g, vis); + return dfs(0, -1); } - private int dfs(int u, Map>> g, boolean[] vis) { - vis[u] = true; + private int dfs(int a, int fa) { int ans = 0; - for (Pair e : g.getOrDefault(u, Collections.emptyList())) { - int v = e.getKey(); - boolean exist = e.getValue(); - if (!vis[v]) { - if (exist) { - ++ans; - } - ans += dfs(v, g, vis); + for (var e : g[a]) { + int b = e[0], c = e[1]; + if (b != fa) { + ans += c + dfs(b, a); } } return ans; @@ -130,28 +123,22 @@ class Solution { class Solution { public: int minReorder(int n, vector>& connections) { - unordered_map>> g; + vector> g[n]; for (auto& e : connections) { - int u = e[0], v = e[1]; - g[u].push_back({v, true}); - g[v].push_back({u, false}); + int a = e[0], b = e[1]; + g[a].emplace_back(b, 1); + g[b].emplace_back(a, 0); } - vector vis(n); - return dfs(0, g, vis); - } - - int dfs(int u, unordered_map>>& g, vector& vis) { - vis[u] = true; - int ans = 0; - for (auto& p : g[u]) { - int v = p.first; - bool exist = p.second; - if (!vis[v]) { - if (exist) ++ans; - ans += dfs(v, g, vis); + function dfs = [&](int a, int fa) { + int ans = 0; + for (auto& [b, c] : g[a]) { + if (b != fa) { + ans += c + dfs(b, a); + } } - } - return ans; + return ans; + }; + return dfs(0, -1); } }; ``` @@ -160,33 +147,70 @@ public: ```go func minReorder(n int, connections [][]int) int { - type pib struct { - v int - b bool - } - g := map[int][]pib{} + g := make([][][2]int, n) for _, e := range connections { - u, v := e[0], e[1] - g[u] = append(g[u], pib{v, true}) - g[v] = append(g[v], pib{u, false}) + a, b := e[0], e[1] + g[a] = append(g[a], [2]int{b, 1}) + g[b] = append(g[b], [2]int{a, 0}) } - vis := make([]bool, n) - var dfs func(int) int - dfs = func(u int) int { - ans := 0 - vis[u] = true - for _, p := range g[u] { - v, exist := p.v, p.b - if !vis[v] { - if exist { - ans++ - } - ans += dfs(v) + var dfs func(int, int) int + dfs = func(a, fa int) (ans int) { + for _, e := range g[a] { + if b, c := e[0], e[1]; b != fa { + ans += c + dfs(b, a) } } - return ans + return } - return dfs(0) + return dfs(0, -1) +} +``` + +### **TypeScript** + +```ts +function minReorder(n: number, connections: number[][]): number { + const g: [number, number][][] = Array.from({ length: n }, () => []); + for (const [a, b] of connections) { + g[a].push([b, 1]); + g[b].push([a, 0]); + } + const dfs = (a: number, fa: number): number => { + let ans = 0; + for (const [b, c] of g[a]) { + if (b !== fa) { + ans += c + dfs(b, a); + } + } + return ans; + }; + return dfs(0, -1); +} +``` + +### **Rust** + +```rust +impl Solution { + pub fn min_reorder(n: i32, connections: Vec>) -> i32 { + let mut g: Vec> = vec![vec![]; n as usize]; + for e in connections.iter() { + let a = e[0] as usize; + let b = e[1] as usize; + g[a].push((b as i32, 1)); + g[b].push((a as i32, 0)); + } + fn dfs(a: usize, fa: i32, g: &Vec>) -> i32 { + let mut ans = 0; + for &(b, c) in g[a].iter() { + if b != fa { + ans += c + dfs(b as usize, a as i32, g); + } + } + ans + } + dfs(0, -1, &g) + } } ``` diff --git a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/README_EN.md b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/README_EN.md index 5eb70a5bdcaba..6209892840440 100644 --- a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/README_EN.md +++ b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/README_EN.md @@ -51,9 +51,15 @@ ## Solutions -DFS. +**Solution 1: DFS** -Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. +The route map given in the problem has $n$ nodes and $n-1$ edges. If we ignore the direction of the edges, then these $n$ nodes form a tree. The problem requires us to change the direction of some edges so that each node can reach node 0ドル$. + +We might as well consider starting from node 0ドル$ and reaching all other nodes. The direction is opposite to the problem description, which means that when we build the graph, for the directed edge $[a, b],ドル we should regard it as the directed edge $[b, a]$. That is to say, if it is from $a$ to $b,ドル we need to change the direction once; if it is from $b$ to $a,ドル no direction change is needed. + +Next, we only need to start from node 0ドル,ドル search all other nodes, and during the process, if we encounter an edge that needs to change direction, we accumulate the number of direction changes once. + +The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the problem. @@ -62,52 +68,39 @@ Treat the graph as undirected. Start a dfs from the root, if you come across an ```python class Solution: def minReorder(self, n: int, connections: List[List[int]]) -> int: - def dfs(u): - vis[u] = True - ans = 0 - for v in g[u]: - if not vis[v]: - if (u, v) in s: - ans += 1 - ans += dfs(v) - return ans - - g = defaultdict(list) - s = set() + def dfs(a: int, fa: int) -> int: + return sum(c + dfs(b, a) for b, c in g[a] if b != fa) + + g = [[] for _ in range(n)] for a, b in connections: - g[a].append(b) - g[b].append(a) - s.add((a, b)) - vis = [False] * n - return dfs(0) + g[a].append((b, 1)) + g[b].append((a, 0)) + return dfs(0, -1) ``` ### **Java** ```java class Solution { + private List[] g; + public int minReorder(int n, int[][] connections) { - Map>> g = new HashMap(); - for (int[] e : connections) { - int u = e[0], v = e[1]; - g.computeIfAbsent(u, k -> new ArrayList()).add(new Pair(v, true)); - g.computeIfAbsent(v, k -> new ArrayList()).add(new Pair(u, false)); + g = new List[n]; + Arrays.setAll(g, k -> new ArrayList()); + for (var e : connections) { + int a = e[0], b = e[1]; + g[a].add(new int[] {b, 1}); + g[b].add(new int[] {a, 0}); } - boolean[] vis = new boolean[n]; - return dfs(0, g, vis); + return dfs(0, -1); } - private int dfs(int u, Map>> g, boolean[] vis) { - vis[u] = true; + private int dfs(int a, int fa) { int ans = 0; - for (Pair e : g.getOrDefault(u, Collections.emptyList())) { - int v = e.getKey(); - boolean exist = e.getValue(); - if (!vis[v]) { - if (exist) { - ++ans; - } - ans += dfs(v, g, vis); + for (var e : g[a]) { + int b = e[0], c = e[1]; + if (b != fa) { + ans += c + dfs(b, a); } } return ans; @@ -121,28 +114,22 @@ class Solution { class Solution { public: int minReorder(int n, vector>& connections) { - unordered_map>> g; + vector> g[n]; for (auto& e : connections) { - int u = e[0], v = e[1]; - g[u].push_back({v, true}); - g[v].push_back({u, false}); + int a = e[0], b = e[1]; + g[a].emplace_back(b, 1); + g[b].emplace_back(a, 0); } - vector vis(n); - return dfs(0, g, vis); - } - - int dfs(int u, unordered_map>>& g, vector& vis) { - vis[u] = true; - int ans = 0; - for (auto& p : g[u]) { - int v = p.first; - bool exist = p.second; - if (!vis[v]) { - if (exist) ++ans; - ans += dfs(v, g, vis); + function dfs = [&](int a, int fa) { + int ans = 0; + for (auto& [b, c] : g[a]) { + if (b != fa) { + ans += c + dfs(b, a); + } } - } - return ans; + return ans; + }; + return dfs(0, -1); } }; ``` @@ -151,33 +138,70 @@ public: ```go func minReorder(n int, connections [][]int) int { - type pib struct { - v int - b bool - } - g := map[int][]pib{} + g := make([][][2]int, n) for _, e := range connections { - u, v := e[0], e[1] - g[u] = append(g[u], pib{v, true}) - g[v] = append(g[v], pib{u, false}) + a, b := e[0], e[1] + g[a] = append(g[a], [2]int{b, 1}) + g[b] = append(g[b], [2]int{a, 0}) } - vis := make([]bool, n) - var dfs func(int) int - dfs = func(u int) int { - ans := 0 - vis[u] = true - for _, p := range g[u] { - v, exist := p.v, p.b - if !vis[v] { - if exist { - ans++ - } - ans += dfs(v) + var dfs func(int, int) int + dfs = func(a, fa int) (ans int) { + for _, e := range g[a] { + if b, c := e[0], e[1]; b != fa { + ans += c + dfs(b, a) } } - return ans + return } - return dfs(0) + return dfs(0, -1) +} +``` + +### **TypeScript** + +```ts +function minReorder(n: number, connections: number[][]): number { + const g: [number, number][][] = Array.from({ length: n }, () => []); + for (const [a, b] of connections) { + g[a].push([b, 1]); + g[b].push([a, 0]); + } + const dfs = (a: number, fa: number): number => { + let ans = 0; + for (const [b, c] of g[a]) { + if (b !== fa) { + ans += c + dfs(b, a); + } + } + return ans; + }; + return dfs(0, -1); +} +``` + +### **Rust** + +```rust +impl Solution { + pub fn min_reorder(n: i32, connections: Vec>) -> i32 { + let mut g: Vec> = vec![vec![]; n as usize]; + for e in connections.iter() { + let a = e[0] as usize; + let b = e[1] as usize; + g[a].push((b as i32, 1)); + g[b].push((a as i32, 0)); + } + fn dfs(a: usize, fa: i32, g: &Vec>) -> i32 { + let mut ans = 0; + for &(b, c) in g[a].iter() { + if b != fa { + ans += c + dfs(b as usize, a as i32, g); + } + } + ans + } + dfs(0, -1, &g) + } } ``` diff --git a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.cpp b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.cpp index d08fcbcecfaa9..c1ac286b2ccf9 100644 --- a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.cpp +++ b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.cpp @@ -1,27 +1,21 @@ -class Solution { -public: - int minReorder(int n, vector>& connections) { - unordered_map>> g; - for (auto& e : connections) { - int u = e[0], v = e[1]; - g[u].push_back({v, true}); - g[v].push_back({u, false}); - } - vector vis(n); - return dfs(0, g, vis); - } - - int dfs(int u, unordered_map>>& g, vector& vis) { - vis[u] = true; - int ans = 0; - for (auto& p : g[u]) { - int v = p.first; - bool exist = p.second; - if (!vis[v]) { - if (exist) ++ans; - ans += dfs(v, g, vis); - } - } - return ans; - } +class Solution { +public: + int minReorder(int n, vector>& connections) { + vector> g[n]; + for (auto& e : connections) { + int a = e[0], b = e[1]; + g[a].emplace_back(b, 1); + g[b].emplace_back(a, 0); + } + function dfs = [&](int a, int fa) { + int ans = 0; + for (auto& [b, c] : g[a]) { + if (b != fa) { + ans += c + dfs(b, a); + } + } + return ans; + }; + return dfs(0, -1); + } }; \ No newline at end of file diff --git a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.go b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.go index 66494169bf2c2..66bd5c48ff160 100644 --- a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.go +++ b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.go @@ -1,29 +1,18 @@ func minReorder(n int, connections [][]int) int { - type pib struct { - v int - b bool - } - g := map[int][]pib{} + g := make([][][2]int, n) for _, e := range connections { - u, v := e[0], e[1] - g[u] = append(g[u], pib{v, true}) - g[v] = append(g[v], pib{u, false}) + a, b := e[0], e[1] + g[a] = append(g[a], [2]int{b, 1}) + g[b] = append(g[b], [2]int{a, 0}) } - vis := make([]bool, n) - var dfs func(int) int - dfs = func(u int) int { - ans := 0 - vis[u] = true - for _, p := range g[u] { - v, exist := p.v, p.b - if !vis[v] { - if exist { - ans++ - } - ans += dfs(v) + var dfs func(int, int) int + dfs = func(a, fa int) (ans int) { + for _, e := range g[a] { + if b, c := e[0], e[1]; b != fa { + ans += c + dfs(b, a) } } - return ans + return } - return dfs(0) + return dfs(0, -1) } \ No newline at end of file diff --git a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.java b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.java index 5c0db29bea4f6..0ef19041a7dc9 100644 --- a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.java +++ b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.java @@ -1,28 +1,25 @@ -class Solution { - public int minReorder(int n, int[][] connections) { - Map>> g = new HashMap(); - for (int[] e : connections) { - int u = e[0], v = e[1]; - g.computeIfAbsent(u, k -> new ArrayList()).add(new Pair(v, true)); - g.computeIfAbsent(v, k -> new ArrayList()).add(new Pair(u, false)); - } - boolean[] vis = new boolean[n]; - return dfs(0, g, vis); - } - - private int dfs(int u, Map>> g, boolean[] vis) { - vis[u] = true; - int ans = 0; - for (Pair e : g.getOrDefault(u, Collections.emptyList())) { - int v = e.getKey(); - boolean exist = e.getValue(); - if (!vis[v]) { - if (exist) { - ++ans; - } - ans += dfs(v, g, vis); - } - } - return ans; - } +class Solution { + private List[] g; + + public int minReorder(int n, int[][] connections) { + g = new List[n]; + Arrays.setAll(g, k -> new ArrayList()); + for (var e : connections) { + int a = e[0], b = e[1]; + g[a].add(new int[] {b, 1}); + g[b].add(new int[] {a, 0}); + } + return dfs(0, -1); + } + + private int dfs(int a, int fa) { + int ans = 0; + for (var e : g[a]) { + int b = e[0], c = e[1]; + if (b != fa) { + ans += c + dfs(b, a); + } + } + return ans; + } } \ No newline at end of file diff --git a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.py b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.py index ed0798abdfff7..49271fb166ff5 100644 --- a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.py +++ b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.py @@ -1,20 +1,10 @@ -class Solution: - def minReorder(self, n: int, connections: List[List[int]]) -> int: - def dfs(u): - vis[u] = True - ans = 0 - for v in g[u]: - if not vis[v]: - if (u, v) in s: - ans += 1 - ans += dfs(v) - return ans - - g = defaultdict(list) - s = set() - for a, b in connections: - g[a].append(b) - g[b].append(a) - s.add((a, b)) - vis = [False] * n - return dfs(0) +class Solution: + def minReorder(self, n: int, connections: List[List[int]]) -> int: + def dfs(a: int, fa: int) -> int: + return sum(c + dfs(b, a) for b, c in g[a] if b != fa) + + g = [[] for _ in range(n)] + for a, b in connections: + g[a].append((b, 1)) + g[b].append((a, 0)) + return dfs(0, -1) diff --git a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.rs b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.rs new file mode 100644 index 0000000000000..ed99514902f28 --- /dev/null +++ b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.rs @@ -0,0 +1,21 @@ +impl Solution { + pub fn min_reorder(n: i32, connections: Vec>) -> i32 { + let mut g: Vec> = vec![vec![]; n as usize]; + for e in connections.iter() { + let a = e[0] as usize; + let b = e[1] as usize; + g[a].push((b as i32, 1)); + g[b].push((a as i32, 0)); + } + fn dfs(a: usize, fa: i32, g: &Vec>) -> i32 { + let mut ans = 0; + for &(b, c) in g[a].iter() { + if b != fa { + ans += c + dfs(b as usize, a as i32, g); + } + } + ans + } + dfs(0, -1, &g) + } +} diff --git a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.ts b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.ts new file mode 100644 index 0000000000000..20836e1303a7b --- /dev/null +++ b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.ts @@ -0,0 +1,17 @@ +function minReorder(n: number, connections: number[][]): number { + const g: [number, number][][] = Array.from({ length: n }, () => []); + for (const [a, b] of connections) { + g[a].push([b, 1]); + g[b].push([a, 0]); + } + const dfs = (a: number, fa: number): number => { + let ans = 0; + for (const [b, c] of g[a]) { + if (b !== fa) { + ans += c + dfs(b, a); + } + } + return ans; + }; + return dfs(0, -1); +} diff --git a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/README.md b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/README.md index e79c6918b2446..c2d0267e73cc4 100644 --- a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/README.md +++ b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/README.md @@ -90,15 +90,15 @@ ```python class Solution: def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int: - def dfs(a, fa): - size = 1 + def dfs(a: int, fa: int) -> int: + nonlocal ans + sz = 1 for b in g[a]: if b != fa: t = dfs(b, a) - nonlocal ans - ans += (t + seats - 1) // seats - size += t - return size + ans += ceil(t / seats) + sz += t + return sz g = defaultdict(list) for a, b in roads: @@ -116,8 +116,8 @@ class Solution: ```java class Solution { private List[] g; - private long ans; private int seats; + private long ans; public long minimumFuelCost(int[][] roads, int seats) { int n = roads.length + 1; @@ -134,15 +134,15 @@ class Solution { } private int dfs(int a, int fa) { - int size = 1; + int sz = 1; for (int b : g[a]) { if (b != fa) { int t = dfs(b, a); ans += (t + seats - 1) / seats; - size += t; + sz += t; } } - return size; + return sz; } } ``` @@ -154,23 +154,23 @@ class Solution { public: long long minimumFuelCost(vector>& roads, int seats) { int n = roads.size() + 1; - vector> g(n); + vector g[n]; for (auto& e : roads) { int a = e[0], b = e[1]; g[a].emplace_back(b); g[b].emplace_back(a); } long long ans = 0; - function dfs = [&](int a, int fa) -> int { - int size = 1; + function dfs = [&](int a, int fa) { + int sz = 1; for (int b : g[a]) { if (b != fa) { int t = dfs(b, a); ans += (t + seats - 1) / seats; - size += t; + sz += t; } } - return size; + return sz; }; dfs(0, -1); return ans; @@ -181,7 +181,7 @@ public: ### **Go** ```go -func minimumFuelCost(roads [][]int, seats int) int64 { +func minimumFuelCost(roads [][]int, seats int) (ans int64) { n := len(roads) + 1 g := make([][]int, n) for _, e := range roads { @@ -189,28 +189,79 @@ func minimumFuelCost(roads [][]int, seats int) int64 { g[a] = append(g[a], b) g[b] = append(g[b], a) } - ans := 0 var dfs func(int, int) int dfs = func(a, fa int) int { - size := 1 + sz := 1 for _, b := range g[a] { if b != fa { t := dfs(b, a) - ans += (t + seats - 1) / seats - size += t + ans += int64((t + seats - 1) / seats) + sz += t } } - return size + return sz } dfs(0, -1) - return int64(ans) + return } ``` ### **TypeScript** ```ts +function minimumFuelCost(roads: number[][], seats: number): number { + const n = roads.length + 1; + const g: number[][] = Array.from({ length: n }, () => []); + for (const [a, b] of roads) { + g[a].push(b); + g[b].push(a); + } + let ans = 0; + const dfs = (a: number, fa: number): number => { + let sz = 1; + for (const b of g[a]) { + if (b !== fa) { + const t = dfs(b, a); + ans += Math.ceil(t / seats); + sz += t; + } + } + return sz; + }; + dfs(0, -1); + return ans; +} +``` +### **Rust** + +```rust +impl Solution { + pub fn minimum_fuel_cost(roads: Vec>, seats: i32) -> i64 { + let n = roads.len() + 1; + let mut g: Vec> = vec![vec![]; n]; + for road in roads.iter() { + let a = road[0] as usize; + let b = road[1] as usize; + g[a].push(b); + g[b].push(a); + } + let mut ans = 0; + fn dfs(a: usize, fa: i32, g: &Vec>, ans: &mut i64, seats: i32) -> i32 { + let mut sz = 1; + for &b in g[a].iter() { + if (b as i32) != fa { + let t = dfs(b, a as i32, g, ans, seats); + *ans += ((t + seats - 1) / seats) as i64; + sz += t; + } + } + sz + } + dfs(0, -1, &g, &mut ans, seats); + ans + } +} ``` ### **...** diff --git a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/README_EN.md b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/README_EN.md index 5d8dbc9138107..5a55e774e0a61 100644 --- a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/README_EN.md +++ b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/README_EN.md @@ -75,15 +75,15 @@ It can be proven that 7 is the minimum number of liters of fuel needed. ```python class Solution: def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int: - def dfs(a, fa): - size = 1 + def dfs(a: int, fa: int) -> int: + nonlocal ans + sz = 1 for b in g[a]: if b != fa: t = dfs(b, a) - nonlocal ans - ans += (t + seats - 1) // seats - size += t - return size + ans += ceil(t / seats) + sz += t + return sz g = defaultdict(list) for a, b in roads: @@ -99,8 +99,8 @@ class Solution: ```java class Solution { private List[] g; - private long ans; private int seats; + private long ans; public long minimumFuelCost(int[][] roads, int seats) { int n = roads.length + 1; @@ -117,15 +117,15 @@ class Solution { } private int dfs(int a, int fa) { - int size = 1; + int sz = 1; for (int b : g[a]) { if (b != fa) { int t = dfs(b, a); ans += (t + seats - 1) / seats; - size += t; + sz += t; } } - return size; + return sz; } } ``` @@ -137,23 +137,23 @@ class Solution { public: long long minimumFuelCost(vector>& roads, int seats) { int n = roads.size() + 1; - vector> g(n); + vector g[n]; for (auto& e : roads) { int a = e[0], b = e[1]; g[a].emplace_back(b); g[b].emplace_back(a); } long long ans = 0; - function dfs = [&](int a, int fa) -> int { - int size = 1; + function dfs = [&](int a, int fa) { + int sz = 1; for (int b : g[a]) { if (b != fa) { int t = dfs(b, a); ans += (t + seats - 1) / seats; - size += t; + sz += t; } } - return size; + return sz; }; dfs(0, -1); return ans; @@ -164,7 +164,7 @@ public: ### **Go** ```go -func minimumFuelCost(roads [][]int, seats int) int64 { +func minimumFuelCost(roads [][]int, seats int) (ans int64) { n := len(roads) + 1 g := make([][]int, n) for _, e := range roads { @@ -172,28 +172,79 @@ func minimumFuelCost(roads [][]int, seats int) int64 { g[a] = append(g[a], b) g[b] = append(g[b], a) } - ans := 0 var dfs func(int, int) int dfs = func(a, fa int) int { - size := 1 + sz := 1 for _, b := range g[a] { if b != fa { t := dfs(b, a) - ans += (t + seats - 1) / seats - size += t + ans += int64((t + seats - 1) / seats) + sz += t } } - return size + return sz } dfs(0, -1) - return int64(ans) + return } ``` ### **TypeScript** ```ts +function minimumFuelCost(roads: number[][], seats: number): number { + const n = roads.length + 1; + const g: number[][] = Array.from({ length: n }, () => []); + for (const [a, b] of roads) { + g[a].push(b); + g[b].push(a); + } + let ans = 0; + const dfs = (a: number, fa: number): number => { + let sz = 1; + for (const b of g[a]) { + if (b !== fa) { + const t = dfs(b, a); + ans += Math.ceil(t / seats); + sz += t; + } + } + return sz; + }; + dfs(0, -1); + return ans; +} +``` +### **Rust** + +```rust +impl Solution { + pub fn minimum_fuel_cost(roads: Vec>, seats: i32) -> i64 { + let n = roads.len() + 1; + let mut g: Vec> = vec![vec![]; n]; + for road in roads.iter() { + let a = road[0] as usize; + let b = road[1] as usize; + g[a].push(b); + g[b].push(a); + } + let mut ans = 0; + fn dfs(a: usize, fa: i32, g: &Vec>, ans: &mut i64, seats: i32) -> i32 { + let mut sz = 1; + for &b in g[a].iter() { + if (b as i32) != fa { + let t = dfs(b, a as i32, g, ans, seats); + *ans += ((t + seats - 1) / seats) as i64; + sz += t; + } + } + sz + } + dfs(0, -1, &g, &mut ans, seats); + ans + } +} ``` ### **...** diff --git a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.cpp b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.cpp index 9f1d7090a0f8e..d2c78524b2cc3 100644 --- a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.cpp +++ b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.cpp @@ -1,26 +1,26 @@ -class Solution { -public: - long long minimumFuelCost(vector>& roads, int seats) { - int n = roads.size() + 1; - vector> g(n); - for (auto& e : roads) { - int a = e[0], b = e[1]; - g[a].emplace_back(b); - g[b].emplace_back(a); - } - long long ans = 0; - function dfs = [&](int a, int fa) -> int { - int size = 1; - for (int b : g[a]) { - if (b != fa) { - int t = dfs(b, a); - ans += (t + seats - 1) / seats; - size += t; - } - } - return size; - }; - dfs(0, -1); - return ans; - } +class Solution { +public: + long long minimumFuelCost(vector>& roads, int seats) { + int n = roads.size() + 1; + vector g[n]; + for (auto& e : roads) { + int a = e[0], b = e[1]; + g[a].emplace_back(b); + g[b].emplace_back(a); + } + long long ans = 0; + function dfs = [&](int a, int fa) { + int sz = 1; + for (int b : g[a]) { + if (b != fa) { + int t = dfs(b, a); + ans += (t + seats - 1) / seats; + sz += t; + } + } + return sz; + }; + dfs(0, -1); + return ans; + } }; \ No newline at end of file diff --git a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.go b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.go index 46a9178774c58..ae8fa81746456 100644 --- a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.go +++ b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.go @@ -1,4 +1,4 @@ -func minimumFuelCost(roads [][]int, seats int) int64 { +func minimumFuelCost(roads [][]int, seats int) (ans int64) { n := len(roads) + 1 g := make([][]int, n) for _, e := range roads { @@ -6,19 +6,18 @@ func minimumFuelCost(roads [][]int, seats int) int64 { g[a] = append(g[a], b) g[b] = append(g[b], a) } - ans := 0 var dfs func(int, int) int dfs = func(a, fa int) int { - size := 1 + sz := 1 for _, b := range g[a] { if b != fa { t := dfs(b, a) - ans += (t + seats - 1) / seats - size += t + ans += int64((t + seats - 1) / seats) + sz += t } } - return size + return sz } dfs(0, -1) - return int64(ans) + return } \ No newline at end of file diff --git a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.java b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.java index cbf1f398f45b9..e5eaf7d67cec7 100644 --- a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.java +++ b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.java @@ -1,31 +1,31 @@ -class Solution { - private List[] g; - private long ans; - private int seats; - - public long minimumFuelCost(int[][] roads, int seats) { - int n = roads.length + 1; - g = new List[n]; - Arrays.setAll(g, k -> new ArrayList()); - this.seats = seats; - for (var e : roads) { - int a = e[0], b = e[1]; - g[a].add(b); - g[b].add(a); - } - dfs(0, -1); - return ans; - } - - private int dfs(int a, int fa) { - int size = 1; - for (int b : g[a]) { - if (b != fa) { - int t = dfs(b, a); - ans += (t + seats - 1) / seats; - size += t; - } - } - return size; - } +class Solution { + private List[] g; + private int seats; + private long ans; + + public long minimumFuelCost(int[][] roads, int seats) { + int n = roads.length + 1; + g = new List[n]; + Arrays.setAll(g, k -> new ArrayList()); + this.seats = seats; + for (var e : roads) { + int a = e[0], b = e[1]; + g[a].add(b); + g[b].add(a); + } + dfs(0, -1); + return ans; + } + + private int dfs(int a, int fa) { + int sz = 1; + for (int b : g[a]) { + if (b != fa) { + int t = dfs(b, a); + ans += (t + seats - 1) / seats; + sz += t; + } + } + return sz; + } } \ No newline at end of file diff --git a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.py b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.py index f4e16c3fe8347..d12b46f1943f8 100644 --- a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.py +++ b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.py @@ -1,19 +1,19 @@ -class Solution: - def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int: - def dfs(a, fa): - size = 1 - for b in g[a]: - if b != fa: - t = dfs(b, a) - nonlocal ans - ans += (t + seats - 1) // seats - size += t - return size - - g = defaultdict(list) - for a, b in roads: - g[a].append(b) - g[b].append(a) - ans = 0 - dfs(0, -1) - return ans +class Solution: + def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int: + def dfs(a: int, fa: int) -> int: + nonlocal ans + sz = 1 + for b in g[a]: + if b != fa: + t = dfs(b, a) + ans += ceil(t / seats) + sz += t + return sz + + g = defaultdict(list) + for a, b in roads: + g[a].append(b) + g[b].append(a) + ans = 0 + dfs(0, -1) + return ans diff --git a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.rs b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.rs new file mode 100644 index 0000000000000..883a50343af5e --- /dev/null +++ b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.rs @@ -0,0 +1,26 @@ +impl Solution { + pub fn minimum_fuel_cost(roads: Vec>, seats: i32) -> i64 { + let n = roads.len() + 1; + let mut g: Vec> = vec![vec![]; n]; + for road in roads.iter() { + let a = road[0] as usize; + let b = road[1] as usize; + g[a].push(b); + g[b].push(a); + } + let mut ans = 0; + fn dfs(a: usize, fa: i32, g: &Vec>, ans: &mut i64, seats: i32) -> i32 { + let mut sz = 1; + for &b in g[a].iter() { + if (b as i32) != fa { + let t = dfs(b, a as i32, g, ans, seats); + *ans += ((t + seats - 1) / seats) as i64; + sz += t; + } + } + sz + } + dfs(0, -1, &g, &mut ans, seats); + ans + } +} diff --git a/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.ts b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.ts new file mode 100644 index 0000000000000..642e2f5f79918 --- /dev/null +++ b/solution/2400-2499/2477.Minimum Fuel Cost to Report to the Capital/Solution.ts @@ -0,0 +1,22 @@ +function minimumFuelCost(roads: number[][], seats: number): number { + const n = roads.length + 1; + const g: number[][] = Array.from({ length: n }, () => []); + for (const [a, b] of roads) { + g[a].push(b); + g[b].push(a); + } + let ans = 0; + const dfs = (a: number, fa: number): number => { + let sz = 1; + for (const b of g[a]) { + if (b !== fa) { + const t = dfs(b, a); + ans += Math.ceil(t / seats); + sz += t; + } + } + return sz; + }; + dfs(0, -1); + return ans; +}

AltStyle によって変換されたページ (->オリジナル) /