|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.LinkedList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Queue; |
| 11 | +import java.util.Set; |
| 12 | + |
| 13 | +public class wk322 { |
| 14 | + //ranking: 349 / 5085 |
| 15 | + //直接判断 |
| 16 | + public boolean isCircularSentence(String sentence) { |
| 17 | + String[] s = sentence.split(" "); |
| 18 | + for (int i = 0; i < s.length - 1; i++) { |
| 19 | + if (s[i].charAt(s[i].length() - 1) != s[i + 1].charAt(0)) { |
| 20 | + return false; |
| 21 | + } |
| 22 | + } |
| 23 | + //判断最后一个 |
| 24 | + if (s[0].charAt(0) != s[s.length - 1].charAt(s[s.length - 1].length() - 1)) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + return true; |
| 28 | + } |
| 29 | + |
| 30 | + //排序+双指针 |
| 31 | + public long dividePlayers(int[] skill) { |
| 32 | + Arrays.sort(skill); |
| 33 | + long cur = -1; |
| 34 | + long ans = 0; |
| 35 | + for (int i = 0; i < skill.length / 2; i++) { |
| 36 | + long sum = skill[i] + skill[skill.length - i - 1]; |
| 37 | + if (cur != -1 && sum != cur) { |
| 38 | + return -1; |
| 39 | + } |
| 40 | + cur = sum; |
| 41 | + ans += (long) skill[i] * skill[skill.length - i - 1]; |
| 42 | + } |
| 43 | + return ans; |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + //求联通块的最小路径 |
| 48 | + public int minScore(int n, int[][] roads) { |
| 49 | + boolean[] visit = new boolean[n + 1]; |
| 50 | + int min = Integer.MAX_VALUE; |
| 51 | + Map<Integer, List<int[]>> map = new HashMap<>(); |
| 52 | + for (int[] road : roads) { |
| 53 | + if (!map.containsKey(road[0])) { |
| 54 | + map.put(road[0], new ArrayList<>()); |
| 55 | + } |
| 56 | + if (!map.containsKey(road[1])) { |
| 57 | + map.put(road[1], new ArrayList<>()); |
| 58 | + } |
| 59 | + map.get(road[0]).add(new int[]{road[1], road[2]}); |
| 60 | + map.get(road[1]).add(new int[]{road[0], road[2]}); |
| 61 | + } |
| 62 | + Queue<Integer> queue = new LinkedList<>(); |
| 63 | + queue.add(1); |
| 64 | + while (!queue.isEmpty()) { |
| 65 | + Integer cur = queue.poll(); |
| 66 | + visit[cur] = true; |
| 67 | + for (int[] next : map.getOrDefault(cur, new ArrayList<>())) { |
| 68 | + if (visit[next[0]]) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + min = Math.min(min, next[1]); |
| 72 | + queue.add(next[0]); |
| 73 | + } |
| 74 | + } |
| 75 | + return min; |
| 76 | + } |
| 77 | + |
| 78 | + //有些直觉的想法是错的,再做下去也不会对啦 |
| 79 | + //只考虑到三个边的换不可以,其实所有奇数边都不行,所以要判断是不是二分图 |
| 80 | + /*public int magnificentSets(int n, int[][] roads) { |
| 81 | + Map<Integer, Set<Integer>> map = new HashMap<>(); |
| 82 | + UnionFind uf = new UnionFind(n + 1); |
| 83 | +
|
| 84 | + for (int[] road : roads) { |
| 85 | + uf.union(road[0], road[1]); |
| 86 | + if (!map.containsKey(road[0])) { |
| 87 | + map.put(road[0], new HashSet<>()); |
| 88 | + } |
| 89 | + if (!map.containsKey(road[1])) { |
| 90 | + map.put(road[1], new HashSet<>()); |
| 91 | + } |
| 92 | + map.get(road[0]).add(road[1]); |
| 93 | + map.get(road[1]).add(road[0]); |
| 94 | + } |
| 95 | + if (!check(map)) { |
| 96 | + return -1; |
| 97 | + } |
| 98 | +
|
| 99 | + Map<Integer, Integer> res = new HashMap<>(); |
| 100 | + for (int i = 1; i < n; i++) { |
| 101 | + boolean[] visit = new boolean[n + 1]; |
| 102 | + Queue<Integer> queue = new LinkedList<>(); |
| 103 | + queue.add(i); |
| 104 | + int m = 0; |
| 105 | + while (!queue.isEmpty()) { |
| 106 | + int size = queue.size(); |
| 107 | + while (size-- > 0) { |
| 108 | + Integer cur = queue.poll(); |
| 109 | + visit[cur] = true; |
| 110 | + for (Integer next : map.getOrDefault(cur, new HashSet<>())) { |
| 111 | + if (visit[next]) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + queue.add(next); |
| 115 | + } |
| 116 | + } |
| 117 | + m++; |
| 118 | + } |
| 119 | + res.put(uf.find(i), Math.max(res.getOrDefault(uf.find(i), 0), m)); |
| 120 | + } |
| 121 | + int ans = 0; |
| 122 | + for (Map.Entry<Integer, Integer> entry : res.entrySet()) { |
| 123 | + ans += entry.getValue(); |
| 124 | + } |
| 125 | + return ans; |
| 126 | + } |
| 127 | +
|
| 128 | + boolean check(Map<Integer, Set<Integer>> map) { |
| 129 | + for (Map.Entry<Integer, Set<Integer>> entry : map.entrySet()) { |
| 130 | + for (Integer two : entry.getValue()) { |
| 131 | + Set<Integer> three = map.getOrDefault(two, new HashSet<>()); |
| 132 | + for (Integer integer : three) { |
| 133 | + if (entry.getValue().contains(integer)) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + return true; |
| 140 | + } |
| 141 | +
|
| 142 | + //并查集 |
| 143 | + class UnionFind { |
| 144 | +
|
| 145 | + public final int[] parents; |
| 146 | + public int count; |
| 147 | +
|
| 148 | + public UnionFind(int n) { |
| 149 | + this.parents = new int[n]; |
| 150 | + reset(); |
| 151 | + } |
| 152 | +
|
| 153 | + public void reset() { |
| 154 | + for (int i = 0; i < parents.length; i++) { |
| 155 | + parents[i] = i; |
| 156 | + } |
| 157 | + count = parents.length - 1; |
| 158 | + } |
| 159 | +
|
| 160 | + public int find(int i) { |
| 161 | + int parent = parents[i]; |
| 162 | + if (parent == i) { |
| 163 | + return i; |
| 164 | + } else { |
| 165 | + int root = find(parent); |
| 166 | + parents[i] = root; |
| 167 | + return root; |
| 168 | + } |
| 169 | + } |
| 170 | +
|
| 171 | + public boolean union(int i, int j) { |
| 172 | + int r1 = find(i); |
| 173 | + int r2 = find(j); |
| 174 | + if (r1 != r2) { |
| 175 | + count--; |
| 176 | + parents[r1] = r2; |
| 177 | + return true; |
| 178 | + } else { |
| 179 | + return false; |
| 180 | + } |
| 181 | + } |
| 182 | +
|
| 183 | +
|
| 184 | + }*/ |
| 185 | + |
| 186 | + //判断二分图 |
| 187 | + //+ bfs求最长路径 |
| 188 | + public int magnificentSets(int n, int[][] edges) { |
| 189 | + int[] visited = new int[n]; |
| 190 | + Map<Integer, List<Integer>> map = new HashMap<>(); |
| 191 | + |
| 192 | + for (int[] road : edges) { |
| 193 | + if (!map.containsKey(road[0] - 1)) { |
| 194 | + map.put(road[0] - 1, new ArrayList<>()); |
| 195 | + } |
| 196 | + if (!map.containsKey(road[1] - 1)) { |
| 197 | + map.put(road[1] - 1, new ArrayList<>()); |
| 198 | + } |
| 199 | + map.get(road[0] - 1).add(road[1] - 1); |
| 200 | + map.get(road[1] - 1).add(road[0] - 1); |
| 201 | + } |
| 202 | + timer = new int[n]; |
| 203 | + int ans = 0; |
| 204 | + for (int i = 0; i < n; i++) { |
| 205 | + if (visited[i] != 0) continue; |
| 206 | + //该联通图的点数 |
| 207 | + List<Integer> nodes = new ArrayList<>(); |
| 208 | + if (!help(i, -1, nodes, map, 1, visited)) { |
| 209 | + return -1; |
| 210 | + } |
| 211 | + int max = 0; |
| 212 | + for (Integer node : nodes) { |
| 213 | + max = Math.max(max, bfs(node, map, n)); |
| 214 | + } |
| 215 | + ans += max; |
| 216 | + } |
| 217 | + return ans; |
| 218 | + } |
| 219 | + |
| 220 | + int[] timer; |
| 221 | + int clock; |
| 222 | + |
| 223 | + int bfs(int index, Map<Integer, List<Integer>> map, int n) { |
| 224 | + Queue<Integer> queue = new LinkedList<>(); |
| 225 | + queue.add(index); |
| 226 | + clock++; |
| 227 | + timer[index]=clock; |
| 228 | + int step = 0; |
| 229 | + while (!queue.isEmpty()) { |
| 230 | + int size = queue.size(); |
| 231 | + while (size-- > 0) { |
| 232 | + Integer poll = queue.poll(); |
| 233 | + for (Integer next : map.getOrDefault(poll, new ArrayList<>())) { |
| 234 | + if (timer[next] == clock) continue; |
| 235 | + timer[next] = clock; |
| 236 | + queue.add(next); |
| 237 | + } |
| 238 | + } |
| 239 | + step++; |
| 240 | + } |
| 241 | + return step; |
| 242 | + |
| 243 | + } |
| 244 | + |
| 245 | + //染色标记法判断二分图 |
| 246 | + boolean help(int node, int pre, List<Integer> nodes, Map<Integer, List<Integer>> map, int color, int[] visited) { |
| 247 | + if (visited[node] != 0) { |
| 248 | + return visited[node] == color; |
| 249 | + } |
| 250 | + visited[node] = color; |
| 251 | + nodes.add(node); |
| 252 | + for (Integer next : map.getOrDefault(node, new ArrayList<>())) { |
| 253 | + if (next == pre) continue; |
| 254 | + if (!help(next, node, nodes, map, -color, visited)) { |
| 255 | + return false; |
| 256 | + } |
| 257 | + } |
| 258 | + return true; |
| 259 | + } |
| 260 | +} |
0 commit comments