|
| 1 | +package g3301_3400.s3311_construct_2d_grid_matching_graph_layout; |
| 2 | + |
| 3 | +// #Hard #Array #Hash_Table #Matrix #Graph #2024_10_08_Time_43_ms_(94.34%)_Space_103.6_MB_(79.25%) |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | + |
| 7 | +@SuppressWarnings("unchecked") |
| 8 | +public class Solution { |
| 9 | + public int[][] constructGridLayout(int n, int[][] edges) { |
| 10 | + final int[] cs = new int[n]; |
| 11 | + final ArrayList<Integer>[] als = new ArrayList[n]; |
| 12 | + for (int i = 0; i < n; ++i) { |
| 13 | + als[i] = new ArrayList<>(); |
| 14 | + } |
| 15 | + for (int[] e : edges) { |
| 16 | + cs[e[0]]++; |
| 17 | + cs[e[1]]++; |
| 18 | + als[e[0]].add(e[1]); |
| 19 | + als[e[1]].add(e[0]); |
| 20 | + } |
| 21 | + int min = 4; |
| 22 | + for (int a : cs) { |
| 23 | + min = Math.min(min, a); |
| 24 | + } |
| 25 | + final boolean[] seen = new boolean[n]; |
| 26 | + int[][] res; |
| 27 | + int st = 0; |
| 28 | + for (int i = 0; i < n; ++i) { |
| 29 | + if (cs[i] == min) { |
| 30 | + st = i; |
| 31 | + break; |
| 32 | + } |
| 33 | + } |
| 34 | + if (min == 1) { |
| 35 | + res = new int[1][n]; |
| 36 | + for (int i = 0; i < n; ++i) { |
| 37 | + res[0][i] = st; |
| 38 | + seen[st] = true; |
| 39 | + if (i + 1 < n) { |
| 40 | + for (int a : als[st]) { |
| 41 | + if (!seen[a]) { |
| 42 | + st = a; |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + return res; |
| 49 | + } |
| 50 | + int row2 = -1; |
| 51 | + for (int a : als[st]) { |
| 52 | + if (cs[a] == min) { |
| 53 | + row2 = a; |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + if (row2 >= 0) { |
| 58 | + return getInts(n, st, row2, seen, als); |
| 59 | + } |
| 60 | + return getInts(n, seen, st, als, cs); |
| 61 | + } |
| 62 | + |
| 63 | + private int[][] getInts(int n, boolean[] seen, int st, ArrayList<Integer>[] als, int[] cs) { |
| 64 | + int[][] res; |
| 65 | + final ArrayList<Integer> al = new ArrayList<>(); |
| 66 | + boolean f = true; |
| 67 | + seen[st] = true; |
| 68 | + al.add(st); |
| 69 | + while (f) { |
| 70 | + f = false; |
| 71 | + for (int a : als[st]) { |
| 72 | + if (!seen[a] && cs[a] <= 3) { |
| 73 | + seen[a] = true; |
| 74 | + al.add(a); |
| 75 | + if (cs[a] == 3) { |
| 76 | + f = true; |
| 77 | + st = a; |
| 78 | + } |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + res = new int[n / al.size()][al.size()]; |
| 84 | + for (int i = 0; i < res[0].length; ++i) { |
| 85 | + res[0][i] = al.get(i); |
| 86 | + } |
| 87 | + for (int i = 1; i < res.length; ++i) { |
| 88 | + for (int j = 0; j < res[0].length; ++j) { |
| 89 | + for (int a : als[res[i - 1][j]]) { |
| 90 | + if (!seen[a]) { |
| 91 | + res[i][j] = a; |
| 92 | + seen[a] = true; |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return res; |
| 99 | + } |
| 100 | + |
| 101 | + private int[][] getInts(int n, int st, int row2, boolean[] seen, ArrayList<Integer>[] als) { |
| 102 | + int[][] res; |
| 103 | + res = new int[2][n / 2]; |
| 104 | + res[0][0] = st; |
| 105 | + res[1][0] = row2; |
| 106 | + seen[st] = seen[row2] = true; |
| 107 | + for (int i = 1; i < res[0].length; ++i) { |
| 108 | + for (int a : als[res[0][i - 1]]) { |
| 109 | + if (!seen[a]) { |
| 110 | + res[0][i] = a; |
| 111 | + seen[a] = true; |
| 112 | + break; |
| 113 | + } |
| 114 | + } |
| 115 | + for (int a : als[res[1][i - 1]]) { |
| 116 | + if (!seen[a]) { |
| 117 | + res[1][i] = a; |
| 118 | + seen[a] = true; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + return res; |
| 124 | + } |
| 125 | +} |
0 commit comments