From 6c27b377f6375cacce6a4d57e32541ed43c37e70 Mon Sep 17 00:00:00 2001 From: kkunkka Date: Thu, 7 Dec 2023 23:16:11 +0800 Subject: [PATCH] kk --- src/main/java/com/dota/graph/Solution.java | 38 ++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/dota/graph/Solution.java b/src/main/java/com/dota/graph/Solution.java index 104ce19..0ebce4e 100644 --- a/src/main/java/com/dota/graph/Solution.java +++ b/src/main/java/com/dota/graph/Solution.java @@ -5,6 +5,7 @@ import java.util.*; class Solution { long res = 0; + int sum = 0; public long minimumFuelCost(int[][] roads, int seats) { List[] map = new List[roads.length + 1]; @@ -27,10 +28,43 @@ class Solution { if (i != from) { int childSum = dfs(i, start, seats, map); cur += childSum; - res += (childSum + seats - 1)/seats; + res += (childSum + seats - 1) / seats; } } return cur; } -} \ No newline at end of file + + // 1466. 重新规划路线 + public int minReorder(int n, int[][] connections) { + List[] list = new List[n]; + List[] otherList = new List[n]; + for (int i = 0; i < list.length; i++) { + list[i] = new ArrayList<>(); + otherList[i] = new ArrayList<>(); + } + + for (int[] ints : connections) { + list[ints[0]].add(ints[1]); + otherList[ints[1]].add(ints[0]); + } + + dfs(0, -1, list, otherList); + return sum; + } + + private void dfs(int now, int parent, List[] list, List[] otherList) { + for (int i = 0; i < list[now].size(); i++) { + if (list[now].get(i) != parent) { + sum++; + dfs(list[now].get(i), now, list, otherList); + } + } + + for (int i = 0; i < otherList[now].size(); i++) { + if (otherList[now].get(i) != parent) { + dfs(otherList[now].get(i), now, list, otherList); + } + } + } +}