|
| 1 | +import javafx.util.Pair; |
| 2 | + |
1 | 3 | import java.util.*;
|
2 | 4 |
|
3 | 5 | import java.math.BigInteger;
|
4 | 6 |
|
5 | 7 | class Solution {
|
6 |
| - public boolean doesValidArrayExist(int[] derived) { |
7 |
| - if (derived.length == 1) return derived[0] == 0; |
8 |
| - int[] original = new int[derived.length]; |
9 |
| - Arrays.fill(original, -1); |
10 |
| - for (int i = 0; i < derived.length; i++) { |
11 |
| - if (derived[i] == 0) { |
12 |
| - original[i] = 0; |
13 |
| - original[(i + 1) % derived.length] = 0; |
| 8 | + |
| 9 | + HashSet<Pair<Integer, Integer>> edgesToReplace = new HashSet<>(); |
| 10 | + |
| 11 | + public int[][] modifiedGraphEdges(int n, int[][] edges, int source, int destination, int target) { |
| 12 | + HashMap<Integer, HashMap<Integer, Integer>> adjList = makeAdjList(edges); |
| 13 | + LinkedList<Integer> minimumPath = bfs(n, adjList, source, destination, target); |
| 14 | + HashSet<Pair<Integer, Integer>> mustReplace = new HashSet<>(); |
| 15 | + replaceMinimumPath(minimumPath, 0, adjList, target, mustReplace); |
| 16 | + |
| 17 | + for (Pair<Integer, Integer> edgeToReplace : edgesToReplace) { |
| 18 | + int from = edgeToReplace.getKey(); |
| 19 | + int to = edgeToReplace.getValue(); |
| 20 | + Pair<Integer, Integer> pairKey = new Pair<>(Math.min(from, to), Math.max(from, to)); |
| 21 | + if (mustReplace.contains(pairKey)) continue; |
| 22 | + adjList.get(from).put(to, Integer.MAX_VALUE); |
| 23 | + adjList.get(to).put(from, Integer.MAX_VALUE); |
| 24 | + } |
| 25 | + |
| 26 | + target -= mustReplace.size(); |
| 27 | + if (target < 0) return new int[0][0]; |
| 28 | + |
| 29 | + |
| 30 | + for (Pair<Integer, Integer> edgeToReplace : mustReplace) { |
| 31 | + int from = edgeToReplace.getKey(); |
| 32 | + int to = edgeToReplace.getValue(); |
| 33 | + |
| 34 | + adjList.get(from).put(to, 1); |
| 35 | + adjList.get(to).put(from, 1); |
| 36 | + |
| 37 | + |
| 38 | + int maxAllowed = Integer.MAX_VALUE; |
| 39 | + int replace = 1; |
| 40 | + if (target > 0) { |
| 41 | + HashMap<Integer, Integer> edgesFrom = adjList.get(from); |
| 42 | + for (Map.Entry<Integer, Integer> kv : edgesFrom.entrySet()) { |
| 43 | + if (kv.getValue() == -1 || kv.getKey() == to) continue; |
| 44 | + maxAllowed = Math.min(maxAllowed, kv.getValue()); |
| 45 | + } |
| 46 | + if (maxAllowed >= target + 1) { |
| 47 | + replace = target + 1; |
| 48 | + } else { |
| 49 | + replace = maxAllowed; |
| 50 | + } |
| 51 | + target -= replace; |
14 | 52 | }
|
| 53 | + adjList.get(from).put(to, replace); |
| 54 | + adjList.get(to).put(from, replace); |
| 55 | + |
| 56 | + } |
| 57 | + if (target > 0) return new int[0][0]; |
| 58 | + for (int[] edge : edges) { |
| 59 | + int from = edge[0]; |
| 60 | + int to = edge[1]; |
| 61 | + edge[2] = adjList.get(from).get(to); |
15 | 62 | }
|
16 |
| - return doesValidArrayExistHelper(0, derived, original); |
| 63 | + return edges; |
| 64 | + |
17 | 65 | }
|
18 | 66 |
|
19 |
| - public boolean doesValidArrayExistHelper(int i, int[] derived, int[] original) { |
20 |
| - if (i == derived.length) return true; |
| 67 | + private void replaceMinimumPath( |
| 68 | + LinkedList<Integer> minimumPath, |
| 69 | + int i, |
| 70 | + HashMap<Integer, HashMap<Integer, Integer>> adjList, |
| 71 | + int target, |
| 72 | + HashSet<Pair<Integer, Integer>> mustReplace |
| 73 | + ) { |
| 74 | + if (i == minimumPath.size() - 1) { |
| 75 | + return; |
| 76 | + } |
| 77 | + int from = minimumPath.get(i); |
| 78 | + int to = minimumPath.get(i + 1); |
| 79 | + int cost = adjList.get(from).get(to); |
| 80 | + if (cost == -1) { |
| 81 | + mustReplace.add(new Pair<>(Math.min(from, to), Math.max(from, to))); |
| 82 | + } |
| 83 | + replaceMinimumPath(minimumPath, i + 1, adjList, target - cost, mustReplace); |
21 | 84 |
|
22 |
| - if (derived[i] == 1) { |
23 |
| - if (original[i] == 0 && original[(i + 1) % derived.length] == 0) return false; |
24 |
| - if (original[i] == 0) { |
25 |
| - if (original[(i + 1) % derived.length] == 1) return false; |
26 |
| - original[(i + 1) % derived.length] = 0; |
| 85 | + } |
| 86 | + |
| 87 | + private HashMap<Integer, HashMap<Integer, Integer>> makeAdjList(int[][] edges) { |
| 88 | + HashMap<Integer, HashMap<Integer, Integer>> adjList = new HashMap<>(); |
| 89 | + for (int[] edge : edges) { |
| 90 | + int from = edge[0]; |
| 91 | + int to = edge[1]; |
| 92 | + int weight = edge[2]; |
| 93 | + adjList.computeIfAbsent(from, (v) -> new HashMap<>()).put(to, weight); |
| 94 | + adjList.computeIfAbsent(to, (v) -> new HashMap<>()).put(from, weight); |
| 95 | + if (weight == -1) { |
| 96 | + edgesToReplace.add(new Pair<>(Math.min(from, to), Math.max(from, to))); |
27 | 97 | }
|
28 |
| - int before = (i - 1) < 0 ? derived.length - 1 : i - 1; |
29 |
| - int after = (i + 1) % derived.length; |
30 |
| - |
31 |
| - |
32 |
| - if (original[before] == 0 || original[after] == 0) { |
33 |
| - original[i] = 1; |
34 |
| - return doesValidArrayExistHelper(i + 1, derived, original); |
35 |
| - } else { |
36 |
| -// if (original[before] == -1) { |
37 |
| -// original[before] = 0; |
38 |
| -// original[i] = 1; |
39 |
| -// if (doesValidArrayExistHelper(i + 1, derived, original)) { |
40 |
| -// return true; |
41 |
| -// } |
42 |
| -// original[i] = -1; |
43 |
| -// original[before] = -1; |
44 |
| -// } |
45 |
| - if (original[after] == -1) { |
46 |
| - original[after] = 0; |
47 |
| - original[i] = 1; |
| 98 | + } |
| 99 | + return adjList; |
| 100 | + } |
| 101 | + |
| 102 | + private LinkedList<Integer> bfs(int n, HashMap<Integer, HashMap<Integer, Integer>> adjList, int source, int destination, int target) { |
| 103 | + Queue<int[]> q = new LinkedList<>(); |
| 104 | + q.add(new int[]{source, 0}); |
| 105 | + HashMap<Integer, Integer> visited = new HashMap<>(); |
| 106 | + int[] path = new int[n + 1]; |
| 107 | + Arrays.fill(path, -1); |
| 108 | + visited.put(source, -1); |
| 109 | + |
| 110 | + boolean found = false; |
| 111 | + while (!q.isEmpty()) { |
| 112 | + int size = q.size(); |
| 113 | + |
| 114 | + for (int i = 0; i < size; i++) { |
| 115 | + int[] info = q.poll(); |
| 116 | + int current = info[0]; |
| 117 | + int cost = info[1]; |
| 118 | + visited.put(current, Math.min(cost, visited.getOrDefault(current, Integer.MAX_VALUE))); |
| 119 | + if (current == destination) { |
| 120 | + found = true; |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + HashMap<Integer, Integer> connections = adjList.get(current); |
| 125 | + for (Map.Entry<Integer, Integer> kv : connections.entrySet()) { |
| 126 | + int to = kv.getKey(); |
| 127 | + int weight = kv.getValue(); |
| 128 | + if (weight == -1) { |
| 129 | + weight = 1; |
| 130 | + |
| 131 | + } |
| 132 | + if (visited.getOrDefault(to, Integer.MAX_VALUE) >= cost + weight) { |
| 133 | + path[to] = current; |
| 134 | + q.add(new int[]{to, cost + weight}); |
| 135 | + } |
48 | 136 | }
|
49 | 137 | }
|
| 138 | + if (found) break; |
50 | 139 |
|
51 |
| - return doesValidArrayExistHelper(i + 1, derived, original); |
52 |
| - } else { |
53 |
| - return doesValidArrayExistHelper(i + 1, derived, original); |
54 | 140 | }
|
55 | 141 |
|
| 142 | + LinkedList<Integer> pathList = new LinkedList<>(); |
| 143 | + |
| 144 | + getPath(path, destination, pathList); |
| 145 | + |
| 146 | + |
| 147 | + return pathList; |
| 148 | + |
56 | 149 | }
|
| 150 | + |
| 151 | + private void getPath(int[] path, int i, LinkedList<Integer> pathList) { |
| 152 | + pathList.addFirst(i); |
| 153 | + if (path[i] == -1) { |
| 154 | + return; |
| 155 | + } |
| 156 | + getPath(path, path[i], pathList); |
| 157 | + } |
| 158 | + |
| 159 | + |
57 | 160 | }
|
58 | 161 |
|
59 | 162 | public class Contest extends Solution {
|
|
0 commit comments